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
  • Loading branch information
Romuald PRIOL committed Apr 17, 2018
1 parent 54d325a commit 805f0f9
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 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

Expand Down Expand Up @@ -28,7 +28,7 @@ Note: For Symfony versions ">=2.3 && <3.0" support, you can use `"m6web/daemon-b

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

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

Expand All @@ -64,6 +66,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,
// In 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 +84,12 @@ 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.

// We do not recommand to use sleep() or usleep() anymore.
$this->setNextIterationSleepingTime(1000); // Every milliseconds
}

/**
Expand Down

0 comments on commit 805f0f9

Please sign in to comment.