From 4d69196bfe4fdd5e879837171543b0ba2c71ada3 Mon Sep 17 00:00:00 2001 From: Netkas Date: Fri, 7 Jul 2023 00:28:35 -0400 Subject: [PATCH] Updated README.md documentation --- README.md | 53 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1bb8c42..5e0b27e 100644 --- a/README.md +++ b/README.md @@ -606,7 +606,7 @@ To learn how to create a worker, see the [Implementing a worker](#implementing-a ### do - > `\TamerLib\tm::do(string $function, array $arguments, int $channel=0): int` + > `\TamerLib\tm::do(string $function, array $arguments=[], ?callable $callback=null, array $options=[]): int` Executes a function on a worker and returns the Job ID, this ID is used to identify the job at a later state once it's completed. this method can also be called statically using the function name as a method name. @@ -614,6 +614,12 @@ completed. this method can also be called statically using the function name as - `$function` - The function to execute on the worker - `$arguments` - The arguments to pass on to the function (can contain classes but they must be serializable, eg. no resources or closures) + - `$callback` *(Optional)* The callback to execute once the job is completed, this callback will be passed on the + result of the job as the first argument + + +Options: + - `$channel` - *(Optional)* The channel to execute the function on, this is only applicable to some workers that may listen to multiple channels, if this is not specified, the function will be executed on channel 0. This is useful for separating different types of jobs on different channels for different workers. @@ -628,17 +634,30 @@ or $job_id = \TamerLib\tm::sleep(5); ``` +To produce callbacks in jobs, you could approach a call as so + +```php +\TamerLib\tm::do('sleep', [5], static function($result) { + echo "Job completed with result: {$result}"; +}); + +\TamerLib\tm::wait(); +``` + +This will execute the callback when the job is completed, this will only work if you call the `wait()` method after +dispatching the job, otherwise the callback will never be executed. + *** ### dof - > `\TamerLib\tm::dof(string $function, array $arguments, int $channel=0): void` + > `dof(string $function, array $arguments=[], array $options=[]): void` The same as [`do`](#do) but in a "Do and forget" method. This method will send the job packet to the server without a return channel, this essentially means that once that worker has finished executing the job and if -fails or succeeds, the job will be dropped regardless and the client will not be notified of the result. +fails or succeeds, the job will be dropped regardless, and the client will not be notified of the result. This method is useful for executing jobs that do not need to return a result, such as logging or sending emails. @@ -652,19 +671,17 @@ This method is useful for executing jobs that do not need to return a result, su ### wait - > `\TamerLib\tm::wait(callable $callback, int $timeout=0): void` + > `\TamerLib\tm::wait(int $timeout=0): void` This method is responsible for waiting for all dispatched jobs to finish executing, this method will block until all jobs have finished executing or until the timeout has been reached. This is usually called after dispatching one or -more jobs and your application needs to wait for the results. +more jobs, and your application needs to wait for the results. -**Note:** This method throw an exception if a Job returns an exception +**Note:** This method throws an exception if a Job returns an exception - - `$callback` - The callback to call when a job has finished executing, this callback will receive the job ID and the - result of the job as arguments. - `$timeout` - *(Optional)* The timeout in seconds to wait for, if this is set to 0, the method will block indefinitely, - or if the value is set to -1 the method will not block at all and will return immediately after it's first iteration. - Otherwise the method will block for the specified amount of seconds. + or if the value is set to -1 the method will not block at all and will return immediately after it's a first iteration. + Otherwise, the method will block for the specified number of seconds. ```php \TamerLib\tm::do('sleep', [5]); @@ -677,13 +694,17 @@ more jobs and your application needs to wait for the results. or ```php -print(\TamerLib\tm::do('sleep', [5]) . PHP_EOL); -print(\TamerLib\tm::do('sleep', [10]) . PHP_EOL); -print(\TamerLib\tm::do('sleep', [15]) . PHP_EOL); - -\TamerLib\tm::wait(function($job_id, $result) { - echo "Job $job_id finished with result $result" . PHP_EOL; +\TamerLib\tm::do('sleep', [5], static function($result) { + echo "Job completed with result: {$result}"; }); +\TamerLib\tm::do('sleep', [10], static function($result) { + echo "Job completed with result: {$result}"; +}); +\TamerLib\tm::do('sleep', [15], static function($result) { + echo "Job completed with result: {$result}"; +}); + +\TamerLib\tm::wait(); ```