|
| 1 | +# Basic Usage |
| 2 | + |
| 3 | +Tasks are configured with the `app/Config/Tasks.php` config file, inside the `init()` method. |
| 4 | +Let's start with a simple example: |
| 5 | + |
| 6 | +```php |
| 7 | +<?php |
| 8 | + |
| 9 | +namespace Config; |
| 10 | + |
| 11 | +use CodeIgniter\Tasks\Config\Tasks as BaseTasks; |
| 12 | +use CodeIgniter\Tasks\Scheduler; |
| 13 | + |
| 14 | +class Tasks extends BaseTasks |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Register any tasks within this method for the application. |
| 18 | + * |
| 19 | + * @param Scheduler $schedule |
| 20 | + */ |
| 21 | + public function init(Scheduler $schedule) |
| 22 | + { |
| 23 | + $schedule->call(function() { |
| 24 | + DemoContent::refresh(); |
| 25 | + })->mondays(); |
| 26 | + } |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +In this example, we use a closure to refresh demo content at 12:00 am every Monday morning. Closures are |
| 31 | +a simple way to handle quick functions like this. You can also execute server commands, execute custom |
| 32 | +CLI commands you have written, call a URL, or even fire off an Event of your choosing. Details are covered |
| 33 | +below. |
| 34 | + |
| 35 | +## Scheduling |
| 36 | + |
| 37 | +This is how we can schedule our tasks. We have many options. |
| 38 | + |
| 39 | +### Scheduling CLI Commands |
| 40 | + |
| 41 | +If you have written your own [CLI Commands](https://codeigniter.com/user_guide/cli/cli_commands.html), you |
| 42 | +can schedule them to run using the `command()` method. |
| 43 | + |
| 44 | +```php |
| 45 | +$schedule->command('demo:refresh --all'); |
| 46 | +``` |
| 47 | + |
| 48 | +The only argument is a string that calls the command, complete with an options or arguments. |
| 49 | + |
| 50 | +### Scheduling Shell Commands |
| 51 | + |
| 52 | +You can call out to the server and execute a command using the `shell()` method. |
| 53 | + |
| 54 | +```php |
| 55 | +$schedule->shell('cp foo bar')->daily()->at('11:00 pm'); |
| 56 | +``` |
| 57 | + |
| 58 | +Simply provide the command to call and any arguments, and it will be executed using PHP's `exec()` method. |
| 59 | + |
| 60 | +!!! note |
| 61 | + |
| 62 | + Many shared servers turn off exec access for security reasons. If you will be running |
| 63 | + on a shared server, double-check you can use the `exec` command before using this feature. |
| 64 | + |
| 65 | +### Scheduling Events |
| 66 | + |
| 67 | +If you want to trigger an [Event](https://codeigniter.com/user_guide/extending/events.html) you can |
| 68 | +use the `event()` method to do that for you, passing in the name of the event to trigger. |
| 69 | + |
| 70 | +```php |
| 71 | +$schedule->event('Foo')->hourly(); |
| 72 | +``` |
| 73 | + |
| 74 | +### Scheduling URL Calls |
| 75 | + |
| 76 | +If you need to ping a URL on a regular basis, you can use the `url()` method to perform a simple |
| 77 | +GET request using cURL to the URL you pass in. If you need more dynamism than can be provided in |
| 78 | +a simple URL string, you can use a closure or command instead. |
| 79 | + |
| 80 | +```php |
| 81 | +$schedule->url('https://my-status-cloud.com?site=foo.com')->everyFiveMinutes(); |
| 82 | +``` |
| 83 | + |
| 84 | +## Frequency Options |
| 85 | + |
| 86 | +There are a number of ways available to specify how often the task is called. |
| 87 | + |
| 88 | + |
| 89 | +| Method | Description | |
| 90 | +|:----------------------------------|:----------------------------------------------------------------------| |
| 91 | +| `->cron('* * * * *')` | Run on a custom cron schedule. | |
| 92 | +| `->daily('4:00 am')` | Runs daily at 12:00am, unless a time string is passed in. | |
| 93 | +| `->hourly() / ->hourly(15)` | Runs at the top of every hour or at specified minute. | |
| 94 | +| `->everyHour(3, 15)` | Runs every 3 hours at XX:15. | |
| 95 | +| `->betweenHours(6,12)` | Runs between hours 6 and 12. | |
| 96 | +| `->hours([0,10,16])` | Runs at hours 0, 10 and 16. | |
| 97 | +| `->everyMinute(20)` | Runs every 20 minutes. | |
| 98 | +| `->betweenMinutes(0,30)` | Runs between minutes 0 and 30. | |
| 99 | +| `->minutes([0,20,40])` | Runs at specific minutes 0,20 and 40. | |
| 100 | +| `->everyFiveMinutes()` | Runs every 5 minutes (12:00, 12:05, 12:10, etc) | |
| 101 | +| `->everyFifteenMinutes()` | Runs every 15 minutes (12:00, 12:15, etc) | |
| 102 | +| `->everyThirtyMinutes()` | Runs every 30 minutes (12:00, 12:30, etc) | |
| 103 | +| `->days([0,3])` | Runs only on Sunday and Wednesday ( 0 is Sunday , 6 is Saturday ) | |
| 104 | +| `->sundays('3:15am')` | Runs every Sunday at midnight, unless time passed in. | |
| 105 | +| `->mondays('3:15am')` | Runs every Monday at midnight, unless time passed in. | |
| 106 | +| `->tuesdays('3:15am')` | Runs every Tuesday at midnight, unless time passed in. | |
| 107 | +| `->wednesdays('3:15am')` | Runs every Wednesday at midnight, unless time passed in. | |
| 108 | +| `->thursdays('3:15am')` | Runs every Thursday at midnight, unless time passed in. | |
| 109 | +| `->fridays('3:15am')` | Runs every Friday at midnight, unless time passed in. | |
| 110 | +| `->saturdays('3:15am')` | Runs every Saturday at midnight, unless time passed in. | |
| 111 | +| `->monthly('12:21pm')` | Runs the first day of every month at 12:00am unless time passed in. | |
| 112 | +| `->daysOfMonth([1,15])` | Runs only on days 1 and 15. | |
| 113 | +| `->months([1,7])` | Runs only on January and July. | |
| 114 | +| `->quarterly('5:00am')` | Runs the first day of each quarter (Jan 1, Apr 1, July 1, Oct 1) | |
| 115 | +| `->yearly('12:34am')` | Runs the first day of the year. | |
| 116 | +| `->weekdays('1:23pm')` | Runs M-F at 12:00 am unless time passed in. | |
| 117 | +| `->weekends('2:34am')` | Runs Saturday and Sunday at 12:00 am unless time passed in. | |
| 118 | +| `->environments('local', 'prod')` | Restricts the task to run only in the specified environments | |
| 119 | + |
| 120 | + |
| 121 | +These methods can be combined to create even more nuanced timings: |
| 122 | + |
| 123 | +```php |
| 124 | +$schedule->command('foo') |
| 125 | + ->weekdays() |
| 126 | + ->hourly() |
| 127 | + ->environments('development'); |
| 128 | +``` |
| 129 | + |
| 130 | +This would run the task at the top of every hour, Monday - Friday, but only in development environments. |
| 131 | + |
| 132 | +## Naming Tasks |
| 133 | + |
| 134 | +You can name tasks so they can be easily referenced later, such as through the CLI with the `named()` method: |
| 135 | + |
| 136 | +```php |
| 137 | +$schedule->command('foo')->nightly()->named('foo-task'); |
| 138 | +``` |
0 commit comments