Skip to content
This repository has been archived by the owner on Dec 20, 2019. It is now read-only.
/ php_scheduler Public archive

Scheduler is a cron-like php environment for docker containers

License

Notifications You must be signed in to change notification settings

readdle/php_scheduler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

General scheduler project

  • Run tests inside docker container docker-compose up

Example usage

<?php

include "./vendor/autoload.php";

// Example of scheduled script
class SomeScript
{
    protected $name;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function __invoke()
    {
        echo time() . " - {$this->name}" . PHP_EOL;
    }
}

// Mock of persistent storage. In production we reccomend to use redis storage
class Storage
{
    protected $keyValue = [];

    public function set(string $key, int $value)
    {
        $this->keyValue[$key] = $value;
    }

    public function get(string $key)
    {
        return array_key_exists($key, $this->keyValue) ? $this->keyValue[$key] : null;
    }
}

$storage = new Storage();

// Create scheduler object
$scheduler = new \Readdle\Scheduler\Scheduler(
    new \Readdle\Scheduler\PersistentStorage(
        'test',
        [$storage, 'set'],
        [$storage, 'get']
    )
);

// Register your scripts
$scheduler->register(10, new SomeScript('10 second'));
$scheduler->register(5, new SomeScript('5 second'));
$scheduler->register(20, new SomeScript('20 second'));
$scheduler->register(7, new SomeScript('7 second'));
$scheduler->register(27, new SomeScript('27 second'));

// Start scheduler loop
$scheduler->loop();