A library to manage worker processes in a run loop., (*1)
At the heart of any run loop is the Worker Manager. It will track execution count, set up basic POSIX signal handling, and other basic infrastructure., (*3)
The actual tasks are performed by a Worker class - anything that implements
This is an identifer for the type of worker.
return __CLASS__; is a sensible implmentation, but may result in more work that desired when specifying the worker if running in the foreground., (*5)
nice level for the process, which controls the overall system priority.
A number between -20 and 20.
-20 is the highest priority (don't go this high, or you could make the system unresponsive); +20 is the lowest priority.
Worker::NICE_DEFAULT (0) to leave the priority unaffected., (*6)
If this value is not empty, the manager will attempt to update the process title that's visible in utilities like
Does not work on all operating systems., (*7)
Exit the run loop after this many iterations.
Useful to cleanly exit workers that may consume more memory than desirable.
Worker::RUN_LIMIT_UNLIMITED to not enforce a limit., (*8)
Note that implementations should avoid relying this, although it's well understood that PHP code often leaves resources hanging when it was originally designed around the web's shared nothing "work once and die" model., (*9)
Perform the actual work. This should fetch a single job and perform it. Implementations may read a row out of a database, grab a job out of a work queue, etc., (*10)
work() method should only return
false if no work was attempted (nothing in the queue, etc).
It should still return
true if work was attempted and failed., (*11)
WorkerManager->runInForeground($name) to run a single worker in the foreground.
This is ideal for applications designed around containerization (Docker) where some higher-level process manager already exists., (*12)
It is highly recommended to create a single worker script and corresponding container, and control which worker to run with a CLI argument or environment variable., (*13)