GithubHelp home page GithubHelp logo

cron-bundle's Introduction

Lingoda's cron bundle

This bundle allows scheduling of jobs that will execute in a different process than the process that triggered them

How to use

Either extend the ScheduleBaseCronJob abstract class or implement the CronJobInterface interface. These will automatically be recognized by your Symfony application. Then there's nothing left to do than execute the command bin/console lg:cron:trigger-due-jobs. This will send a message on the application's messenger bus for each recognized cron job that is due. Usually, this means that a message will be put on some sort of a queue which will then be picked up by a handler which will actually run the job.

When implementing a cron job, you shall not expect the cron to actually run at the scheduled times.

  • The queue might be unavailable or overloaded, and you can't predict the delay
  • The actual previous run time will be passed to your run method. Use it!
  • The job might be run manually. Or it might have been run manually previously. Even multiple times.

Triggering individual jobs

bin/console lg:cron:run-job App\\Cron\\SomeCronJobImplementation

Examples

Extending ScheduleBaseCronJob

class MyCronJob extends ScheduleBaseCronJob
{
    protected function getSchedule(): Schedule
    {
        // this job will execute every hour at the 30th minute
        return Schedule::everyHour(30);
    }

    public function run(): void
    {
        // do the job here
    }
}

Implementing CronJobInterface

class MyCronJob implements CronJobInterface
{
    public function run(): void
    {
        // do the job
    }

    public function shouldRun(DateTimeInterface $lastFinishTime = null): bool
    {
        // implement your own logic for determining if a job should
        // at a particular time or not
    }

    public function getLockTTL(): float
    {
        // before running, an expiring lock is acquired for each job to prevent
        // a job of the same type to run at the same time; with this method
        // the job can specify a custom expiry period in seconds for the lock

        return 30.0;
    }
    
    public function __toString(): string
    {
        // Here comes the cron expression as string, e.g. 30 1 * * *
        return '30 1 * * *';
    }
}

cron-bundle's People

Contributors

balazscsaba2006 avatar stas-lingoda avatar

Watchers

 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.