Skip to content

Commit e7713c1

Browse files
committed
add README
1 parent c8c5c4f commit e7713c1

1 file changed

Lines changed: 69 additions & 0 deletions

File tree

README.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
[![Build Status](https://travis-ci.org/Crowdstar/background-processing.svg?branch=master)](https://travis-ci.org/Crowdstar/background-processing)
2+
3+
This package allows to continue processing PHP after having HTTP response sent back to the client under PHP-FPM.
4+
5+
PHP functions added by this package are executed after HTTP response sent back to the client but before PHP shutdown (
6+
before any registered shutdown function is called).
7+
8+
# Limitations and Side Effects
9+
10+
This package is for PHP-FPM only. Don't try to run it under CLI, PHP built-in web server, mod_php or FastCGI since it
11+
won't work.
12+
13+
This package doesn't end PHP session before running background tasks. Because of this, it could affect HTTP requests that
14+
use same session. Thus, this package should be used only for PHP applications where session is not used, e.g., stateless
15+
micro-services.
16+
17+
After sending HTTP response back to client side, background functions added continue to run and the PHP-FPM process is
18+
still running. To avoid side effects on your web server, please use this package accordingly. You may consider to use
19+
some worker instances or queue servers instead. When using this package, you may consider following suggestions to
20+
minimize side effects:
21+
22+
* increase child processes in PHP-FPM.
23+
* increase maximum execution time for PHP-FPM.
24+
25+
# Installation
26+
27+
```bash
28+
composer require crowdstar/background-processing:@dev
29+
```
30+
31+
# Examples
32+
33+
##
34+
35+
```php
36+
<?php
37+
use CrowdStar\BackgroundProcessing\BackgroundProcessing;
38+
39+
$sum = 0;
40+
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'background-processing.txt';
41+
42+
// First background task added.
43+
BackgroundProcessing::add(
44+
// increase variable $sum by the sum of given numbers. In this example, final value of $sum will be 7 (1+2+4).
45+
function (int ...$params) use (&$sum) {
46+
$sum += array_sum($params);
47+
},
48+
1,
49+
2,
50+
4
51+
);
52+
// Second background task added and will be executed after the first one.
53+
BackgroundProcessing::add(
54+
function () use (&$sum, $file) {
55+
// Number 7 calculated from first task will be written to the file.
56+
file_put_contents($file, $sum);
57+
}
58+
);
59+
60+
// Number 0 will be returned back to HTTP client.
61+
echo "Current sum value is {$sum}. Please check file {$file} in the web server; final sum value there should be 7.\n";
62+
63+
// Send HTTP response back to the client first, then run the two background tasks added.
64+
BackgroundProcessing::run();
65+
66+
// Anything here also runs in background.
67+
echo "This message won't shown up in HTTP response.";
68+
?>
69+
```

0 commit comments

Comments
 (0)