Skip to content
This repository was archived by the owner on Feb 6, 2019. It is now read-only.

Commit 53b1161

Browse files
committed
(pkg) init commit
1 parent e6804c9 commit 53b1161

File tree

14 files changed

+369
-0
lines changed

14 files changed

+369
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
vendor
2+
composer.lock

.travis.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
language: php
2+
php:
3+
- '7.0'
4+
- '7.1'
5+
before_script:
6+
- composer install --dev
7+
script:
8+
- vendor/bin/codecept run

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6+
7+
## [Unreleased]
8+
9+
## [1.0.0] - 18.10.2017
10+
### [Added]
11+
12+
* added methods for forking tasks of, closing them, and checking on them.
13+
* added docs
14+
* added rudimental test

README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# task
2+
3+
> A PHP process forking and fork handling library.
4+
5+
[![Build Status](https://travis-ci.org/troublete/load.svg?branch=master)](https://travis-ci.org/troublete/task)
6+
7+
## Install
8+
9+
```bash
10+
$ composer require troublete/task
11+
```
12+
13+
## Usage
14+
15+
```php
16+
<?php
17+
require_once '/path/to/autoload.php';
18+
19+
use function Task\{forkTask, checkFail, checkSuccess};
20+
21+
$pid = forkTask(function () {
22+
// do something that is only happening in the forked process
23+
});
24+
25+
// continue work...
26+
27+
checkFail($pid); // to check if the process failed
28+
checkSuccess($pid); // to check if the process finished with great success
29+
```
30+
31+
## API
32+
33+
### Functions
34+
35+
#### forkTask($taskClosure)
36+
37+
Function to fork off a child process. Returns the `pid` of the forked off process if successfull, throws an Exception if forking was not possible.
38+
39+
##### Arguments
40+
41+
| Argument | Type | Description |
42+
|---|---|---|
43+
| $taskClosure | `callable` | Closure function that is only executed in the child process |
44+
45+
#### getProcessStatus($processId = null)
46+
47+
Function that returns that status of a forked child by process id. If provided process id is null it'll return `0`.
48+
49+
##### Arguments
50+
51+
| Argument | Type | Description |
52+
|---|---|---|
53+
| $processId | `int` | Id of the process to be checked |
54+
55+
#### checkAvailable($processId = null)
56+
57+
Function to check if a process exists.
58+
59+
##### Arguments
60+
61+
| Argument | Type | Description |
62+
|---|---|---|
63+
| $processId | `int` | Id of the process to be checked |
64+
65+
#### checkSuccess($processId = null)
66+
67+
Function that returns based on the process status if a process already finished with great success. If the return value of this is `false` it does not necessarily mean that the process failed though. Since this is a non blocking process check. It is just not successfully finished at the point of the check.
68+
69+
##### Arguments
70+
71+
| Argument | Type | Description |
72+
|---|---|---|
73+
| $processId | `int` | Id of the process to be checked |
74+
75+
#### checkFail($processId = null)
76+
77+
Function that checks if a processed failed. If the return value is `false` it does not mean that the process didnt fail. Since this is a non blocking check, it is just not failed in the moment of the check.
78+
79+
##### Arguments
80+
81+
| Argument | Type | Description |
82+
|---|---|---|
83+
| $processId | `int` | Id of the process to be checked |
84+
85+
#### closeTask($processId = null)
86+
87+
Function to close a forked process. Returns `true` if successfull.
88+
89+
##### Arguments
90+
91+
| Argument | Type | Description |
92+
|---|---|---|
93+
| $processId | `int` | Id of the process to be checked |
94+
95+
## License
96+
97+
GPL-2.0 © Willi Eßer

codeception.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
paths:
2+
tests: tests
3+
output: tests/_output
4+
data: tests/_data
5+
support: tests/_support
6+
envs: tests/_envs
7+
actor_suffix: Tester
8+
extensions:
9+
enabled:
10+
- Codeception\Extension\RunFailed

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "troublete/task",
3+
"description": "A PHP process forking and fork handling library.",
4+
"type": "library",
5+
"license": "GPL-2.0",
6+
"authors": [
7+
{
8+
"name": "Willi Eßer",
9+
"email": "[email protected]"
10+
}
11+
],
12+
"minimum-stability": "stable",
13+
"autoload": {
14+
"files": [
15+
"./task.php"
16+
]
17+
},
18+
"require": {
19+
"php": ">=7.0"
20+
},
21+
"require-dev": {
22+
"codeception/codeception": "^2.3"
23+
}
24+
}

task.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
namespace Task;
3+
4+
/**
5+
* Function to fork a task from the main thread, execute its task and return the pid
6+
* @param callable $taskClosure
7+
* @return mixed
8+
*/
9+
function forkTask(callable $taskClosure) {
10+
$processId = pcntl_fork();
11+
if ($processId === -1) {
12+
throw new Exception('The task could not be forked off.');
13+
}
14+
15+
if (
16+
$processId !== -1
17+
&& $processId !== 0
18+
) {
19+
return $processId;
20+
}
21+
22+
if ($processId === 0) {
23+
call_user_func($taskClosure);
24+
}
25+
}
26+
27+
/**
28+
* Function to check the status of a process by process id
29+
* @param int $processId
30+
* @return int|null
31+
*/
32+
function getProcessStatus($processId = null) {
33+
if ($processId === null) {
34+
return null;
35+
}
36+
37+
return pcntl_waitpid($processId, $status, WNOHANG);
38+
}
39+
40+
/**
41+
* Function to check if a process exists
42+
* @param int|null $processId
43+
* @return bool
44+
*/
45+
function checkAvailable($processId = null): bool {
46+
$state = getProcessStatus($processId);
47+
return $state !== null;
48+
}
49+
50+
/**
51+
* Function to check if a process succeeded
52+
* @param int|null $processId
53+
* @return bool
54+
*/
55+
function checkSuccess($processId = null): bool {
56+
$state = getProcessStatus($processId);
57+
return $state !== null && $state !== -1;
58+
}
59+
60+
/**
61+
* Function to check if a process failed
62+
* @param int|null $processId
63+
* @return bool
64+
*/
65+
function checkFail($processId = null): bool {
66+
$state = getProcessStatus($processId);
67+
return $state === -1;
68+
}
69+
70+
/**
71+
* Function to close a fork by process id
72+
* @param int $processId
73+
* @return bool
74+
*/
75+
function closeTask(int $processId): bool {
76+
return posix_kill($processId, SIGQUIT);
77+
}

tests/_data/.gitkeep

Whitespace-only changes.

tests/_output/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*
2+
!.gitignore

tests/_support/Helper/Unit.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Unit extends \Codeception\Module
8+
{
9+
10+
}

0 commit comments

Comments
 (0)