Skip to content

Commit

Permalink
add README
Browse files Browse the repository at this point in the history
  • Loading branch information
deminy committed May 30, 2018
1 parent c8c5c4f commit e7713c1
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
[![Build Status](https://travis-ci.org/Crowdstar/background-processing.svg?branch=master)](https://travis-ci.org/Crowdstar/background-processing)

This package allows to continue processing PHP after having HTTP response sent back to the client under PHP-FPM.

PHP functions added by this package are executed after HTTP response sent back to the client but before PHP shutdown (
before any registered shutdown function is called).

# Limitations and Side Effects

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
won't work.

This package doesn't end PHP session before running background tasks. Because of this, it could affect HTTP requests that
use same session. Thus, this package should be used only for PHP applications where session is not used, e.g., stateless
micro-services.

After sending HTTP response back to client side, background functions added continue to run and the PHP-FPM process is
still running. To avoid side effects on your web server, please use this package accordingly. You may consider to use
some worker instances or queue servers instead. When using this package, you may consider following suggestions to
minimize side effects:

* increase child processes in PHP-FPM.
* increase maximum execution time for PHP-FPM.

# Installation

```bash
composer require crowdstar/background-processing:@dev
```

# Examples

##

```php
<?php
use CrowdStar\BackgroundProcessing\BackgroundProcessing;

$sum = 0;
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'background-processing.txt';

// First background task added.
BackgroundProcessing::add(
// increase variable $sum by the sum of given numbers. In this example, final value of $sum will be 7 (1+2+4).
function (int ...$params) use (&$sum) {
$sum += array_sum($params);
},
1,
2,
4
);
// Second background task added and will be executed after the first one.
BackgroundProcessing::add(
function () use (&$sum, $file) {
// Number 7 calculated from first task will be written to the file.
file_put_contents($file, $sum);
}
);

// Number 0 will be returned back to HTTP client.
echo "Current sum value is {$sum}. Please check file {$file} in the web server; final sum value there should be 7.\n";

// Send HTTP response back to the client first, then run the two background tasks added.
BackgroundProcessing::run();

// Anything here also runs in background.
echo "This message won't shown up in HTTP response.";
?>
```

0 comments on commit e7713c1

Please sign in to comment.