Reputation: 1137
Using play framework 1.2.4 with scala. I have few play jobs that looks like like
@OnApplicationStart class MyOtherJob extends Job { ... }
@Every("30s") class MyJob extends Job { ... }
These jobs are running while the application is in test mode, so they mess up things. How can I disable them from running while testing?
I tried the following application config, didn't help:
# Jobs executor
# ~~~~~~
# Size of the Jobs pool
play.jobs.pool=10
test.play.jobs.pool=0
test.cron.queue.every=never
dev.cron.queue.every=20s
prod.cron.queue.every=20s
test.cron.onApplicationStart.trigger=never
dev.cron.onApplicationStart.trigger=auto
prod.cron.onApplicationStart.trigger=auto
Upvotes: 5
Views: 1287
Reputation: 1
if (clazz.isAnnotationPresent(Every.class)) {
try {
Job job = (Job) clazz.newInstance();
scheduledJobs.add(job);
String value = job.getClass().getAnnotation(Every.class).value();
if (value.startsWith("cron.")) {
value = Play.configuration.getProperty(value);
}
value = Expression.evaluate(value, value).toString();
if(!"never".equalsIgnoreCase(value)){
executor.scheduleWithFixedDelay(job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS);
}
so you should define cron.myjob=3min %test.cron.myjob=never and on("cron.myjob")
ex:
cron.SyncWeixinInfo=never
%prod.cron.SyncWeixinInfo=0 0 0 1 * ?
%test.cron.SyncWeixinInfo=0 0 0 1 * ?
%localtest.cron.SyncWeixinInfo=0 0 0 1 * ?
%prodSlave.cron.SyncWeixinInfo=never
@On("cron.SyncWeixinInfo")//每月1号凌晨0点
public class SyncWeixinInfo extends Job {
Upvotes: 0
Reputation: 287
EDIT: Ugh, my formatting is off. Will fix in a bit.
We have a nice little wrapper that checks if a job is enabled in a specific environment.
Example entry in application.conf
job.myjob.enabled=true
%test.job.myjob.enabled=false
%prod.job.myjob.enabled=true
and so on.
def ifEnabled(property: String)(runnable: => Unit) =
play.conf.configuration.getProperty(property + ".enabled", "false") match {
case "true" => runnable
case _ => Logger info "Ignoring " + property + " since it's disabled!"
}
Then in your job
class MyJob extends Job {
ifEnabled("job.myJob") {
// code goes here
}
}
This way you don't have to check each individual environment.
Upvotes: 0
Reputation: 54924
It is possible to check if Play is running in test mode using the following syntax.
play.Play.runingInTestMode()
Note: the spelling mistake is not accidental. That is the method name in the API.
Therefore, in your Jobs, you should be able to wrap the job execution around an IF statement using the above, and therefore, preventing test mode jobs.
Upvotes: 4