GithubHelp home page GithubHelp logo

atomic's Introduction

StyleCI Build Status

Atomic

Atomic is a PHP schedule library, events can be build and scheduled. You may use Atomic simply to schedule your events, or you can attach an adapter and use our bin provided to run your jobs based on a cron schedule.

Schedule a job

# Schedule an event on the first of december each year.
$yearly = new Atomic\Schedule\Yearly(new DateTime("1st December"));

$event = new Atomic\Event("dec-log-clear", $yearly, function() {
    Logs::clearAll(); // hypothetical event code.
});

# Some check at a later time
if ($event->isDue()) {
    $event->fire();
}

Stacks

If you like you can register all your events no a stack and have the stack fire any events as they are due.

$stack = new Atomic\Event\Stack([$event, $event2, $event3]);
$evnt = $stack->getNextEvent();

if ($evnt) {
    Log::info("Triggering event " . $evnt->getName());
    $result = $evnt->fire();
    Log::info("Event Return: " . $result);
}

If you dont need to care about extra event data besides its return value you can just ask the stack to trigger the events for you, if it finds an event it will run it, if not it will return false.

$return = $stack->trigger();
if ($return) {
    Log::info("Oh, we fired an event. It returned: " . $return);
} else {
    Log::info("No events found.");
}

Mappers

We provide a simple mapper, which will take an array of events return a stack. This is handy if you store events in a database table and you want to load that into a stack.

try {
    $mapper = new Atomic\Event\Mapper($events, [
        'name'      => 'name',
        'schedule'  => 'schedule',
        'callback'  => 'callback',
        'first_run' => 'start_time'
    ]);

    $stack = $mapper->getStack();
    $stack->trigger();

} catch (Exception $e) {
    Log::warn($e->getMessage());
}

Custom Schedules

You can add your own schedules quite simply by using the ScheduleInterface your schedule must accept a datetime on construct and it must return a true or false on isNow if this function returns true a stack will fire this event.

This class would trigger your function Every Wednesday.

namespace Custom\Schedule;

class EveryWednesday implements \Atomic\Schedule\ScheduleInterface
{
    public function __construct(DateTime $time = null)
    {
        return true;
    }

    public function getName()
    {
        return "Every Wednesday";
    }

    public function getNextRun()
    {
        return new DateTime("Wednesday");
    }

    public function getFirstRun()
    {
        return new DateTime("Wednesday");
    }

    public function isNow(DateTime $time = null)
    {
        return date("D") == "Wed";
    }
}

Install

composer require waxim/atomic

Test

phpunit

atomic's People

Contributors

waxim avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.