{"id":14987260,"url":"https://github.com/yidas/codeigniter-queue-worker","last_synced_at":"2025-09-11T02:05:56.538Z","repository":{"id":37821176,"uuid":"147904788","full_name":"yidas/codeigniter-queue-worker","owner":"yidas","description":"CodeIgniter 3 Daemon Queue Worker (Consumer) Management Controller","archived":false,"fork":false,"pushed_at":"2023-06-07T05:20:10.000Z","size":102,"stargazers_count":96,"open_issues_count":4,"forks_count":33,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-07-31T06:02:57.702Z","etag":null,"topics":["codeigniter","codeigniter-queue-worker","consumer","listener","manage-workers","multi-process","workers"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yidas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-08T05:37:28.000Z","updated_at":"2025-05-05T13:00:30.000Z","dependencies_parsed_at":"2024-06-19T02:59:26.160Z","dependency_job_id":null,"html_url":"https://github.com/yidas/codeigniter-queue-worker","commit_stats":{"total_commits":11,"total_committers":1,"mean_commits":11.0,"dds":0.0,"last_synced_commit":"576314a4f1e30305e0016e33d3db86ad4ea1b6eb"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/yidas/codeigniter-queue-worker","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcodeigniter-queue-worker","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcodeigniter-queue-worker/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcodeigniter-queue-worker/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcodeigniter-queue-worker/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yidas","download_url":"https://codeload.github.com/yidas/codeigniter-queue-worker/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yidas%2Fcodeigniter-queue-worker/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273808637,"owners_count":25172128,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-05T02:00:09.113Z","response_time":402,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["codeigniter","codeigniter-queue-worker","consumer","listener","manage-workers","multi-process","workers"],"created_at":"2024-09-24T14:14:20.680Z","updated_at":"2025-09-11T02:05:56.521Z","avatar_url":"https://github.com/yidas.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://codeigniter.com/userguide3/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://upload.wikimedia.org/wikipedia/zh/7/7c/CodeIgniter.png\" height=\"\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eCodeIgniter Queue Worker\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nCodeIgniter 3 Daemon Queue Worker Management Controller\n\n[![Latest Stable Version](https://poser.pugx.org/yidas/codeigniter-queue-worker/v/stable?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)\n[![License](https://poser.pugx.org/yidas/codeigniter-queue-worker/license?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)\n[![Total Downloads](https://poser.pugx.org/yidas/codeigniter-queue-worker/downloads?format=flat-square)](https://packagist.org/packages/yidas/codeigniter-queue-worker)\n\nThis Queue Worker extension is collected into [yidas/codeigniter-pack](https://github.com/yidas/codeigniter-pack) which is a complete solution for Codeigniter framework.\n\n\u003e This library only provides worker controller, you need to implement your own queue driver with handler/process in it.  \n\nFeatures\n--------\n\n- ***Multi-Processing** implementation on native PHP-CLI*\n\n- ***Dynamically Workers dispatching (Daemon)** management*\n\n- ***Running in background permanently** without extra libraries* \n\n- ***Process Uniqueness Guarantee** feature by Launcher*\n\n---\n\nOUTLINE\n-------\n\n- [Demonstration](#demonstration)\n- [Introduction](#introduction)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Configuration](#configuration)\n    - [How to Design a Worker](#how-to-design-a-worker)\n        - [1. Build Initializer](#1-build-initializer)\n        - [2. Build Worker](#2-build-worker)\n        - [3. Build Listener](#3-build-listener)\n    - [Porperties Setting](#porperties-setting)\n        - [Public Properties](#public-properties)\n- [Usage](#usage)\n    - [Running Queue Worker](#running-queue-worker)\n        - [Worker](#worker)\n        - [Listener](#listener)\n    - [Running in Background](#running-in-background)\n        - [Launcher](#launcher)\n        - [Process Status](#process-status)\n\n---\n\nDEMONSTRATION\n-------------\n\nRunning a listener (Daemon) with 2~5 workers setting added per 3 seconds:\n\n```\n$ php index.php job_controller/listen\n2018-10-06 14:36:28 - Queue Listener - Job detect\n2018-10-06 14:36:28 - Queue Listener - Start dispatch\n2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #1 (PID: 13254)\n2018-10-06 14:36:28 - Queue Listener - Dispatch Worker #2 (PID: 13256)\n2018-10-06 14:36:31 - Queue Listener - Dispatch Worker #3 (PID: 13266)\n2018-10-06 14:36:34 - Queue Listener - Job empty\n2018-10-06 14:36:34 - Queue Listener - Stop dispatch, total cost: 6.00s\n```\n\n---\n\nINTRODUCTION\n------------\n\nThis library provides a Daemon Queue Worker total solution for Codeigniter 3 framework with Multi-Processes implementation, it includes Listener (Daemon) and Worker for processing new jobs from queue. You may integrate your application queue (such as Redis) with Queue Worker Controller.\n\nPHP is a lack of support for multithreading at the core language level, this library implements multithreading by managing multiprocessing.\n\nFor more concepts, the following diagram shows the implementation structure of this library:\n\n\u003cimg src=\"https://raw.githubusercontent.com/yidas/codeigniter-queue-worker/master/img/introduction-structure.png\" /\u003e\n\nListener (Daemon) could continue to run for detecting new jobs until it is manually stopped or you close your terminal. On the other hand\n, Worker could continue to run for processing new jobs until there is no job left, which the workers could be called by Listener.\n\nLauncher is suitable for launching a listener process, which the running Listener process could be unique that the second launch would detect existent listener and do NOT launch again.\n\n---\n\nREQUIREMENTS\n------------\nThis library requires the following:\n\n- PHP CLI 5.4.0+\n- CodeIgniter 3.0.0+\n\n---\n\nINSTALLATION\n------------\n\nRun Composer in your Codeigniter project under the folder `\\application`:\n\n    composer require yidas/codeigniter-queue-worker\n    \nCheck Codeigniter `application/config/config.php`:\n\n```php\n$config['composer_autoload'] = TRUE;\n```\n    \n\u003e You could customize the vendor path into `$config['composer_autoload']`\n\n---\n\nCONFIGURATION\n-------------\n\nFirst, create a controller that extends the working controller, and then use your own queue driver to design your own handler to implement the worker controller. There are common interfaces as following:\n\n```php\nuse yidas\\queue\\worker\\Controller as WorkerController;\n\nclass My_worker extends WorkerController\n{\n    // Initializer\n    protected function init() {}\n    \n    // Worker\n    protected function handleWork() {}\n    \n    // Listener\n    protected function handleListen() {}\n}\n```\n\nThese handlers are supposed to be designed for detecting the same job queue, but for different purpose. For example, if you are using Redis as message queue, Listener and Worker detect the same Redis list queue, Listener only do dispatching jobs by forking Worker, while Worker continue to takes out jobs and do the processing until job queue is empty.\n\n### How to Design a Worker\n\n#### 1. Build Initializer\n\n```php\nprotected void init()\n```\n\nThe `init()` method is the constructor of worker controller, it provides you with an interface for defining initializartion such as Codeigniter library loading.\n\n*Example Code:*\n```php\nclass My_worker extends \\yidas\\queue\\worker\\Controller\n{\n    protected function init()\n    {\n        // Optional autoload (Load your own libraries or models)\n        $this-\u003eload-\u003elibrary('myjobs');\n    }\n// ...\n```\n\n\u003e As above, `myjobs` library is defined by your own application which handles your job processes. [Example code of myjobs with Redis](https://github.com/yidas/codeigniter-queue-worker/blob/master/examples/myjobs/MyjobsWithRedis.php)\n\n#### 2. Build Worker\n\n```php\nprotected boolean handleWork(object $static=null)\n```\n\nThe `handleWork()` method is a processor for Worker that continue to take out jobs and do the processing. When this method returns `false`, that means the job queue is empty and the worker will close itself.   \n\n*Example Code:*\n```php\nclass My_worker extends \\yidas\\queue\\worker\\Controller\n{\n    protected function handleWork()\n    {\n        // Your own method to get a job from your queue in the application\n        $job = $this-\u003emyjobs-\u003epopJob();\n        \n        // return `false` for job not found, which would close the worker itself.\n        if (!$job)\n            return false;\n        \n        // Your own method to process a job\n        $this-\u003emyjobs-\u003eprocessJob($job);\n        \n        // return `true` for job existing, which would keep handling.\n        return true;\n    }\n// ...\n```\n\n#### 3. Build Listener\n\n```php\nprotected boolean handleListen(object $static=null)\n```\n\nThe `handleListen()` method is a processor for Listener that dispatches workers to handle jobs while it detects new job by returning `true`. When this method returns `false`, that means the job queue is empty and the listener will stop dispatching.\n\n*Example Code:*\n```php\nclass My_worker extends \\yidas\\queue\\worker\\Controller\n{\n    protected function handleListen()\n    {\n        // Your own method to detect job existence\n        // return `true` for job existing, which leads to dispatch worker(s).\n        // return `false` for job not found, which would keep detecting new job\n        return $this-\u003emyjobs-\u003eexists();\n    }\n// ...\n```\n\n\n### Porperties Setting\n\nYou could customize your worker by defining properties.\n\n```php\nuse yidas\\queue\\worker\\Controller as WorkerController;\n\nclass My_worker extends WorkerController\n{\n    // Setting for that a listener could fork up to 10 workers\n    public $workerMaxNum = 10;\n    \n    // Enable text log writen into specified file for listener and worker\n    public $logPath = 'tmp/my-worker.log';\n}\n```\n\n#### Public Properties\n\n|Property          |Type     |Deafult      |Description|\n|:--               |:--      |:--          |:--        |\n|$debug            |boolean  |true         |Debug mode |\n|$logPath          |string   |null         |Log file path|\n|$phpCommand       |string   |'php'        |PHP CLI command for current environment|\n|$listenerSleep    |integer  |3            |Time interval of listen frequency on idle|\n|$workerSleep      |integer  |0            |Time interval of worker processes frequency|\n|$workerMaxNum     |integer  |4            |Number of max workers|\n|$workerStartNum   |integer  |1            |Number of workers at start, less than or equal to $workerMaxNum|\n|$workerWaitSeconds|integer  |10           |Waiting time between worker started and next worker starting|\n|$workerHeathCheck |boolean  |true         |Enable worker health check for listener|\n\n---\n\nUSAGE\n-----\n\nThere are 3 actions for usage:\n\n- `listen` A listener (Daemon) to manage and dispatch jobs by forking workers.\n- `work` A worker to process and solve jobs from queue.\n- `launch` A launcher to run `listen` or `work` process in background and keep it running uniquely.\n\nYou could run above actions by using Codeigniter 3 PHP-CLI command after configuring a Queue Worker controller.\n\n### Running Queue Worker\n\n#### Worker\n\nTo process new jobs from the queue, you could simply run Worker: \n\n```\n$ php index.php myjob/work\n```\n\nAs your worker processor `handleWork()`, the worker will continue to run (return `true`) until the job queue is empty (return `false`).\n\n\n#### Listener\n\nTo start a listener to manage workers, you could simply run Listener:\n\n```\n$ php index.php myjob/listen\n```\n\nAs your listener processor `handleListen()`, the listener will dispatch workers when detecting new jobs (return `true`) until the job queue is empty with stopping dispatching and listening for next new jobs (return `false`).\n\nListener manage Workers by forking each Worker into running process, it implements Multi-Processes which could dramatically improve job queue performance.\n\n### Running in Background\n\nThis library supports running Listener or Worker permanently in the background, it provides you the ability to run Worker as service.\n\n#### Launcher\n\nTo run Listener or Worker in the background, you could call Launcher to launch process:\n\n```\n$ php index.php myjob/launch\n```\n\nBy default, Launcher would launch `listen` process, you could also launch `work` by giving parameter:\n\n```\n$ php index.php myjob/launch/worker\n```\n\nLauncher could keep launching process running uniquely, which prevents multiple same listeners or workers running at the same time. For example, the first time to launch a listener:\n\n```ps\n$ php index.php myjob/launch\nSuccess to launch process `listen`: myjob/listen.\nCalled command: php /srv/ci-project/index.php myjob/listen \u003e /dev/null \u0026\n------\nUSER   PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND\nuser 14650  0.0  0.7 327144 29836 pts/3    R+   15:43   0:00 php /srv/ci-project/index.php myjob/listen\n```\n\nThen, when you launch the listener again, Launcher would prevent repeated running:\n\n```ps\n$ php index.php myjob/launch\nSkip: Same process `listen` is running: myjob/listen.\n------\nUSER   PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND\nuser 14650  0.4  0.9 337764 36616 pts/3    S   15:43   0:00 php /srv/ci-project/index.php myjob/listen\n```\n\nFor uniquely work scenario, you may use database as application queue, which would lead to race condition if there are multiple workers handling the same jobs. Unlike memcache list, database queue should be processed by only one worker at the same time. \n\n#### Process Status\n\nAfter launching a listener, you could check the listener service by command `ps aux|grep php`:\n\n```ps\n...\nwww-data  2278  0.7  1.0 496852 84144 ?        S    Sep25  37:29 php-fpm: pool www\nwww-data  3129  0.0  0.4 327252 31064 ?        S    Sep10   0:34 php /srv/ci-project/index.php myjob/listen\n...\n```\n\nAccording to above, you could manage listener and workers such as killing listener by command `kill 3129`.\n\nWorkers would run while listener detected job, the running worker processes would also show in `ps aux|grep php`.\n\n\u003e Manually, you could also use an `\u0026` (an ampersand) at the end of the listener or worker to run in the background.\n\n\n\n\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fcodeigniter-queue-worker","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyidas%2Fcodeigniter-queue-worker","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyidas%2Fcodeigniter-queue-worker/lists"}