Grails Jesque
Jesque is an implementation of Resque in Java. It is fully-interoperable with the Ruby and Node.js (Coffee-Resque) implementations.
The grails jesque plugin uses jesque and the grails redis plugin as a dependency. While it uses jesque for the core functionality it makes it groovier to use in grails.
There is also a grails jesque-web plugin initially ported from the jesque-web spring-mvc app, which itself was based on the Sinatra web app resque-web in resque. Either UI will allow you to see what's in your queues and view and re-process failed messages.
A scheduler (a la Quartz) has been added to support scheduled injection of jobs. The syntax is very similar to the grails Quartz plugin.
How do I use it?
Add the jesque plugin to grails, it will automatically pull in jesque with it's dependencies, and the grails redis plugin.
grails install-plugin jesque
You must also have redis installed in your environment.
Example to enqueue
class BackgroundJob {
def someOtherService //auto-wiring supported
def perform( arg1, arg2 ) {
def domainObject = DomainClass.get(arg1) //GORM supported
domainObject.message = arg2
domainObject.save()
}
}
class SomeOtherClass {
def jesqueService
def doWorkAsync() {
jesqueService.enqueue( 'myQueueName', BackgroundJob.simpleName, 1, 'hi there')
}
}
Workers can be started manually by calling
jesqueService.startWorker( 'myQueueName', BackgroundJob.simpleName, BackgroundJob )
or automatically upon start-up with the following config
grails {
jesque {
workers {
someNameForYourWorkerPool {
workers = 3 //defaults to 1
queueNames = 'myQueueName' //or a list
jobTypes = [(BackgroundJob.simpleName):BackgroundJob]
}
}
}
}
The redis pool used is configured in the redis plugin:
grails {
redis {
host = localhost //default
prot = 6379 //default
}
}
Jobs
Jobs should be placed in grails-app/jobs similar to the Quartz plugin. However, to not clash with quartz, and to retain similarties with resque, the method to execute must be called perform.
You can run the script create-jesque-job to create a shell of a job for you automatically. The following will create a BackgroundJob in the grails-app/jobs folder.
grails create-jesque-job package.Background
class MyJob {
static queue = 'MyJesqueQueue'
static workerPool = 'MyWorkerPook'
def injectedService //auto-wired
static triggers = {
cron name: 'MyJob', cronExpression: '0 0 23 * * ? *'
}
def perform() {
log.info "Executing Job"
injectedService.doSomeWork()
}
}
Unit and integration tests will also automatically be created. If you have spock installed and listed in your application.properties it will create an integration specification instead of a grails integration test.
Roadmap
- Ability to execute methods on services without creating a job object
- Wrap above ability automatically with annotation and dynamically creating a method with the same name + "Async" suffix
- Create grails/groovy docs (gdoc?) to extensively document options
- Support job/config changes when running as `grails run-app
- Dynamic wake time of delayed jobs thread to reduce polling
Release Notes
- 0.2.0 - released 2011-10-17
- First publicly announced version
- 0.3.0 - released 2011-02-03
- First implementation of scheduler
- 0.4.0 - released 2012-6-6
- Gracefully shutdown threads
- Handle changes to scheduled jobs during development
- Upgrade to Jedis 2.1.0 (Note, this is binary incompatible with Jedis 2.0.0, Grails Jesque < 0.4.0 will not run with Jedis 2.1.0 and >= 0.4.0 must run with Jedis >= 2.1.0)
- Change artefact name from "Job" to "JesqueJob" to not clash with other grails plugins (e.g. quartz) that use an artefact name of "Job" (issue #14)
- 0.5.0 - released 2012-11-20
- Add delayed job queue implementation
- Ability to use grailsConfiguration in triggers closure
License
Copyright 2011 Michael Cameron
Licensed 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
http://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.