PHP code example of geckoboom / scheduler

1. Go to this page and download the library: Download geckoboom/scheduler library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

geckoboom / scheduler example snippets


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);     
    }
}

   $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
   );

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

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

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
);

   $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;
        }
   );

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

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