Giter Site home page Giter Site logo

taskschedulerbundle's Introduction

Task Scheduler Bundle

A task scheduler for Symfony applications using CRON jobs porting that allows you to run tasks periodically.

Requirements

For this bundle to work, you must be able to define CRON jobs on your server.

Installation

Start by adding the bundle to your composer.json : composer require rewieer/taskschedulerbundle

Symfony Flex will automatically enable that bundle for you. If you are not using Flex yet, add the bundle to your AppKernel.php :

// in AppKernel::registerBundles()
$bundles = array(
    // ...
    new Rewieer\TaskSchedulerBundle\RewieerTaskSchedulerBundle(),
    // ...
);

You're ready to start scheduling!

Usage

First, add the following line to your CRON tabs : * * * * * php /path/to/your/project/bin/console ts:run >> /dev/null 2>&1

This will call the scheduler, which will review all scheduled tasks and dispatch accordingly. You can call a task directly by providing it's ID as a parameter to the command. Note that this ID is generated by the scheduler and is in no way related to the service itself.

The >> /dev/null 2>&1 option discards the printed results of the tasks. Removing it allows to send it by email instead. This may vary so please refer to your server's configuration.

You then need to create a task. Tasks can be any service! You just have to give it the ts.task tag and implement TaskInterface, or for simplicity extend from AbstractScheduledTask.

Every X Minutes

In Foo\Bar\Task to schedule a task every 5 minutes:

use Rewieer\TaskSchedulerBundle\Task\AbstractScheduledTask;
use Rewieer\TaskSchedulerBundle\Task\Schedule;

class Task extends AbstractScheduledTask {
  protected function initialize(Schedule $schedule) {
    $schedule
      ->everyMinutes(5); // Perform the task every 5 minutes
  }

  public function run() {
    // Do stuff
  }
}

Your task is now scheduled and will be called every 5 minutes.

Every X Hours or Daily

if you are scheduling your task on an hourly or daily basis, you'll need to set minutes() in addition to hours(), everyHours(), or daily(). If you omit minutes(), the task will run every minute on the scheduled hour(s).

In Foo\Bar\Task to schedule a task every 5 hours:

use Rewieer\TaskSchedulerBundle\Task\AbstractScheduledTask;
use Rewieer\TaskSchedulerBundle\Task\Schedule;

class Task extends AbstractScheduledTask {
  protected function initialize(Schedule $schedule) {
    $schedule
      ->minutes(0)
      ->everyHours(5); // Perform the task every 5 hours on minute 0
      
    // Or if you want to perform your task at midnight every day
    // $schedule->minutes(0)->hours(0)->daily();
    
    // Or schedule your task to run once at 9AM daily (this is effectively the same as daily() above)
    // $schedule->minutes(0)->hours(9);
  }

  public function run() {
    // Do stuff
  }
}

You're good to go! You can now check your logs to see if this is working.

taskschedulerbundle's People

Contributors

fwolfsjaeger avatar marcin-derlukiewicz avatar mikeyudin avatar mrshaharb avatar nicohaase avatar paha77 avatar zasco avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

taskschedulerbundle's Issues

ts:run does not review all scheduled tasks

hello, i have created this simple task:

<?php

namespace App\Tasks;

use Psr\Log\LoggerInterface;
use Rewieer\TaskSchedulerBundle\Task\AbstractScheduledTask;
use Rewieer\TaskSchedulerBundle\Task\Schedule;

class NotifyTask extends AbstractScheduledTask
{
  private $log;

  public function __construct(LoggerInterface $log) {
    $this->log = $log;
  }

  protected function initialize(Schedule $schedule)
  {
    $schedule->everyMinutes(2);
  }

  public function run()
  {
    $this->log->debug('This will be called every 2 minutes');
  }
}

and i have give it the tag ts.task like this:

services:
    App\Tasks\NotifyTask:
        tags: ['ts.task']

and i have registered the following cron tab:

* * * * * php /path/to/my/project/bin/console ts:run >> /dev/null 2>&1

but i got this error in dev.log:

[2021-08-24T21:49:04.442160+03:00] scheduler.INFO: [24/08/21 21:49:04] Starting... [] []
[2021-08-24T21:49:04.498320+03:00] console.CRITICAL: Error thrown while running command "ts:run". Message: "Call to a member function isDue() on null" {"exception":"[object] (Error(code: 0): Call to a member function isDue() on null at /myproject/vendor/rewieer/taskschedulerbundle/Task/AbstractScheduledTask.php:27)","command":"ts:run","message":"Call to a member function isDue() on null"} []
[2021-08-24T21:49:04.503243+03:00] console.DEBUG: Command "ts:run" exited with code "1" {"command":"ts:run","code":1} []
[2021-08-24T21:49:04.507488+03:00] php.CRITICAL: Uncaught Error: Call to a member function isDue() on null {"exception":"[object] (Error(code: 0): Call to a member function isDue() on null at /myproject/vendor/rewieer/taskschedulerbundle/Task/AbstractScheduledTask.php:27)"} []

symfony version: 5.3
is there something wrong in this configurations ?

i have tried to run the following commands to make sure the task works correctly:

$ php bin/console ts:list
+----+----------------------+
| ID | Class                |
+----+----------------------+
| 1  | App\Tasks\NotifyTask |
+----+----------------------+

$ php bin/console ts:run 1

output in dev.log:

[2021-08-24T21:47:47.671968+02:00] scheduler.INFO: Running App\Tasks\NotifyTask [] []
[2021-08-24T21:47:47.673133+02:00] app.DEBUG: This will be called every 2 minutes [] []
[2021-08-24T21:47:47.673175+02:00] scheduler.INFO: Finished App\Tasks\NotifyTask in 0.001207s [] []

Documentation Update: Clarification

Hello,

Thank you for the work on the bundle. It's very helpful. I think it may be a good idea to specify in the documentation that if you're using the hours scheduling (hours(), everyHours(), daily), that they should also need to specify the minutes() accordingly.

Currently, if you specify $schedule->everyHours(3), the job will run at every minute within the hours specified. Users need to specify $schedule->minutes(0)->everyHours(3) to ensure it only runs once every 3 hours.

Took a little time to figure that one out, and might help users going forward.

Thanks!

Documentation induces in error

Documentation says to use ts:run 1, but this calls the task ID nĀ°1 with no regard of it's schedule and instead rely on the associated CRON recurrency. I get this may be the desired feature, but it isn't clear. It should be clearly mentionned that to use the scheduler, which is in my sense the default behaviour, no id should be provided. Documentation should hence be adjusted accordingly.

Impossible to use string values in methods

Using the minutes() and hours() methods of class Rewieer\TaskSchedulerBundle\Task\Schedule, it is impossible to use a string as value. Their declaration requires it to be an int and thus prevents the use of the wrapper for such cases.

Current workaround is to use Cron\CronExpression's setPart().

Printing in initialize function cancels CRON's recurrency

Any kind of text printing (echo, print_r, var_dump) used in a task's initialize() will make it's Cron fire every minute instead of the configured expression.
However, the Cron's expression is the configured one and isDue is TRUE.
Log says task is run as usual.

This is not so bad, but you have to know.

Compatibility with Symfony 5

After upgrading Sf from 4.2 to 4.3, the following deprecation notices are given:

  • A tree builder without a root node is deprecated since Symfony 4.2 and will not be supported anymore in 5.0.
  • The "Symfony\Component\Config\Definition\Builder\TreeBuilder::root()" method called for the "rewieer_task_scheduler" configuration is deprecated since Symfony 4.3, pass the root name to the constructor instead.
  • The "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" class is deprecated since Symfony 4.2, use "Symfony\Component\Console\Command\Command" with dependency injection instead.
  • The "Rewieer\TaskSchedulerBundle\Command\ListCommand" class extends "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" that is deprecated since Symfony 4.2, use {@see Command} instead.
  • The "Rewieer\TaskSchedulerBundle\Command\RunCommand" class extends "Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand" that is deprecated since Symfony 4.2, use {@see Command} instead.

Unexpected schedule behaviour

I want to run a task hourly. If I configure the $schedule like this in initialize() function of my implementation, the task is always (minutely) executed:

    protected function initialize(Schedule $schedule)
    {
        $schedule
            ->minutes(0)
            ->everyHours()
        ;
    }
var_dump($schedule->getExpression());

outputs:

string(9) "0 * * * *"

which is a valid cron definition (see: https://crontab.guru/#0__**)

After longer debugging I've found that in Task/Schedule.php the variable is passed as an integer but dragonmantank/cron-expression's CronExpression expects a string (in 2.3 it's only a PHPDOC annotation but in the master already a type hint).

If I change the affected line to

$this->cron->setPart(0, (string)$minutes);

It works as expected.

Symfony 7 support

Hello,

Could you please release a version support for Symfony 7.

Thank you,

Working with SF4

I tried to install your bundle using SF4, I used composer, then added this in my config/bundles.php :

Rewieer\TaskSchedulerBundle\RewieerTaskSchedulerBundle::class => ['all' => true],

But when I try the command : php bin\console ts:run

I got this error :

There are no commands defined in the "ts" namespace.

Do I need to do something more to get it working with SF4 ? Thanks.

php8 support

php8 is released, symfony supports it, could we add support for it?

Error Call to a member function isDue() on null

Hi.
I have a service that extends AbstractScheduledTask and methods initialize and run but i have this error:
In AbstractScheduledTask.php line 27:

Call to a member function isDue() on null

/**
* @param Schedule $schedule
*/
protected function initialize(Schedule $schedule)
{
$schedule
->everyMinutes($this->cronTime); // Perform the task every 15 minutes
}

/**
 * @return int
 */
public function run()
{
    return Process::fromShellCommandline('php bin/console supplier:load ' . $this->tagName)->run();
}

Any help here?
Thanks!

compatibility with composer 2

when installing package, composer displays warnings

Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\Event\DummySubscriber located in ./vendor/rewieer/taskschedulerbundle/Tests/Event/EventDispatcherTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\Event\EventDispatcherTest located in ./vendor/rewieer/taskschedulerbundle/Tests/Event/EventDispatcherTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\Task located in ./vendor/rewieer/taskschedulerbundle/Tests/Task/SchedulerTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\ScheduledTask located in ./vendor/rewieer/taskschedulerbundle/Tests/Task/SchedulerTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\SchedulerTest located in ./vendor/rewieer/taskschedulerbundle/Tests/Task/SchedulerTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201
Deprecation Notice: Class Rewieer\TaskSchedulerBundle\Test\ScheduleTest located in ./vendor/rewieer/taskschedulerbundle/Tests/Task/ScheduleTest.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///mnt/c/cygwin64/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201

Sf4 bin/console error

Hello,
i added to my bundle
Rewieer\TaskSchedulerBundle\RewieerTaskSchedulerBundle::class => ['all' => true],
and when i try command bin/console i got 2 errors

In ListCommand.php line 21: Too few arguments to function Rewieer\TaskSchedulerBundle\Command\ListCommand::__construct(), 0 passed in /home/app/api/var/cache/dev/Container21wWpWf/getTs_ListCommandService.php on line 12 and exac tly 1 expected

In RunCommand.php line 21: Too few arguments to function Rewieer\TaskSchedulerBundle\Command\RunCommand::__construct(), 0 passed in /home/app/api/var/cache/dev/Container21wWpWf/getTs_RunCommandService.php on line 12 and exactl y 1 expected

Please help me

Support for Symfony 6 and php 8.1

Hello,

Will there be an upgrade that support upgrading to Symfony 6 with php 8.1 ?
In case there is, is there a timeframe ?

Thank you,

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.