定制 makinacorpus/cron 二次开发

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

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

makinacorpus/cron

Composer 安装命令:

composer require makinacorpus/cron

包简介

Simple applicative cron API

README 文档

README

Simple applicative cron task implementation.

How it works:

  • You as the library user register callables as being cron tasks, using a descriptive attribute.

  • It can be any callable, a function, an anonymous function, an instance method, a class static method or an invokable class instance.

  • You need to setup a system cron entry for running the applicative cron, depending upon your framework, using Symfony a CLI command is provided. This tick must run very often, such as every minute.

  • When the cron runs, for each registered task, it checks its schedule againsts the current sytem date.

  • For each matching schedule, it checks first for the configured minimum delay between two run and excludes tasks which have been run too recently.

  • For each remaining non excluded task, it runs it, and store the latest run data long error message and error trace if any error occured.

Simply put, it takes a task list, test each task schedule against current date, and run it when it matches.

State is per default kept in memory during runtime, then discarded. Future implementations will allow you to store it within PDO and maybe other backends.

State when persisted allows the user to change task schedule without changing the code. Schedule is always stored as a raw string which allows alternative implementations to exist.

Default schedule implementation accepts incomplete POSIX cron expression but only with single digit values. An alternative implementation can use dragonmantank/cron-expression for a more complete POSIX cron expression.

Roadmap

  • makinacorpus/goat-query state store implementation,
  • PDO state store implementation,
  • unit test Symfony integration,
  • logging using psr/log everything everywhere,
  • add scheduler implementation using dragonmantank/cron-expression,
  • cron task list and detailed information restitution command,
  • meaningful information display via console commands.

How to use

First, install it:

composer require makinacorpus/cron

Then proceed with one of the following.

Standalone

Configuring cron tasks

First, create some cron methods:

namespace MyVendor\MyApp\Cron;

use MakinaCorpus\Cron\CronTask;

// Using a function.
#[CronTask(id: 'foo', schedule: '1 2 3 4 5')]
function foo(): void
{
    // Do something.
}

// Using an invokable class.
#[CronTask(id: 'bar', schedule: '@daily')]
class Bar
{
    public function __invoke(): mixed
    {
        // Do something.
    }
}

// Using an instance method.
class Buzz
{
    #[CronTask(id: 'buzz', schedule: '@monthly')]
    public function someMethod(): void
    {
    }
}

// Using a static class method.
class Fizz
{
    #[CronTask(id: 'fizz', schedule: '@weekly')]
    public function someMethod(): void
    {
    }
}

Then create a task registry:

namespace MyVendor\MyApp\Command;

use MakinaCorpus\Cron\TaskRegistry\ArrayTaskRegistry;
use MyVendor\MyApp\Cron\Bar;
use MyVendor\MyApp\Cron\Buzz;
use MyVendor\MyApp\Cron\Fizz;

$taskRegistry = new ArrayTaskRegistry([
    'MyVendor\\MyApp\\Cron\\foo',
    new Bar(),
    [new Buzz(), 'someMethod']
    [Fizz::class, 'someMethod'],
]);

Running it

Then, create a runner and execute it, this is basically the piece of code you need to have in your CLI script that executes the cron:

namespace MyVendor\MyApp\Command;

use MakinaCorpus\Cron\CronRunner;

// $taskRegistry is the instance you created upper.

$runner = new CronRunner($taskRegistry);
$runner->run();

And that's it.

Per default, schedule is forgiving, you may run this script only every 2 or 3 minutes, cron rules will match in a 5 minutes time span after their due date to avoid missing running them.

Symfony

Installing

Start by adding the bundle to the config/bundles.php file:

return [
    // Other bundles.
    MakinaCorpus\Cron\Bridge\Symfony\CronBundle::class => ['all' => true],
];

Configuring cron tasks

Create some services that have cron task methods, it can litteraly be any class or service, the only requirement is to set the CronTask attribute over the targeted methods:

namespace MyVendor\MyApp\Cron;

use MakinaCorpus\Cron\CronTask;

// Using an instance method.
class SomeClassWithCronTaskMethods
{
    #[CronTask(id: 'buzz', schedule: '@monthly')]
    public function someInstanceMethod(): void
    {
    }

    #[CronTask(id: 'buzz', schedule: '@monthly')]
    public static function someStaticMethod(): void
    {
    }
}

Using the makinacorpus/argument-resolver dependency, considering you installed and configured the provided bundle, your methods can have other services as parameters, they will be injected at runtimme.

Make sure they are services in config/services.yaml or via any other service registration method:

services:
    MyVendor\MyApp\Cron\SomeClassWithCronTaskMethods:
        autoconfigure: true

And that's it.

Usage

Configuring schedule implementation

Default implementation

Default implementation if configuration is left untouched supports incomplete POSIX cron expressions, where parts can only be single digits.

For a lot of applications, this is more than enough.

You don't need to configure anything since this is the default.

dragonmantank/cron-expression

First install it:

composer require dragonmantank/cron-expression

Then, during your application bootstrap, call:

use MakinaCorpus\Cron\ScheduleFactoryRegistry;
use MakinaCorpus\Cron\Schedule\CronExpressionScheduleFactory;

ScheduleFactoryRegistry::set(new CronExpressionScheduleFactory());

And use this API as you would normally do.

Commands

Commands are available when using it as a Symfony bundle, but nothing prevents you from setting up and using those outside of the Symfony full stack framework usage.

Run all cron tasks (that should run every minute)

Set this in your system cron, or supervisord, or any other orchestrator application:

crontab -e

# Run every minute
* * * * * /symfony/project/path/bin/console cron:run

You can run it manually as well:

bin/console cron:run

Force run a single cron task

Simply call the same command, adding the cron task identifier as first argument:

bin/console cron:run my_cron_task_id

Task configuration

Set a minimum interval

Write me.

Running tests

Core tests

Simply run PHPUnit:

composer install
vendor/bin/phpunit

Database related tests will be skipped due to the lack of configuration.

Database related tests

This uses docker compose for spawning a database environement:

cd sys/
./run-test.sh

This is experimental, it should work.

makinacorpus/cron 适用场景与选型建议

makinacorpus/cron 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 makinacorpus/cron 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2023-05-22