elektro-potkan/scheduler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

elektro-potkan/scheduler

Composer 安装命令:

composer require elektro-potkan/scheduler

包简介

Robust job scheduler (cron) with locking

README 文档

README

Simplifies running periodic jobs of PHP application. Just register all of them to the scheduler and let it to run them at specified time by single call.

Heavily modified fork of contributte/scheduler.

Original package reorganised - jobs moved into individual namespace as well as schedulers.

Console and Nette DI integrations split into separate packages. Run composer suggests for more info.

Usage

The bundled jobs are using the cron syntax.

Example

// init
$scheduler = new ElektroPotkan\Scheduler\Schedulers\LastCheck('path-to-directory-for-storing-locks-and-timestamp');

$scheduler->add(new ElektroPotkan\Scheduler\Jobs\Callback('0 3 * * *',// daily at 3:00 a.m.
	function(): void {
		do_something();
	}
));

// run periodically - optimaly each minute, but heavily depends on Your needs and configured jobs
$scheduler->run();

Cron syntax

See description below. You can also validate your cron using crontab.guru.

  *  *  *  *  *
  -  -  -  -  -
  |  |  |  |  |
  |  |  |  |  |
  |  |  |  |  +----- day of week (0 - 7) (Sunday=0 or 7)
  |  |  |  +---------- month (1 - 12)
  |  |  +--------------- day of month (1 - 31)
  |  +-------------------- hour (0 - 23)
  +------------------------- min (0 - 59)

Schedulers

All the schedulers provided in this package are in the namespace ElektroPotkan\Scheduler\Schedulers.

A list of the available schedulers - the next one is extending the previous one, so it also provides all features of all previous ones.

  • Simple
    • The most basic implementation of scheduler.
    • Does not need any storage.
    • If not called each minute, the job's requested run times might be missed.
    • No locking is provided when running the jobs so if called concurrently, the jobs will be also run concurrently.
  • Locking
    • Uses locks when running the jobs, so if called concurrently, it will skip the job if it is already being run by another instance.
    • Needs directory to store locks.
  • LastCheck
    • Provides $lastCheck timestamp to jobs.
    • If not called each minute, the jobs might use such information to be run at the next call. Eliminates the problem of missing the jobs run times even if not called at their exact time. All jobs provided by this package supports this feature.
  • LastRun
    • Most advanced type of scheduler, provides each job with its $lastRun timestamp.
    • Feature not used by the jobs provided by this package as they do not need it.

Jobs

This package provides 2 basic jobs (located in the namespace ElektroPotkan\Scheduler\Jobs):

  • Callback - Runs given callback.
  • AExpression - Abstract class providing cron syntax support.

Callback job

If You need to call a method myClassMethod of Your class instance $myClass, simply use Callback job:

// run each minute by cron syntax
$job = new ElektroPotkan\Scheduler\Jobs\Callback('* * * * *', [$myClass, 'myClassMethod']);
$scheduler->add($job);

Custom cron-syntax job

To get more flexibility for Your job but use cron syntax effortlessly, extend the AExpression job:

class MyCronJob extends ElektroPotkan\Scheduler\Jobs\AExpression {
	private $myDataSource;
	
	
	public function __construct(string $cron, $myDataSource){
		parent::__construct($cron);
		$this->myDataSource = $myDataSource;
	}
	
	public function run(): void {
		$this->myDataSource->runSomePeriodicTask();
	}
}

$job = new MyCronJob('0 0 * * 1', $myDataSource);// run each Monday at midnight
$scheduler->add($job);

Custom job

For full freedom with Your job, just implement the ElektroPotkan\Scheduler\IJob interface:

class MyJob implements ElektroPotkan\Scheduler\IJob {
	private $dateService;
	
	private $statisticsService;
	
	
	public function __construct($dateService, $statisticsService){
		$this->dateService = $dateService;
		$this->statisticsService = $statisticsService;
	}
	
	public function isDue(
		DateTimeInterface $now,
		?DateTimeInterface $lastCheck = null,
		?DateTimeInterface $lastRun = null
	): bool {
		return $this->dateService->isRightTime($now);
	}
	
	public function run(): void {
		$this->statisticsService->calculate();
	}
}

Logging

To log info and errors from built-in schedulers, You need to provide an implementation of ElektroPotkan\Scheduler\ILogger interface via setLogger call:

// $logger implements ElektroPotkan\Scheduler\ILogger
$scheduler->setLogger($logger);

If You are using Tracy, You can use the provided TracyLogger:

// $logger implements Tracy\ILogger
$scheduler->setLogger(new ElektroPotkan\Scheduler\TracyLogger($logger));

Authors

Info

Versioning

This project uses Semantic Versioning 2.0.0 (semver.org).

Branching

This project uses slightly modified Git-Flow Workflow and Branching Model:

License

You may use this program under the terms of the MIT License.

See file LICENSE.

elektro-potkan/scheduler 适用场景与选型建议

elektro-potkan/scheduler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 44 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 09 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「cron」 「scheduler」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 elektro-potkan/scheduler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-09-16