/** * Looks for a job in the running buffer * * @param identifier The identifier of a job * @return The job, or null if it is not found */ private Job getJobRunning(String identifier) { synchronized (running) { for (int i = 0; i != running.length; i++) { if (running[i] != null && running[i].getIdentifier().equals(identifier)) return running[i]; } return null; } }
/** * Action when a job is going to run * * @param job The job */ private void onJobRun(Job job) { // register as running synchronized (running) { for (int i = 0; i != running.length; i++) { if (running[i] == null) { running[i] = job; break; } } } // event before running job.onRun(); }
if (i != 0) builder.append(", "); builder.append(scheduled.get(i).serializedJSON()); if (i != 0) builder.append(", "); builder.append(running.get(i).serializedJSON()); if (i != 0) builder.append(", "); builder.append(completed.get(i).serializedJSON());
@Override public void schedule(Job job) { ThreadPoolExecutor pool = getExecutorPool(); boolean exists = storage.exists(); if (!exists) { exists = storage.mkdirs(); } if (exists) { try (Writer write = Files.getWriter(new File(storage, getFileName(job)).getAbsolutePath())) { write.write(job.serializedJSON()); } catch (IOException exception) { Logger.DEFAULT.error(exception); } } else { Logger.DEFAULT.error("Cannot serialize the job, storage is inaccessible"); } job.onScheduled(); pool.execute(job); }
@Override public XSPReply cancel(Job job) { SecurityService securityService = Register.getComponent(SecurityService.class); if (securityService == null) return XSPReplyServiceUnavailable.instance(); XSPReply reply = securityService.checkAction(ACTION_CANCEL, job); if (!reply.isSuccess()) return reply; boolean success = executorPool.remove(job); if (success) { // the job was queued and prevented from running job.onTerminated(true); return XSPReplySuccess.instance(); } switch (job.getStatus()) { case Unscheduled: case Scheduled: job.onTerminated(true); return XSPReplySuccess.instance(); case Running: return job.cancel(); case Completed: return new XSPReplyApiError(ERROR_ALREADY_COMPLETED); case Cancelled: return new XSPReplyApiError(ERROR_ALREADY_CANCELLED); default: return new XSPReplyApiError(ERROR_INVALID_JOB_STATE, job.getStatus().toString()); } }
job.onTerminated(job.getStatus() == JobStatus.Cancelled); Logging.get().info(new RichString("Ended job ", job));
builder.append(", "); first = false; builder.append(job.serializedJSON()); builder.append(", "); first = false; builder.append(job.serializedJSON()); builder.append(", "); first = false; builder.append(job.serializedJSON());
@Override public XSPReply schedule(Job job) { boolean exists = storage.exists(); if (!exists) { exists = storage.mkdirs(); } if (exists) { try (Writer write = IOUtils.getWriter(new File(storage, getFileName(job)).getAbsolutePath())) { write.write(job.serializedJSON()); } catch (IOException exception) { Logging.get().error(exception); } } else { Logging.get().error("Cannot serialize the job, storage is inaccessible"); } job.onScheduled(); synchronized (initQueue) { if (canExecute.get()) { executorPool.execute(job); } else { initQueue.add(job); } } Logging.get().info(new RichString("Scheduled job ", job)); return new XSPReplyResult<>(job); }
@Override public HttpResponse onMessage(String method, String uri, Map<String, String[]> parameters, String contentType, byte[] content, String accept) { String[] ids = parameters.get("id"); if (ids != null && ids.length > 0) { Job job = getJob(ids[0], JobStatus.Completed); if (job == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); } return onRequestJobs(); }
/** * Looks for a job in the running buffer * * @param identifier The identifier of a job * @return The job, or null if it is not found */ private Job getJobRunning(String identifier) { synchronized (running) { for (int i = 0; i != running.length; i++) { if (running[i] != null && running[i].getIdentifier().equals(identifier)) return running[i]; } return null; } }
/** * Action when a job is going to run * * @param job The job */ private void onJobRun(Job job) { // register as running synchronized (running) { for (int i = 0; i != running.length; i++) { if (running[i] == null) { running[i] = job; break; } } } // event before running job.onRun(); Logging.get().info(new RichString("Begin running job ", job)); }
if (job == null) return new HttpResponse(HttpURLConnection.HTTP_NOT_FOUND); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); } else if (rest.substring(index).equals("/cancel")) { if (!HttpConstants.METHOD_POST.equals(request.getMethod()))
/** * Looks for a job in the queue * * @param identifier The identifier of a job * @return The job, or null if it is not found */ private Job getJobScheduled(String identifier) { for (Runnable runnable : executorPool.getQueue()) { Job job = (Job) runnable; if (job.getIdentifier().equals(identifier)) return job; } return null; }
/** * Responds to the request to pull an artifact from the live store * When successful, this action creates the appropriate job and returns it. * * @param parameters The request parameters * @return The response */ private HttpResponse onMessagePullFromLive(Map<String, String[]> parameters) { String[] ids = parameters.get("id"); if (ids == null || ids.length == 0) return new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, HttpConstants.MIME_TEXT_PLAIN, "Expected an id parameter"); JobExecutionService executor = ServiceUtils.getService(JobExecutionService.class); if (executor == null) return XSPReplyUtils.toHttpResponse(XSPReplyServiceUnavailable.instance(), null); Job job = new PullArtifactFromLiveJob(ids[0]); executor.schedule(job); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); }
/** * Looks for a job in the completed buffer * * @param identifier The identifier of a job * @return The job, or null if it is not found */ private Job getJobCompleted(String identifier) { synchronized (completed) { if (completedStart < 0) return null; for (int i = 0; i != completed.length; i++) { if (completed[i] == null) return null; if (completed[i].getIdentifier().equals(identifier)) return completed[i]; } } return null; }
/** * Responds to a request to delete an artifact * * @param parameters The request parameters * @return The response */ private HttpResponse onMessageDeleteArtifact(Map<String, String[]> parameters) { String[] ids = parameters.get("id"); if (ids == null || ids.length == 0) return new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, HttpConstants.MIME_TEXT_PLAIN, "Expected an id parameter"); JobExecutionService executor = ServiceUtils.getService(JobExecutionService.class); if (executor == null) return XSPReplyUtils.toHttpResponse(XSPReplyServiceUnavailable.instance(), null); Job job = new DeleteArtifactJob(ids[0]); executor.schedule(job); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); }
/** * Looks for a job in the completed buffer * * @param identifier The identifier of a job * @return The job, or null if it is not found */ private Job getJobCompleted(String identifier) { synchronized (completed) { if (completedStart < 0) return null; for (int i = 0; i != completed.length; i++) { if (completed[i] == null) return null; if (completed[i].getIdentifier().equals(identifier)) return completed[i]; } } return null; }
/** * Responds to the request to push an artifact to the live store * When successful, this action creates the appropriate job and returns it. * * @param parameters The request parameters * @return The response */ private HttpResponse onMessagePushToLive(Map<String, String[]> parameters) { String[] ids = parameters.get("id"); if (ids == null || ids.length == 0) return new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, HttpConstants.MIME_TEXT_PLAIN, "Expected an id parameter"); JobExecutionService executor = ServiceUtils.getService(JobExecutionService.class); if (executor == null) return XSPReplyUtils.toHttpResponse(XSPReplyServiceUnavailable.instance(), null); Job job = new PushArtifactToLiveJob(ids[0]); executor.schedule(job); return new HttpResponse(HttpURLConnection.HTTP_OK, HttpConstants.MIME_JSON, job.serializedJSON()); } }
/** * Gets the file name for a job * * @param job The job * @return The file name */ private static String getFileName(Job job) { return "job-" + SHA1.hashSHA1(job.getIdentifier()) + ".json"; }