@Override public void onApplicationStop() { List<Class> jobs = Play.classloader.getAssignableClasses(Job.class); for (final Class clazz : jobs) { // @OnApplicationStop if (clazz.isAnnotationPresent(OnApplicationStop.class)) { try { Job<?> job = ((Job<?>) clazz.newInstance()); scheduledJobs.add(job); job.run(); if (job.wasError) { if (job.lastException != null) { throw job.lastException; } throw new RuntimeException("@OnApplicationStop Job has failed"); } } catch (InstantiationException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (IllegalAccessException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (Throwable ex) { if (ex instanceof PlayException) { throw (PlayException) ex; } throw new UnexpectedException(ex); } } } executor.shutdownNow(); executor.getQueue().clear(); }
protected boolean isGraphBuilt(final boolean allowBackgroundContinuation) throws InterruptedException { if (!isGraphBuilt()) { s_logger.info("Building dependency graph"); do { final Job job = createConstructionJob(); _activeJobCount.incrementAndGet(); synchronized (_activeJobs) { if (!_cancelled) { _activeJobs.add(job); } else { throw new CancellationException(); } } job.run(); synchronized (_activeJobs) { if (!_runQueue.isEmpty()) { // more jobs in the queue so keep going continue; } } if (allowBackgroundContinuation) { // Nothing in the queue for us so take a nap. There are background threads running and // maybe items on the deferred queue. s_logger.info("Waiting for background threads"); Thread.sleep(100); } else { return false; } } while (!isGraphBuilt()); } return true; }
public void readline() { term.readline( "% ", line -> { if (line == null) { // EOF term.close(); return; } List<CliToken> tokens = CliToken.tokenize(line); if (tokens.stream().filter(CliToken::isText).count() == 0) { // For now do like this ShellImpl.this.readline(); return; } Optional<CliToken> first = tokens.stream().filter(CliToken::isText).findFirst(); if (first.isPresent()) { String name = first.get().value(); switch (name) { case "exit": case "logout": term.close(); return; case "jobs": jobController .jobs() .forEach( job -> { String statusLine = statusLine(job, job.status()) + "\n"; term.write(statusLine); }); readline(); return; case "fg": { Job job = findJob(); if (job == null) { term.write("no such job\n"); readline(); } else { if (job.status() == ExecStatus.STOPPED) { job.resume(true); } else { job.toForeground(); } } return; } case "bg": { Job job = findJob(); if (job == null) { term.write("no such job\n"); readline(); } else { if (job.status() == ExecStatus.STOPPED) { job.resume(false); term.echo(statusLine(job, ExecStatus.RUNNING) + "\n"); readline(); } else { term.write("job " + job.id() + " already in background\n"); readline(); } } return; } } } Job job; try { job = createJob(tokens); } catch (Exception e) { term.echo(e.getMessage() + "\n"); readline(); return; } job.setTty(term); job.setSession(session); job.statusUpdateHandler( status -> { if (status == ExecStatus.RUNNING && job == jobController.foregroundJob()) { term.echo(job.line() + "\n"); } }); job.run(); }, completion -> { commandManager.complete(completion); }); }
@Override public void afterApplicationStart() { List<Class<?>> jobs = new ArrayList<Class<?>>(); for (Class clazz : Play.classloader.getAllClasses()) { if (Job.class.isAssignableFrom(clazz)) { jobs.add(clazz); } } scheduledJobs = new ArrayList<Job>(); for (final Class<?> clazz : jobs) { // @OnApplicationStart if (clazz.isAnnotationPresent(OnApplicationStart.class)) { // check if we're going to run the job sync or async OnApplicationStart appStartAnnotation = clazz.getAnnotation(OnApplicationStart.class); if (!appStartAnnotation.async()) { // run job sync try { Job<?> job = ((Job<?>) clazz.newInstance()); scheduledJobs.add(job); job.run(); if (job.wasError) { if (job.lastException != null) { throw job.lastException; } throw new RuntimeException("@OnApplicationStart Job has failed"); } } catch (InstantiationException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (IllegalAccessException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (Throwable ex) { if (ex instanceof PlayException) { throw (PlayException) ex; } throw new UnexpectedException(ex); } } else { // run job async try { Job<?> job = ((Job<?>) clazz.newInstance()); scheduledJobs.add(job); // start running job now in the background @SuppressWarnings("unchecked") Callable<Job> callable = (Callable<Job>) job; executor.submit(callable); } catch (InstantiationException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } catch (IllegalAccessException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } } } // @On if (clazz.isAnnotationPresent(On.class)) { try { Job<?> job = ((Job<?>) clazz.newInstance()); scheduledJobs.add(job); scheduleForCRON(job); } catch (InstantiationException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } catch (IllegalAccessException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } } // @Every 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); } } catch (InstantiationException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } catch (IllegalAccessException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } } } }
@Override public void afterApplicationStart() { List<Class> jobs = new ArrayList(); for (Class clazz : Play.classloader.getAllClasses()) { if (Job.class.isAssignableFrom(clazz)) { jobs.add(clazz); } } scheduledJobs = new ArrayList(); for (final Class clazz : jobs) { // @OnApplicationStart if (clazz.isAnnotationPresent(OnApplicationStart.class)) { try { Job job = ((Job) clazz.newInstance()); scheduledJobs.add(job); job.run(); if (job.wasError) { if (job.lastException != null) { throw job.lastException; } throw new RuntimeException("@OnApplicationStart Job has failed"); } } catch (InstantiationException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (IllegalAccessException e) { throw new UnexpectedException("Job could not be instantiated", e); } catch (Throwable ex) { if (ex instanceof PlayException) { throw (PlayException) ex; } throw new UnexpectedException(ex); } } // @On if (clazz.isAnnotationPresent(On.class)) { try { Job job = ((Job) clazz.newInstance()); scheduledJobs.add(job); scheduleForCRON(job); } catch (InstantiationException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } catch (IllegalAccessException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } } // @Every if (clazz.isAnnotationPresent(Every.class)) { try { Job job = (Job) clazz.newInstance(); scheduledJobs.add(job); String value = ((Every) (job.getClass().getAnnotation(Every.class))).value(); executor.scheduleWithFixedDelay( job, Time.parseDuration(value), Time.parseDuration(value), TimeUnit.SECONDS); } catch (InstantiationException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } catch (IllegalAccessException ex) { throw new UnexpectedException("Cannot instanciate Job " + clazz.getName()); } } } }