用于处理长时间运行进程的工具

1.1.0 2021-12-18 08:40 UTC

This package is auto-updated.

Last update: 2024-09-22 14:11:05 UTC


README

这是从 LongRunning 单一仓库中分离出来的只读分支。

如果您想进行修改,请在此处创建 pull request 那里.

安装

composer require long-running/core

Symfony

如果您使用的是 Symfony,请确保启用该捆绑包

<?php
// config/bundles.php

return [
    // ...
    LongRunning\Core\Bundle\LongRunningBundle::class => ['all' => true],
];

如何使用?

<?php

final class MyCleaner implements \LongRunning\Core\Cleaner
{
    public function cleanUp() : void
    {
        echo "Cleaning up memory!";
    }
}

$cleaner = new \LongRunning\Core\DelegatingCleaner([
    new MyCleaner(),
]);

while (true) {
    // Do heavy work, like processing jobs from a queue
    echo "Doing heavy work";
    sleep(1);
    echo "Done with heavy work";

    // Cleanup things
    $cleaner->cleanUp();
}

如果您使用的是 Symfony,任何实现了 LongRunning\Core\Cleaner 接口的服务都将自动配置并添加到 LongRunning\Core\DelegatingCleaner.

LongRunning\Core\DelegatingCleaner 被别名为 LongRunning\Core\Cleaner.

这意味着您可以在您的 worker 中注入 LongRunning\Core\Cleaner 服务,并在 cleanUp() 时调用所有配置的清理器

<?php

namespace App;

use LongRunning\Core\Cleaner;

final class Worker
{
    private Cleaner $cleaner;

    public function __construct(Cleaner $cleaner)
    {
        $this->cleaner = $cleaner;
    }

    public function doWork() : void
    {
        while (true) {
            // Do heavy work, like processing jobs from a queue
            echo "Doing heavy work";
            sleep(1);
            echo "Done with heavy work";

            // Cleanup things
            $this->cleaner->cleanUp();
        }
    }
}

现有清理器

LongRunning 提供了 2 个包,它们添加了额外的清理器