Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
@tbarker9 tbarker9
122 lines (89 sloc) 4.962 kb

grails-job-runner-plugin

Build Status

Intended to be a simple replacement for the batch-runner plugin that can run any service which has an 'execute' method. This plugin was built to deal with my frustrations over running command line jobs in grails. The batch plugin used to do this, but no longer works in Grails 2.2. While running a service, all features in grails will be used (transactions, injection, etc.)

Download

Use the standard dependency management in grails

  plugins {
    runtime ":job-runner:<version>"
  }

Or clone the repository and run grails maven-install and make sure any application or plugin referencing it has a repository reference to mavenLocal()

Basic Usage

This plugins allows you to run any bean that is accessible in the ApplicationContext which has an execute() method.
To use it, simply give the name of stored bean in the ApplicationContext. Assume you have the service foo:

class FooService {

  def usage = "foo usage"

  def execute() {
    log.error "hello from foo"
  }
}

simply call grails run-job foo, and you will get the following output:

$ grails run-job foo
| Error 2013-05-20 21:34:10,980 [main] ERROR FooService  - hello from foo

(Note: we are logging at the error level to make sure the log message is not ignored)

The plugin also detects if the application is running locally, and will use that ApplicationContext instead of bootstrapping a headless application. You can run the application locally by running run-app or run-headless-app. As expected run-headless-app is a headless version of run-app that doesn't require a servlet container. To shutdown the headless application just run stop-headless-app.

Each job can have a usage field. The contents of this field will be printed instead of running the job when using the flag -u or -usage. For instance run-job foo -u.

You can list jobs by running list-jobs. This will print the bean name of all available jobs. You do not need Service at the end of the job name, the script will be smart enough to pick the right job. So if you have the service FooService, you just need to run run-job foo to run that job.

Command Line Arguments

Command line arguments are injected into the field values of the service. Assuming you have the service

class FooService {

  boolean bar
  String foo
  static scope = "prototype"

  def execute() {
    log.error "bar is $bar"
    log.error "foo is $foo"
  }
}

And run the command grails run-job foo -bar=true -foo=foobar, you will get output similar to

$ grails run-job foo -bar=true -foo=foobar
| Error 2013-05-20 21:34:10,980 [main] ERROR FooService  - bar is true
| Error 2013-05-20 21:34:10,980 [main] ERROR FooService  - foo is foobar

If you are running jobs after running grails run-app, services should use the scope prototype to avoid changing the state of a singleton. If you still want to react to command line arguments without using injection, you can write the method execute(Map args). In this case, arguments will be passed to the execute method instead of performing injection. If you want to deal directly with the command line arguments while still using injection, add a field named argsMap. All keys in argsMap will correspond to properties in the command line with the exception of params.
params contains all non flag values.

Config Override Command Line Arguments

Works as of 0.6

  • config - use this flag to provide a location to a file to override the provided config, for example grails run-job foo -config=/some/path.groovy
  • config.<property name> - any property that starts with config. will override a property with the same name in the application config, for example grails run-job foo -config.dataSource.url=jdbc:h2:mem:altDb

The plugin is loaded before the dataSource plugin to allow overriding DataSources.groovy values. All config properties (ie start with config.) will override a provided config file.

Other Handy Scripts

list-jobs - lists all available jobs with their full name, including their shortest alias

create-job-scripts - finds all available jobs and creates scripts for them in the scripts directory. After generating the job scripts, they can be used instead of run-job <job name>

create-bash-job-script <script name> - creates a bash script in bin to delegate all work to grailsw. The wrapper must be installed for this to work

License

This plugin uses the ECL2 license. This is essentially a slightly modified apache 2 license.

Jump to Line
Something went wrong with that request. Please try again.