Skip to content

Commit 9276e5c

Browse files
committed
Update readme
1 parent 0ef6879 commit 9276e5c

1 file changed

Lines changed: 20 additions & 9 deletions

File tree

README.md

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ All you need to have installed:
2929
6. Use cases
3030

3131
# 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.
3432

3533
## What is a Worker?
3634
**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:
4038

4139
### How to create your Worker
4240

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.
4442

4543
For example:
4644
```php
@@ -52,6 +50,8 @@ class SleepingWorker extends Worker
5250
echo 'I have started at '.date('r').PHP_EOL;
5351
sleep(3);
5452
echo 'I have ended at '.date('r').PHP_EOL;
53+
54+
return true;
5555
}
5656
}
5757
```
@@ -324,7 +324,7 @@ $pool->setPoolSize(2);
324324
// dispatch payload to workers. Notice! WorkersPool uses sendData() method instead of sendPayload().
325325
foreach ($files as $file) {
326326
echo 'Enqueuing '.$file[0].' with size '.$file[1].PHP_EOL;
327-
$pool->sendData([$file]);
327+
$pool->sendData($file);
328328
}
329329

330330
// register tracker, which should be launched every 0.5 seconds.
@@ -365,19 +365,30 @@ As you can see, we got few improvements:
365365
3. We don't take care of worker termination anymore. Let WorkersPool work for us.
366366

367367
# 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.
369375

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.
372382

373383
**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()`.
374384

375-
## WorkersPool features
385+
## WorkersPool API
376386

377387
- `countIdleWorkers(): integer` - returns number of workers that are in `Worker::IDLE` state.
378388
- `countRunningWorkers(): integer` - returns number of workers that are in `Worker::RUNNING` state.
379389
- `countActiveWorkers(): integer` - returns number of workers that are either in `Worker::RUNNING` or `Worker::IDLE` states.
380-
- `enableDataOverhead()` - enables _dataOverhead_-mode.
390+
- `getRunningWorkers(): Worker[]` - returns workers that are in `Worker::RUNNING` state.
391+
- `enableDataOverhead()` / `disableDataOverhead()` - enables/disables _dataOverhead_-mode.
381392
- `sendData($data, $wait = false): null|boolean` - dispatches payload to any free worker. Behavior depends on _dataOverhead_ feature status:
382393
- 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`).
383394
- 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

Comments
 (0)