@Nullable @Override public JobRunnerResponse runJob(final JobRunnerRequest request) { final Map<FileType, ReplicationResult> result = performReplication(); if (result == null) { return JobRunnerResponse.aborted("Full replication is already in progress"); } return JobRunnerResponse.success(result.toString()); } }
/** * Creates a response that indicates the request has failed. * * @param message the message to return, which will be truncated to {@link com.atlassian.scheduler.status.RunDetails#MAXIMUM_MESSAGE_LENGTH} if * necessary. The message is <strong>required</strong> when reporting that the job has failed. * @return the response */ public static JobRunnerResponse failed(final String message) { checkArgument(isNotBlank(message), "The message must be specified when reporting a job as failed!"); return new JobRunnerResponse(FAILED, message); }
/** * Creates a response that indicates the request has failed. The {@link #getMessage() message} is set to * to an abbreviated representation of the exception and its causes, but the * {@link com.atlassian.scheduler.status.RunDetails#MAXIMUM_MESSAGE_LENGTH} still applies, so this information may be incomplete. * When possible, the {@link JobRunner} is encouraged to trap its exceptions and report more specific * diagnostic messages with {@link #failed(String)}, instead. * * @param cause the exception that caused this failure * @return the response */ public static JobRunnerResponse failed(final Throwable cause) { return new JobRunnerResponse(FAILED, toMessage(requireNonNull(cause, "cause"))); }
@Override public JobRunnerResponse runJob(@Nonnull JobRunnerRequest request) { if (!CLEANUP_JOB_ID.equals(request.getJobId())) { return JobRunnerResponse.aborted("Unknown job ID: " + request.getJobId()); } try { run(); } catch(Exception e) { return JobRunnerResponse.failed(e); } return JobRunnerResponse.success(); }
/** * Creates a successful response with no additional message. * * @return the response */ public static JobRunnerResponse success() { return success(null); }
private static JobRunnerResponse runServiceId(final Long serviceId) { JobRunnerResponse response; final JiraServiceContainer service; try { final ServiceManager serviceManager = ComponentAccessor.getComponent(ServiceManager.class); service = serviceManager.getServiceWithId(serviceId); if (service == null) { response = JobRunnerResponse.aborted("Service ID '" + serviceId + "' no longer exists!"); } else { response = runService(service); } } catch (Exception e) { LOG.error("An error occurred while trying to run service with ID '" + serviceId + "'. " + e.getMessage(), e); response = JobRunnerResponse.failed(e); } return response; }
@Nonnull private JobRunnerResponse launchAndBuildResponse() throws JobRunnerNotRegisteredException { try { response = validate(); if (response == null) { response = runJob(); } } catch (RuntimeException ex) { LOG.error("Scheduled job with ID '{}' failed", jobId, ex); response = failed(ex); } catch (LinkageError err) { LOG.error("Scheduled job with ID '{}' failed due to binary incompatibilities", jobId, err); response = failed(err); } return response; }
@Nullable private JobRunnerResponse validateJobDetails() { if (jobDetails == null) { jobDetails = schedulerService.getJobDetails(jobId); if (jobDetails == null) { return aborted("No corresponding job details"); } } if (jobDetails.getRunMode() != schedulerRunMode) { return aborted("Inconsistent run mode: expected '" + jobDetails.getRunMode() + "' got: '" + schedulerRunMode + '\''); } return null; }
/** * Creates a successful response with the specified message. * * @param message the message to return, which is optional and will be truncated to * {@link com.atlassian.scheduler.status.RunDetails#MAXIMUM_MESSAGE_LENGTH} if necessary * @return the response */ public static JobRunnerResponse success(@Nullable final String message) { return new JobRunnerResponse(SUCCESS, message); }
@Nullable @Override public JobRunnerResponse runJob(JobRunnerRequest request) { if (!CLEANUP_JOB_ID.equals(request.getJobId())) { return JobRunnerResponse.aborted("Unknown job ID: " + request.getJobId()); } try { run(); } catch(Exception e) { return JobRunnerResponse.failed(e); } return JobRunnerResponse.success(); }
@Override public JobRunnerResponse runJob(@Nonnull JobRunnerRequest jobRunnerRequest) { truncateAlerts(); return JobRunnerResponse.success(); } }
@Nullable @Override public JobRunnerResponse runJob(final JobRunnerRequest jobRunnerRequest) { // Get the job class to actually run final Map<String,Serializable> parameters = jobRunnerRequest.getJobConfig().getParameters(); final Long serviceId = (Long) parameters.get(ServiceManager.SERVICE_ID_KEY); if (serviceId == null) { return JobRunnerResponse.failed("Service entry with job ID '" + jobRunnerRequest.getJobId() + "' has no job config parameters"); } return runServiceId(serviceId); }
@Nullable private JobRunnerResponse validateJobConfig() { try { jobConfig = JobConfig.forJobRunnerKey(jobDetails.getJobRunnerKey()) .withRunMode(jobDetails.getRunMode()) .withSchedule(jobDetails.getSchedule()) .withParameters(jobDetails.getParameters()); return null; } catch (SchedulerRuntimeException sre) { return aborted(jobDetails.toString()); } }
@Nullable @Override public JobRunnerResponse runJob(final JobRunnerRequest jobRunnerRequest) { try { final Long removeThreshold = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(90); userHistoryStore.removeHistoryOlderThan(removeThreshold); } catch (Exception ex) { LOG.warn("Exception occurred when running " + JOB_RUNNER_KEY + " job.", ex); return JobRunnerResponse.failed(ex); } return JobRunnerResponse.success(); }
@Override public JobRunnerResponse runJob(JobRunnerRequest request) { // set the context class loader to the current bundle ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); try { TaskMonitor<Void> monitor = start(); monitor.get(); return JobRunnerResponse.success(); } catch (InterruptedException e) { return JobRunnerResponse.aborted(e.getMessage()); } catch (ExecutionException e) { return JobRunnerResponse.failed(e.getCause()); } finally { // restore the original classloader Thread.currentThread().setContextClassLoader(ctxClassLoader); } }
@Nullable @Override public JobRunnerResponse runJob(JobRunnerRequest request) { writeFile(); return JobRunnerResponse.success(); }
@Nonnull private JobRunnerResponse runJob() { final RunningJob job = new RunningJobImpl(firedAt, jobId, jobConfig); final RunningJob existing = schedulerService.enterJob(jobId, job); if (existing != null) { LOG.debug("Unable to start job {} because it is already running as {}", job, existing); return JobRunnerResponse.aborted("Already running"); } schedulerService.preJob(); final Thread thd = Thread.currentThread(); final ClassLoader originalClassLoader = thd.getContextClassLoader(); try { // SCHEDULER-11: Ensure that the Job runs with its own class loader set as the thread's CCL thd.setContextClassLoader(jobRunner.getClass().getClassLoader()); final JobRunnerResponse response = jobRunner.runJob(job); return (response != null) ? response : JobRunnerResponse.success(); } finally { thd.setContextClassLoader(originalClassLoader); schedulerService.leaveJob(jobId, job); schedulerService.postJob(); } }
/** * Creates a response that indicates the request was aborted. In most cases, it will make * more sense to report the job as either having {@link #success() succeeded} with nothing to * do or {@link #failed(String) failed}, instead. * * @param message the message to return, which will be truncated to {@link com.atlassian.scheduler.status.RunDetails#MAXIMUM_MESSAGE_LENGTH} if * necessary. The message is <strong>required</strong> when reporting that the job was aborted. * @return the response */ public static JobRunnerResponse aborted(final String message) { checkArgument(isNotBlank(message), "The message must be specified when reporting a job as aborted!"); return new JobRunnerResponse(ABORTED, message); }
@Nullable @Override public JobRunnerResponse runJob(final JobRunnerRequest jobRunnerRequest) { try { final List<CacheStatisticsAnalyticEvent> cacheStatistics = getCacheStatistics(); sendToAnalytics(cacheStatistics); } catch (final Exception ex) { LOG.warn("Exception occurred when running " + JOB_RUNNER_KEY + " job.", ex); return JobRunnerResponse.failed(ex); } return JobRunnerResponse.success(); }