Download the PHP package piotrek-r/worker without Composer
On this page you can find all versions of the php package piotrek-r/worker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download piotrek-r/worker
More information about piotrek-r/worker
Files in piotrek-r/worker
Package worker
Short Description A simple worker implementation to loop a callback function until one of the defined conditions is met
License MIT
Informations about the package worker
Worker
A simple worker implementation to loop a callback function until one of the defined conditions is met
Installation
Usage
Basic usage
The callback function should return a truthy value if it did some work, and a falsy value otherwise. This is used to determine stop conditions and sleep time.
Conditions
WorkerConditions
can be used to set conditions for the worker to stop.
Condition: time limit
You can set a time limit in seconds.
Condition: memory limit
You can set a memory limit in bytes as int or as a string with a unit suffix. Supported units are: b
, k
, m
, g
, t
, p
, e
, z
, y
. It uses memory_get_usage(true)
to determine the current memory usage.
Condition: loop limit
You can set a loop limit. It will stop the worker after the given number of loops.
There are three different types of loop limits:
countLoops
- the number of loops which is the sum of the other typescountHandled
- the number of loops were the function returned a truthy valuecountEmpty
- the number of loops were the function returned a falsy value
To only make 100 loops no matter the result:
To only run 5 times when the function returns true
and loop infinitely when it returns false
:
To run infinitely and stop after the first time the function returns false
(e.g. when draining a queue, it will run until the queue is empty):
Configuration
Sleep time
You can set the sleep time in microseconds. It will sleep after each loop. You can set separate values for sleep time after a loop with a truthy value and a falsy value.
Arguments
You can pass arguments to the run
method. All arguments will be passed to the callback function.
Result
The run
method returns a WorkerResult
object with the following methods:
getTimeElapsed(): int
- the time elapsed in secondsgetCountLoops(): int
- the number of loops which is the sum of the other typesgetCountHandled(): int
- the number of loops were the function returned a truthy valuegetCountEmpty(): int
- the number of loops were the function returned a falsy value