定制 geckoboom/scheduler 二次开发

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

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

geckoboom/scheduler

最新稳定版本:1.0.5

Composer 安装命令:

composer require geckoboom/scheduler

包简介

Cron scheduler for console commands

关键字:

README 文档

README

Table of Contents

  1. Introduction
  2. Installation
  3. Configuration
  4. Usage
  5. Scheduling
    1. Schedule Frequency Options
    2. Truth Test Constraints 6Advanced

Introduction

Command scheduler is a great approach to managing scheduled console commands on the server. The package allows you to control your task scheduling within the console application. When using a scheduler, only a single cron entry is required on your server.

Installation

This is installable via Composer as This is installable via Composer as geckoboom/scheduler:

composer require geckoboom/scheduler

Configuration

Several steps required to configure scheduler

  1. Provide implementation of Geckoboom\Scheduler\CallerInterface.
class ReflectionCaller implements \Geckoboom\Scheduler\CallerInterface
{
    protected ReflectionContainer $container;
    
    public function call(\Closure $callback){
        $args = [];
        $reflectionMethod = new \ReflectionMethod($callback, '__invoke');
        $args = [];
        foreach ($reflectionMethod->getParameters() as $parameter) {
            if (!$this->container->has($parameter->getName()) && $parameter->isDefaultValueAvailable()) {
                $args[$parameter->getName()] = $parameter->getDefaultValue();
            }
        }
   
        return $this->container->call($callback, $args);     
    }
}
  1. Provide two implementations of EventMutexInterface and ScheduleMutexInterface. The package contains default realizations of these interfaces based on Psr\SimpleCache\CacheInterface dependency. You can do it in such ways
   $container->add(
        \Psr\SimpleCache\CacheInterface::class,
        MyCacheImplementation::class
   );

   $container->add(
        \Geckoboom\Scheduler\EventMutexInterface::class,
        \Geckoboom\Scheduler\EventMutex\CacheEventMutex::class
   );

   $container->add(
        \Geckoboom\Scheduler\ScheduleMutexInterface::class,
        \Geckoboom\Scheduler\ScheduleMutex\CacheScheduleMutex::class
   );

or create your own realizations

    $container->add(
           \Geckoboom\Scheduler\EventMutexInterface::class,
           MyEventMutexImplementation::class
    );

    $container->add(
        \Geckoboom\Scheduler\ScheduleMutexInterface::class,
        MyScheduleMutexImplementation::class
    );
  1. Provide your ScheduleRegistrarInterface dependency.
class MyScheduleRegistrar implements \Geckoboom\Scheduler\ScheduleRegistrarInterface
{
    public function schedule(\Geckoboom\Scheduler\Schedule $schedule) : void {
        $schedule->command('send:emails', ['--option1' => 'value', 'argument1'])
            ->daily()
            ->withoutOverlapping();
        
        $schedule->command('orders:distribute')
            ->mondays()
            ->at('14:00')
            ->runInBackground();
    }
}

...

$container->add(
    \Geckoboom\Scheduler\ScheduleRegistrarInterface::class,
    MyScheduleRegistrar::class
);
  1. Provider Schedule singleton dependency
   $container->addShared(
        \Geckoboom\Scheduler\Schedule::class,
        function ($di): \Geckoboom\Scheduler\Schedule {
            $schedule = new \Geckoboom\Scheduler\Schedule(
                $di->get(\Geckoboom\Scheduler\EventMutexInterface::class),
                $di->get(\Geckoboom\Scheduler\ScheduleMutexInterface::class),
                $di->get(\Geckoboom\Scheduler\CommandBuilder::class),
                '/path/to/project/root',
                new DateTimeZone('Europe/London')
            );
            
            $registrar = $di->get(\Geckoboom\Scheduler\ScheduleRegistrarInterface::class);
            $registrar->schedule($schedule);
            
            return $schedule;
        }
   );

Usage

Now you can create executable script or console command based on the appropriate framework syntax.

   $service = $container->get(\Geckoboom\Scheduler\ScheduleService::class);

   $service->run(new \DateTimeImmutable());

The last step is to add a single cron configuration entry to our server that runs your executable command every minute.

* * * * * cd /path-to-your-project && php /path-to-executable-script >> /dev/null 2>&1

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-10-19

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固