Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ncrypthic/daemon
Simple non blocking php daemon library for long running script
https://github.com/ncrypthic/daemon
Last synced: 13 days ago
JSON representation
Simple non blocking php daemon library for long running script
- Host: GitHub
- URL: https://github.com/ncrypthic/daemon
- Owner: ncrypthic
- License: mit
- Created: 2013-10-08T16:01:17.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2015-12-22T05:00:41.000Z (almost 9 years ago)
- Last Synced: 2024-10-11T18:32:46.727Z (about 1 month ago)
- Language: PHP
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
## Background
We often need to implement a long running script in background to do house keeping tasks for our application.
There are couple of techniques that we can use as listed bellow :
1. With cron by default there are no process control mechanism. If we set a script to run every minute, then it is very that there will be a race condition between process execution when every process of script takes more time than the scheduled time to finish.
2. When we use one script to execute a collection of tasks in an array using loop. The tasks will be executed one after another. This will make the last task in the collection have to wait for earlier tasks to finish.
## What this library do
This library provides basic threaded process control to run our tasks but we want to prevent race condition by running each tasks in order on forked child process using PHP **pcntl** extension.
## Minimum Requirements
1. **PHP >= 5.3.6**
2. Installed and enabled **pcntl extension**## Installing
- Via composer
```
requires: {
"ncrypthic/daemon": "dev-master"
}
```## Running
- Create process(es) classes which implements **ProcessInterface** interface
```
addProcess(new AliceProcess());
$manager->addProcess(new BobProcess());
```- Now daemonize the manager
```
daemonize();
} catch (\Ncrypthic\Daemon\Exception\ChildProcessException $exc) {
echo $exc->getMessage();
}
```## Output
- Console output
```
bash-4.2$ php .php
Alice done
Bob done
No child processes
Alice done
Bob done
No child processes
Alice done
Alice done
Bob done
No child processes
Alice done
Bob done
No child processes
Alice done
```- Process manager output
```
php
`+-php
+-php
```-- see examples