protected <T> String getNextRunTime(ScheduledTask<T> task) { String nextRunTime = "n/a"; // Run now type tasks should never have a next run time if (!task.getSchedule().getClass().isAssignableFrom(RunNowSchedule.class) && task.getNextRun() != null) { nextRunTime = task.getNextRun().toString(); } return nextRunTime; }
/** Add a new scheduled service to nexus. */ @Override @POST @ResourceMethodSignature( input = ScheduledServiceResourceResponse.class, output = ScheduledServiceResourceStatusResponse.class) public Object post(Context context, Request request, Response response, Object payload) throws ResourceException { ScheduledServiceResourceResponse serviceRequest = (ScheduledServiceResourceResponse) payload; ScheduledServiceResourceStatusResponse result = null; if (serviceRequest != null) { ScheduledServiceBaseResource serviceResource = serviceRequest.getData(); try { Schedule schedule = getModelSchedule(serviceRequest.getData()); ScheduledTask<?> task = null; if (schedule != null) { task = getNexusScheduler() .schedule( getModelName(serviceResource), getModelNexusTask(serviceResource, request), schedule); } else { task = getNexusScheduler() .schedule( getModelName(serviceResource), getModelNexusTask(serviceResource, request), new ManualRunSchedule()); } task.setEnabled(serviceResource.isEnabled()); // Need to store the enabled flag update getNexusScheduler().updateSchedule(task); ScheduledServiceResourceStatus resourceStatus = new ScheduledServiceResourceStatus(); resourceStatus.setResource(serviceResource); // Just need to update the id, as the incoming data is a POST w/ no id resourceStatus.getResource().setId(task.getId()); resourceStatus.setResourceURI(createChildReference(request, this, task.getId()).toString()); resourceStatus.setStatus(task.getTaskState().toString()); resourceStatus.setReadableStatus(getReadableState(task.getTaskState())); resourceStatus.setCreated( task.getScheduledAt() == null ? "n/a" : task.getScheduledAt().toString()); resourceStatus.setLastRunResult( TaskState.BROKEN.equals(task.getTaskState()) ? "Error" : "Ok"); resourceStatus.setLastRunTime( task.getLastRun() == null ? "n/a" : task.getLastRun().toString()); resourceStatus.setNextRunTime( task.getNextRun() == null ? "n/a" : task.getNextRun().toString()); if (task.getScheduledAt() != null) { resourceStatus.setCreatedInMillis(task.getScheduledAt().getTime()); } if (task.getLastRun() != null) { resourceStatus.setLastRunTimeInMillis(task.getLastRun().getTime()); } if (task.getNextRun() != null) { resourceStatus.setNextRunTimeInMillis(task.getNextRun().getTime()); } result = new ScheduledServiceResourceStatusResponse(); result.setData(resourceStatus); } catch (RejectedExecutionException e) { getLogger().warn("Execution of task " + getModelName(serviceResource) + " rejected."); throw new ResourceException(Status.CLIENT_ERROR_CONFLICT, e.getMessage()); } catch (ParseException e) { getLogger().warn("Unable to parse data for task " + getModelName(serviceResource)); throw new PlexusResourceException( Status.CLIENT_ERROR_BAD_REQUEST, e.getMessage(), getNexusErrorResponse("cronCommand", e.getMessage())); } catch (InvalidConfigurationException e) { handleConfigurationException(e); } } return result; }