承接 emerick42/kairoi-php 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

emerick42/kairoi-php

Composer 安装命令:

composer require emerick42/kairoi-php

包简介

The official PHP client for Kairoi, a Dynamic, Accurate and Scalable Time-based Job Scheduler.

README 文档

README

A PHP client for Kairoi, a Dynamic, Accurate and Scalable Time-based Job Scheduler.

Quick Words

Before trying to use this library, you should be familiar with Kairoi's core concepts. If it's not yet the case, you can start by reading the Kairoi Official documentation.

About this library, its main goal is to provide an easy-to-use, object-oriented, and low-level programmable interface to communicate with Kairoi servers in PHP. It abstracts managing socket connections with servers, encoding and decoding messages using the Kairoi Client Protocol, and handling Kairoi Instructions. It should be noted that this library is designed to be used with Dependency Injection Containers, and the associated "Actor/Message" pattern as a specialization of Object-Oriented Programming. While this can definitely be considered as a constraint, it allows Kairoi-PHP to be highly extensible.

The usual workflow to work with Kairoi and this library is the following:

  1. Rules and runners are configured for an application, defining how jobs will be processed by Kairoi. You can check the Kairoi Runners documentation for the list of existing runners. Runners should be written to the Kairoi server only once during the deployment of the application, using the programmable interface provided by this library.
  2. Executables (in the application scope) responsible for handling job executions are implemented. For example, in the case of a Shell runner, a CLI can be registered in your favorite framework, taking a job identifier as its main parameter, and executing the domain code associated with the received jobs. This part is outside of the scope of this library.
  3. Jobs are created on the fly by the application during its entire lifetime. When needed during domain processes, the application can schedule jobs through this library. Kairoi will then automatically trigger the job execution at the proper time, launching the domain code associated with this job, following the Rules and Runners configured at the first step.

Usage

Configuring a Client

The first thing you'll need to start communicating with a Kairoi server is a Kairoi\Domain\Client\ClientInterface. The library provides Kairoi\Infrastructure\Client\Client, as a default implementation of this interface. It takes as a parameter a string being the server URL (as accepted by the PHP method stream_socket_client), a Kairoi\Domain\Client\Encoding\EncoderInterface and a Kairoi\Domain\Client\Decoding\DecoderInterface, responsible for respectively encoding Kairoi\Domain\Protocol\Requests to a streamable message to send to the server, and decoding streamed messages received from the server to Kairoi\Domain\Protocol\Response. Once again, this library provides a Kairoi\Domain\Client\Encoding\Encoder to pass as the first parameter, and a Kairoi\Infrastructure\Client\Decoding\ParsicaDecoder for the second.

To summarize, you can create a client, configured to communicate with a Kairoi server listening on 127.0.0.1:5678, with the following code:

<?php

use Kairoi\Domain\Client\Encoding\Encoder;
use Kairoi\Infrastructure\Client\Client;
use Kairoi\Infrastructure\Client\Decoding\ParsicaDecoder;

$client = new Client('tcp://localhost:5678', new Encoder(), new ParsicaDecoder());

Setting a Job

Once the client is configured, it can be used to execute instructions. We're going to talk about the Job Set instruction first. Its goal is to set jobs in Kairoi, so they can be triggered at some point. The job will be triggered as soon as its execution date is past. This library provides a Kairoi\Domain\Job\Set\Writer to achieve this task. It is constructed with a Kairoi\Domain\Client\ClientInterface (the client we configured previously), and a Kairoi\Domain\Job\Set\Driver\DriverInterface responsible for converting jobs into standard Kairoi\Domain\Protocol\Requests. The library provides the service Kairoi\Domain\Job\Set\Driver\Driver as a default implementation for this driver.

Then, we will use the write method to set a single job to the Kairoi server. It takes a Kairoi\Domain\Job\Set\Job as a parameter, and returns a Kairoi\Domain\Job\Set\Result, both being simple Message objects. It should be noted that the returned result will represent the success (or failure) of the "SET" instruction: if the job is written to the Kairoi server, it's a success, but if an error occurs during the communication with the Kairoi server, it's a failure. The Job execution status is completely unrelated.

Here is an example to set the job app.domain.job.1 to be executed in the future:

<?php

use Kairoi\Domain\Job\Set\Driver\Driver;
use Kairoi\Domain\Job\Set\Job;
use Kairoi\Domain\Job\Set\Writer;

$driver = new Driver();
$writer = new Writer($client, $driver);

$job = new Job(
    'app.domain.job.1',
    new \DateTime('+5 minutes')
);
$result = $writer->write($job);
if ($result->isFailure()) {
    printf("Instruction failed to be executed.\n");
}

Setting a Rule

For this job to be executed properly, it needs to match a Kairoi rule that can be set using the Rule Set instruction. Kairoi-PHP provides a Kairoi\Domain\Rule\Set\Writer, that works like the Job Writer: it is constructed with a Kairoi\Domain\Client\ClientInterface and a Kairoi\Domain\Rule\Set\Driver\DriverInterface for converting to standard requests. A default implementation Kairoi\Domain\Rule\Set\Driver\Driver is also provided.

The Kairoi\Domain\Rule\Set\Rule is the Message object used to configure the rule and its associated runner. It takes a string being the unique identifier of this rule as its first parameter, a string being the match pattern as a second parameter, and a Runner as a third parameter. In Kairoi, the match pattern is a simple "starts by" comparison: app.domain. will match every job starting by this string, such as app.domain.job.1 or app.domain.1, but not app.domainjob. The Runner can be anything supported by driver in use.

The default Rule Set driver is a bit more complicated than the Job Set driver. It takes as parameter a collection of Kairoi\Domain\Rule\Set\Driver\Runner\DriverInterface, where each element is responsible for handling a type of runner.

An Shell runner can be configured as the following:

<?php

use Kairoi\Domain\Rule\Set\Driver\Driver;
use Kairoi\Domain\Rule\Set\Driver\Runner\Shell as ShellDriver;
use Kairoi\Domain\Rule\Set\Rule;
use Kairoi\Domain\Rule\Set\Runner\Shell as ShellRunner;
use Kairoi\Domain\Rule\Set\Writer;

$driver = new Driver([new ShellDriver()]);
$writer = new Writer($client, $driver);

$runner = new ShellRunner('/usr/src/app/domain_job.sh');
$rule = new Rule(
    'app.rule.domain.job',
    'app.domain.job.',
    $runner
);
$result = $writer->write($rule);
if ($result->isFailure()) {
    printf("Instruction failed to be executed.\n");
}

emerick42/kairoi-php 适用场景与选型建议

emerick42/kairoi-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.66k 次下载、GitHub Stars 达 8, 最近一次更新时间为 2021 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 emerick42/kairoi-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 8
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-19