Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Perdiodic Timer and nextiterationSleepingTime #29

Merged
merged 1 commit into from
Apr 25, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
// So you can wait between two iterations if your workers has nothing to do.

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

/**
Expand Down