Created
November 11, 2008 15:46
-
-
Save dudleyf/23883 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BeancounterJob.java | |
package beancounter.jobs; | |
import org.jruby.Ruby; | |
import org.quartz.Job; | |
import org.quartz.JobDataMap; | |
import org.quartz.JobExecutionContext; | |
import org.quartz.JobExecutionException; | |
public class BeancounterJob implements Job { | |
public static final String RUNTIME_KEY = "beancounter.ruby.runtime"; | |
public static final String SCRIPT_KEY = "beancounter.ruby.job.script"; | |
public void execute(JobExecutionContext jobContext) | |
throws JobExecutionException { | |
JobDataMap dataMap = jobContext.getJobDetail().getJobDataMap(); | |
String script = dataMap.getString(SCRIPT_KEY); | |
Ruby runtime = (Ruby)dataMap.get(RUNTIME_KEY); | |
if (runtime == null) { | |
throw new JobExecutionException( | |
"There was no Ruby runtime in the job's data map under the key " + RUNTIME_KEY); | |
} | |
if (script == null || script.equals("")) { | |
throw new JobExecutionException( | |
"There was no Ruby script in the job's data map under the key " + SCRIPT_KEY); | |
} | |
runtime.evalScriptlet(script); | |
} | |
} | |
----------------------------------------------- | |
# scheduler.rb | |
begin | |
require 'quartz-1.6.0' | |
rescue LoadError | |
# Let's hope the jar has already been loaded | |
end | |
module Beancounter | |
def schedule_job(name, opts={}) | |
Beancounter::Scheduler.schedule_job(name, opts) | |
end | |
module_function :schedule_job | |
class Scheduler | |
include_class "org.quartz.JobDetail" | |
include_class "org.quartz.CronTrigger" | |
include_class "beancounter.jobs.BeancounterJob" | |
FACTORY_KEY = org.quartz.ee.servlet.QuartzInitializerListener::QUARTZ_FACTORY_KEY | |
JOB_GROUP = 'BeancounterJobsGroup' | |
class << self | |
def schedule_job(job_name, opts={}) | |
if scheduler | |
Merb.logger.info "Scheduling #{job_name}" | |
job_detail = make_job_detail(job_name, opts[:script]) | |
trigger = make_trigger(opts[:cron], job_detail.getName) | |
scheduler.scheduleJob(job_detail, trigger) | |
else | |
Merb.logger.info "There's no scheduler available, so we can't schedule any jobs." | |
end | |
end | |
def factory | |
@factory ||= | |
defined?($servlet_context) ? $servlet_context.getAttribute(FACTORY_KEY) : nil | |
end | |
def scheduler | |
@scheduler ||= factory.nil? ? nil : factory.getScheduler | |
end | |
def make_trigger(cron_expr, job_name, group=JOB_GROUP) | |
name = "#{job_name}Trigger" | |
Merb.logger.info "Creating a new CronTrigger named #{name} with cron expression '#{cron_expr}'" | |
CronTrigger.new(name, group, job_name, group, cron_expr) | |
end | |
def make_job_detail(name, script) | |
Merb.logger.info "Creating a new JobDetail named #{name} that will run this code: '#{script}'" | |
detail = JobDetail.new(name, JOB_GROUP, BeancounterJob.java_class) | |
detail.getJobDataMap.put(BeancounterJob::RUNTIME_KEY, JRuby.runtime) | |
detail.getJobDataMap.put(BeancounterJob::SCRIPT_KEY, script) | |
detail | |
end | |
end | |
end | |
end | |
----------------------------------------------- | |
Then, in my init file, I use something like: | |
Beancounter.schedule_job 'LedgerFileImport', | |
:script => 'Beancounter::LedgerFileImporter.new.download_and_import', | |
:cron => '0 0 0 * * ?' | |
to schedule a job. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment