blob: aab2a05d63334dd8ab00ef48cd191278ee18a6f6 [file]
////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
////
[[scheduling]]
== Scheduling Basics
=== Scheduling Jobs
To create a new job, run the `grails create-job` command and enter the name of the job. Grails will create a new job and place it in the `grails-app/jobs` directory:
[source,groovy]
----
class MyJob {
static group = 'MyGroup'
static description = 'Example job with Simple Trigger'
static triggers = {
simple(
name: 'mySimpleTrigger',
startDelay: 60000,
repeatInterval: 1000
)
}
void execute() {
println 'Job run!'
}
}
----
The above example will wait for 1 minute and after that will call the `execute()` method every second. The `repeatInterval` and `startDelay` properties are specified in milliseconds and must have Integer or Long type. If these properties are not specified, default values are applied (1 minute for `repeatInterval` property and 30 seconds for `startDelay` property). Jobs can optionally be placed in different groups.
The trigger name property must be unique across all triggers in the application.
By default, jobs will not be executed when running under the test environment.
==== Scheduling a Cron Job
Jobs can be scheduled using a cron expression. For those unfamiliar with `cron`, this means being able to create a firing schedule such as "At 8:00am every Monday through Friday" or "At 1:30am every last Friday of the month". (See the API docs for the `CronTrigger` class in Quartz for more info on cron expressions).
[source,groovy]
----
class MyJob {
static group = 'MyGroup'
static description = 'Example job with Cron Trigger'
static triggers = {
cron(
name: 'myTrigger',
cronExpression: '0 0 6 * * ?'
)
}
void execute() {
println 'Job run!'
}
}
----
The fields in the cronExpression are: (summarizing the Quartz CronTrigger Tutorial)
[,]
----
cronExpression: "s m h D M W Y"
| | | | | | - Year <<optional>>
| | | | | - Day of Week, 1-7 or SUN-SAT, ?
| | | | - Month, 1-12 or JAN-DEC
| | | - Day of Month, 1-31, ?
| | - Hour, 0-23
| - Minute, 0-59
- Second, 0-59
----
[NOTE]
====
* Year is the only optional field and may be omitted, the rest are mandatory.
* Day-of-Week and Month are case-insensitive, so "DEC" = "dec" = "Dec"
* Either Day-of-Week or Day-of-Month must be "?", or you will get an error since support by the underlying library is not complete. So you can't specify both fields, nor leave both as the all-values wildcard "*"; this is a departure from the unix crontab specification.
* See the CronTrigger Tutorial for an explanation of all the special characters you may use.
====