crowdstar/background-processing

在PHP-FPM下,在将HTTP响应发送回客户端后继续处理PHP。

1.0.2 2023-12-12 18:11 UTC

This package is auto-updated.

Last update: 2024-09-04 18:37:15 UTC


README

Library Status Latest Stable Version Latest Unstable Version License

此包允许在PHP-FPM下将HTTP响应发送回客户端后继续处理PHP。

此包添加的PHP函数在将HTTP响应发送回客户端后执行,但在PHP关闭(在调用任何注册的关闭函数之前)之前。有关PHP关闭顺序、函数fastcgi_finish_request()及相关主题的详细讨论,请参阅帖子"PHP中的后台处理"。

限制和副作用

此包仅适用于PHP-FPM。不要在CLI、PHP内置Web服务器、mod_php或FastCGI下运行它,因为它不会工作。

在将HTTP响应发送回客户端后,添加的后台函数将继续运行,PHP-FPM进程仍在运行。为了避免对您的Web服务器产生副作用,请相应地使用此包。您可以考虑使用一些工作实例或队列服务器。在您使用此包时,您可以考虑以下建议以最大限度地减少副作用

  • 增加PHP-FPM中的子进程数量。
  • 增加PHP-FPM的最大执行时间。

请注意,当使用锁时,后续请求可能会阻塞,即使客户端已经从先前的请求中收到响应,因为锁可能在先前的请求中运行的后台任务期间仍然处于活动状态。

安装

composer require crowdstar/background-processing:~1.0.0

示例用法

<?php
use CrowdStar\BackgroundProcessing\BackgroundProcessing;

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

// First background task added.
BackgroundProcessing::add(
// Increase $sum by the sum of given numbers. Final value of $sum is this example is 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.";
?>

集成指南

与Symfony集成

<?php
// Sample code borrowed from https://github.com/symfony/demo/blob/v1.2.4/public/index.php
use App\Kernel;
use Symfony\Component\HttpFoundation\Request;

require __DIR__.'/../vendor/autoload.php';

$kernel = new Kernel($env, $debug);
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);

// Method BackgroundProcessing::add() could be called from a bundle, a controller or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
    function() {
        mail('user@example.com', 'test', 'test');
    }
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
?>

与Laravel集成

<?php
// Sample code borrowed from https://github.com/laravel/laravel/blob/5.5/public/index.php
define('LARAVEL_START', microtime(true));

require __DIR__.'/../vendor/autoload.php';

$app = require_once __DIR__.'/../bootstrap/app.php';

$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
    $request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);

// Method BackgroundProcessing::add() could be called from a controller, a middleware or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
    function() {
        mail('user@example.com', 'test', 'test');
    }
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
?>

与Lumen集成

<?php
// Sample code borrowed from https://github.com/laravel/lumen/blob/5.5/public/index.php
$app = require __DIR__ . '/../bootstrap/app.php';
$app->run();

// Method BackgroundProcessing::add() could be called from a controller, a middleware or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
    function() {
        mail('user@example.com', 'test', 'test');
    }
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
?>

与Slim 3集成

<?php
// Sample code borrowed from https://github.com/slimphp/Slim/blob/3.x/example/index.php
require 'vendor/autoload.php';

$app = new Slim\App();

$app->get('/', function ($request, $response, $args) {
    $response->write("Welcome to Slim!");
    return $response;
});

$app->get('/hello[/{name}]', function ($request, $response, $args) {
    $response->write("Hello, " . $args['name']);
    return $response;
})->setArgument('name', 'World!');

$app->run();

// Method BackgroundProcessing::add() can be called from a route, a middleware or
// anywhere before method BackgroundProcessing::run() is called.
CrowdStar\BackgroundProcessing\BackgroundProcessing::add(
    function() {
        mail('user@example.com', 'test', 'test');
    }
);
CrowdStar\BackgroundProcessing\BackgroundProcessing::run();
?>