Skip to content

Commit

Permalink
Add Perdiodic Timer and nextiterationSleepingTime
Browse files Browse the repository at this point in the history
Update comment

Add link to reactPhp

Edit comments and add link

review Fix

Add Note for old PHP version
  • Loading branch information
Romuald PRIOL committed Apr 17, 2018
1 parent 54d325a commit 3450780
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# DaemonBundle [![Build Status](https://travis-ci.org/M6Web/DaemonBundle.svg?branch=master)](https://travis-ci.org/M6Web/DaemonBundle)

Allows you to create daemonized commands.
Allows you to create daemonized commands with the [React event-loop component](https://github.com/reactphp/event-loop).

## Installation

Via composer :

```json
"require": {
"m6web/daemon-bundle":"^3.0"
"m6web/daemon-bundle":"^4.0"
}
```

Expand All @@ -22,13 +22,15 @@ $bundles = [
];
```

Note: For Symfony versions ">=2.3 && <3.0" support, you can use `"m6web/daemon-bundle":"^1.4"`.

Note:
- For Symfony versions ">=2.3 && <3.0" support, you can use `"m6web/daemon-bundle":"^1.4"`.
- For PHP versions ">=5.5.9 && <7.0" support, you can use `"m6web/daemon-bundle":"^3.0"`.

## Configuration

You can optionally define events which are triggered each X iterations :

```yml
```yaml
m6_web_daemon:
iterations_events:
-
Expand All @@ -41,6 +43,8 @@ m6_web_daemon:
## Write command
This command use the [event-loop component](https://github.com/reactphp/event-loop#usage) which [ReactPHP](https://reactphp.org) uses to run loops and other asynchronous tasks.
```php
<?php

Expand All @@ -64,6 +68,16 @@ class MyDaemonizedCommand extends DaemonCommand

// Add your own optional callback : called every 10 iterations
$this->addIterationsIntervalCallback(10, [$this, 'executeEveryTenLoops']);

// You can add your own Periodic Timer,
// Here this timer will be called every 0.5s
$daemon = $this;
$this->loop->addPeriodicTimer(0.5, function ($timer) use ($daemon) {
// It's the last loop, cancel the timer.
if ($daemon->isLastLoop()) {
$daemon->loop->cancelTimer($timer);
}
});
}

/**
Expand All @@ -72,8 +86,11 @@ class MyDaemonizedCommand extends DaemonCommand
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln("Iteration");

usleep(100000);

// This method helps to give back the CPU to the react-loop (here for 1000 $µseconds).
// So you can wait between two iterations if your workers has nothing to do.

$this->setNextIterationSleepingTime(1000000); // Every seconds
}

/**
Expand Down

0 comments on commit 3450780

Please sign in to comment.