Disabling play.jobs.Job from running in test mode in play mode

Using the playback frame 1.2.4 with scala. I have few work tasks that look like

@OnApplicationStart class MyOtherJob extends Job { ... } @Every("30s") class MyJob extends Job { ... } 

These tasks are performed while the application is in test mode, so they ruin things. How can I disconnect them from work during testing?

I tried the following application configuration, did not 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 
+4
source share
3 answers

You can check whether Play is in test mode using the following syntax.

 play.Play.runingInTestMode() 

Note: spelling error is not accidental. This is the name of the method in the API.

Therefore, in your Tasks, you should be able to wrap the task around the IF statement using the above, and, therefore, prevent the test mode tasks.

+4
source

EDIT: My formatting is disabled. We’ll fix it a bit.

We have a small little shell that checks to see 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

etc.

def ifEnabled(property: String)(runnable: => Unit) = play.conf.configuration.getProperty(property + ".enabled", "false") match { case "true" => runnable case _ => Logger info "Ignoring " + property + " since it disabled!" }

Then in your work

class MyJob extends Job { ifEnabled("job.myJob") { // code goes here } }

Thus, you do not need to check each individual environment.

0
source
  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); } 

therefore you must define cron.myjob = 3min% test.cron.myjob = never and on ("cron.myjob")

Example:

 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 { 
0
source

Source: https://habr.com/ru/post/1387779/


All Articles