定制 stephanschuler/fork-job-runner 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

stephanschuler/fork-job-runner

Composer 安装命令:

composer require stephanschuler/fork-job-runner

包简介

README 文档

README

Creating child processes is expensive, especially when this comes with bootstrapping huge parts of a framework.

This package constantly holds a single PHP process at a certain bootstrap level and forks this process whenever a job is to be executed.

All the demo code blow is to be implemented by the project, it is not provided by this package.

This package does not aim for parallel processing. This might work but other libraries or frameworks like e.g. ReactPHP are way better suited for this.

The primary objective is providing a technical basis for message queues in functional tests. There is specifically the Flowpack Jobqueue Common package shipping a Fake Queue where this package is meant to help providing a replacement.

Jobs

Every code that should be run in a separate process is called a Job. Jobs are created by the parent, serialized, passed to the worker child and unserialized prior to execution. This needs to be considered, especially when dealing with database connections, file pointers and other resources.

<?php
declare(strict_types=1);

namespace StephanSchuler\Demo;

use StephanSchuler\ForkJobRunner\Utility\WriteBack;
use StephanSchuler\ForkJobRunner\Job;

class DemoJob implements Job
{
    public function run(WriteBack $writeBack): void
    {
        // Do something
    }
}

The Job is a regular class definition. Put it in your project where it can be found by the autoloader.

Dispatching

Passing a job from the parent process to isolated execution is called dispatching. There is no routing mechanism to hand jobs to different works nor is it this packages intention to execute jobs asynchronously or in parallel.

<?php
declare(strict_types=1);

namespace StephanSchuler\Demo;

use StephanSchuler\ForkJobRunner\Dispatcher;

$dispatcher = Dispatcher::create(
    \escapeshellcmd(\PHP_BINARY) . ' ' . \escapeshellarg(\__DIR__ . '/loop.php')
);

$dispatcher->run(
    new DemoJob()
);

Creating the dispatcher should be done only once because each dispatcher keeps its own worker process around.

Dispatching should be done wherever needed, e.g. within action methods in MVC controllers or in test methods of unit tests.

The Loop

The Loop is the child process waiting for the incoming Job. The Dispatcher initializes the Loop and passes Jobs along.

This is meant to be a CLI entry point. It's the "loop.php" file mentioned in the example code of the Dispatcher.

<?php
declare(strict_types=1);

use StephanSchuler\ForkJobRunner\Dispatcher;
use StephanSchuler\ForkJobRunner\Loop;

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

// Bootstrap framework here

Loop::create()
    ->run();

The Loop::run() method does never return unless the dispatchers command stream is open.

Responding

Jobs can respond to the dispatching process. Every serializable data can be passed from Job to Dispatcher.

<?php
declare(strict_types=1);

use StephanSchuler\ForkJobRunner\Utility\WriteBack;
use StephanSchuler\ForkJobRunner\Dispatcher;
use StephanSchuler\ForkJobRunner\Job;
use StephanSchuler\ForkJobRunner\Response;

class DemoJob implements Job
{
    public function run(WriteBack $writer) : void
    {
        $writer->send(
            new Response\DefaultResponse('this goes to stdout')
        );
        $writer->send(
            new Response\ThrowableResponse(
                new RuntimeException('This is an exception')
            )
        );
        throw new RuntimeException('This is another exception');
    }
}

assert($dispatcher instanceof Dispatcher);

$job = new DemoJob();
$responses = $dispatcher->run($job);

foreach ($responses as $response) {
    switch (true) {
        case ($response instanceof Response\NoOpResponse):
            // No Op responses act as keep alive signal.
            // There is at least two per job, one at the beginning and one 
            // at the end.
            break;
        case ($response instanceof Response\DefaultResponse):
            // Default responses contain text.
            // They are not used internally.
            \fputs(\STDOUT, $response->get());
            break;
        case ($response instanceof Response\ThrowableResponse):
            // This catches both,
            // the explicite call of $writer->send()
            // as well as the thrown exception.
            \fputs(\STDERR, print_r($response->get(), true));
            break;
       case ($response instanceof Response\Response):
            // Feel free to implement custom responses.
            break;
    }
}

stephanschuler/fork-job-runner 适用场景与选型建议

stephanschuler/fork-job-runner 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.34k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 stephanschuler/fork-job-runner 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 stephanschuler/fork-job-runner 我们能提供哪些服务?
定制开发 / 二次开发

基于 stephanschuler/fork-job-runner 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.34k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-09