You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+20-9Lines changed: 20 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,8 +29,6 @@ All you need to have installed:
29
29
6. Use cases
30
30
31
31
# Structure
32
-
## What is a Threadable?
33
-
**Threadable** - is a trait for adding fork'ing functionality to any class. It's used in `Worker` class.
34
32
35
33
## What is a Worker?
36
34
**Worker** - is a basic class for any worker. It is composed of two substances (physically, stored in one class, but providing different functionalities):
@@ -40,7 +38,7 @@ All you need to have installed:
40
38
41
39
### How to create your Worker
42
40
43
-
The all you need it to inherit `Worker` class (full name is _wapmorgan\Threadable\Worker_) and redefine`onPayload($data)` public method.
41
+
The all you need it to extend `wapmorgan\Threadable\Worker` class and reimplement`onPayload($data)` public method.
44
42
45
43
For example:
46
44
```php
@@ -52,6 +50,8 @@ class SleepingWorker extends Worker
52
50
echo 'I have started at '.date('r').PHP_EOL;
53
51
sleep(3);
54
52
echo 'I have ended at '.date('r').PHP_EOL;
53
+
54
+
return true;
55
55
}
56
56
}
57
57
```
@@ -324,7 +324,7 @@ $pool->setPoolSize(2);
324
324
// dispatch payload to workers. Notice! WorkersPool uses sendData() method instead of sendPayload().
325
325
foreach ($files as $file) {
326
326
echo 'Enqueuing '.$file[0].' with size '.$file[1].PHP_EOL;
327
-
$pool->sendData([$file]);
327
+
$pool->sendData($file);
328
328
}
329
329
330
330
// register tracker, which should be launched every 0.5 seconds.
@@ -365,19 +365,30 @@ As you can see, we got few improvements:
365
365
3. We don't take care of worker termination anymore. Let WorkersPool work for us.
366
366
367
367
# API
368
-
## Worker secrets and important methods
368
+
## Worker API
369
+
370
+
-`sendPayload($data): int` - sends payload to worker and returns serial id for payload.
371
+
-`checkForFinish(): array|null` - checks if worker sent result of payload and returns it in this case.
372
+
-`checkForTermination(): boolean|null` - returns true if worker process has died.
373
+
-`stop($wait = false): boolean` - sends stop command to worker thread. It uses _SIGTERM_ signal to allow worker thread finish work correctly and don't lose any data. If `$wait = true`, holds the execution until the worker is down.
374
+
-`kill($wait = false): boolean` - sends stop command to worker thread. It uses _SIGKILL_ signal and not recommended except special cases, because it simply kills the worker thread and it loses all data being processed in that moment. If `$wait = true`, holds the execution until the worker is down.
369
375
370
-
-`stop($wait = false)` - sends stop command to worker thread. It uses _SIGTERM_ signal to allow worker thread finish work correctly and don't lose any data. If `$wait = true`, holds the execution until the worker is down.
371
-
-`kill($wait = false)` - sends stop command to worker thread. It uses _SIGKILL_ signal and not recommended except special cases, because it simply kills the worker thread and it loses all data being processed in that moment. If `$wait = true`, holds the execution until the worker is down.
376
+
Information:
377
+
-`isActive(): boolean` - true if worker is in `Worker::RUNNING` or `Worker::IDLE` states.
378
+
-`isRunning(): boolean` - true if worker is in `Worker::RUNNING` state.
379
+
-`isIdle(): boolean` - true if worker is in `Worker::IDLE` state.
380
+
-`getPid(): int` - returns process id of worker.
381
+
-`getCurrentPayload(): int` - returns serial number of last done payload.
372
382
373
383
**Warning about worker re-using!** You can't restart a worker that has been terminated (with `stop()` or `kill()`), you need to create new worker and start it with `start()`.
374
384
375
-
## WorkersPool features
385
+
## WorkersPool API
376
386
377
387
-`countIdleWorkers(): integer` - returns number of workers that are in `Worker::IDLE` state.
378
388
-`countRunningWorkers(): integer` - returns number of workers that are in `Worker::RUNNING` state.
379
389
-`countActiveWorkers(): integer` - returns number of workers that are either in `Worker::RUNNING` or `Worker::IDLE` states.
-`sendData($data, $wait = false): null|boolean` - dispatches payload to any free worker. Behavior depends on _dataOverhead_ feature status:
382
393
- When _dataOverhead_ is disabled and `$wait = false` (by default), this method returns `null` when no free workers available or `boolean` with status of dispatching (`true/false`).
383
394
- When _dataOverhead_ is disabled (by default) and `$wait = true`, this method will hold the execution of the script until any worker became free, dispatch your payload to it and return the status of dispatching (`true/false`).
0 commit comments