Giter Site home page Giter Site logo

matfish2 / craft-console-scheduler Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 51 KB

Schedule CraftCMS Console Commands in Code

Home Page: https://plugins.craftcms.com/console-scheduler?craft4

License: Other

PHP 100.00%
craft4 craftcms craftcms-plugin cron-jobs crontab scheduler

craft-console-scheduler's Introduction

Console Scheduler

This package adds the ability to schedule console commands directly from code.

Why?

While you may write a cron configuration entry for each task you need to schedule on your server, this can quickly become a pain, because your task schedule is not recorded to source control and you must SSH into your server to view your existing cron entries, add additional entries or change the schedule.

This package will allow you to:

  1. Define only one command in crontab and the rest will be handled in code.
  2. Use intuitive and readable fluent syntax to define the frequency.
  3. Log all executed commands to a dedicated console-scheduler-{date}.log file, along with any output the command may generate and the exit code.

Installation

  1. Include the package:
composer require matfish/craft-console-scheduler
  1. Install the plugin:
php craft plugin/install console-scheduler

Initial Setup

Add the following line to your crontab

* * * * * /var/www/my-site/php craft console-scheduler/schedule >> /dev/null 2>&1

This will ensure the scheduler runs every minute, and checks for due commands to run. It is recommended to set the frequency according to the highest frequency your project requires. E.g if the most frequent command you have runs hourly, there is no need to call the scheduler every minute, and you can use 0 * * * * instead

Usage

  1. Create a config/console-scheduler.php file
  2. Add schedules according to the following example:
return [
    'schedules' => static function (\matfish\ConsoleScheduler\Schedule\SchedulesCollection $schedule) {
        $schedule->command('activity-logs/logs/prune --days=30 --interactive=0')->monthly();
        $schedule->command('cache/clear')->daily()->at('23:00');
    }
];

Supported frequencies:

  • everyMinute()
  • everyTwoMinutes()
  • everyThreeMinutes()
  • everyFourMinutes()
  • everyFiveMinutes()
  • everyTenMinutes()
  • everyFifteenMinutes()
  • everyThirtyMinutes()
  • hourly()
  • hourlyAt($time)
  • everyOddHour()
  • everyTwoHours()
  • everyThreeHours()
  • everyFourHours()
  • everySixHours()
  • daily()
  • dailyAt($time)
  • twiceDaily($hour1 = 1,$hour2 = 13)
  • twiceDailyAt($hour1 = 1,$hour2 = 13, $offset = 0)
  • weekdays()
  • weedends()
  • days($day1,$day2, $day3, etc) - specific days of week
  • sundays()
  • mondays()
  • tuesdays()
  • wednesdays()
  • thrusdays()
  • fridays()
  • satrudays()
  • weekly()
  • weeklyOn($dayOfWeek, $time = '0:0')
  • monthly()
  • monthlyOn($dayOfMonth, $time)
  • twiceMonthly($firstDay = 1, $secondDay = 16, $time = '0:0')
  • lastDayOfMonth($time = '0:0')
  • quarterly()
  • quarterlyOn($dayOfQuarter = 1, $time = '0:0')
  • yearly()
  • yearlyOn($month = 1, $dayOfMonth = 1, $time = '0:0')

Methods without a specified time can be followed by an at method using fluent syntax.

If you need more control you can also use the raw cron method to define your own frequency, e.g:

$schedule->command('my/cool/command')->cron('*/5 * 3 7 4'); // Every 5 minutes, on day 3 of the month, and on Thursday, only in July

Testing

When CRAFT_ENVIRONMENT=dev you can pass a --test option to the scheduler (in Y-m-d H:i format), to mimic a time which is not the present, and make sure the commands run as expected. E.g:

php craft console-scheduler/schedule --test="2024-01-26 19:05" 

The --test option will be ignored in any other environment.

License

You can try Console Scheduler in a development environment for as long as you like. Once your site goes live, you are required to purchase a license for the plugin. License is purchasable through the Craft Plugin Store.

For more information, see Craft's Commercial Plugin Licensing.

Requirements

This plugin requires Craft CMS 4.0.0 or later.

Acknowledgements

This package is heavily inspired by Laravel's Task Scheduling feature.

Contribution Guidelines

Community is at the heart of open-source projects. We are always happy to receive constructive feedback from users to incrementally improve the product and/or the documentation.

Below are a few simple rules that are designed to facilitate interactions and prevent misunderstandings:

Please only open a new issue for bug reports. For feature requests and questions open a new Discussion instead, and precede [FR] to the title.

If you wish to endorse an existing FR please just vote the OP up, while refraining from +1 replies.

craft-console-scheduler's People

Contributors

matfish3 avatar

Stargazers

G. Jason Head avatar

Watchers

Matanya 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.