Search in sources :

Example 1 with DefaultRequest

use of org.xwiki.job.DefaultRequest in project xwiki-platform by xwiki.

the class ModelFactory method toJobRequest.

public DefaultRequest toJobRequest(JobRequest restJobRequest) {
    DefaultRequest request = new DefaultRequest();
    if (restJobRequest.getId() != null) {
        request.setId(restJobRequest.getId().getElements());
    }
    request.setInteractive(restJobRequest.isInteractive());
    request.setVerbose(restJobRequest.isVerbose());
    request.setStatusSerialized(restJobRequest.isStatusSerialized());
    request.setStatusLogIsolated(restJobRequest.isStatusLogIsolated());
    for (MapEntry restEntry : restJobRequest.getProperties()) {
        request.setProperty(restEntry.getKey(), this.jaxbConverter.unserializeAny(restEntry.getValue()));
    }
    return request;
}
Also used : MapEntry(org.xwiki.rest.model.jaxb.MapEntry) DefaultRequest(org.xwiki.job.DefaultRequest)

Example 2 with DefaultRequest

use of org.xwiki.job.DefaultRequest in project xwiki-platform by xwiki.

the class JobsResourceImpl method executeJob.

@Override
public JobStatus executeJob(String jobType, boolean async, JobRequest restJobRequest) throws XWikiRestException {
    // TODO: provide extension point to decide of the access depending on the job
    if (!this.authorization.hasAccess(Right.PROGRAM, null)) {
        throw new WebApplicationException(Status.UNAUTHORIZED);
    }
    // Parse JobRequest
    DefaultRequest request = this.factory.toJobRequest(restJobRequest);
    if (request == null) {
        request = new DefaultRequest();
    }
    // Start job
    Job job;
    try {
        // Give a few context related values to the job
        if (request.getProperty(JobRequestContext.KEY) == null) {
            JobRequestContext.set(request, this.xcontextProvider.get());
        }
        job = this.jobExecutor.execute(jobType, request);
    } catch (JobException e) {
        throw new XWikiRestException("Failed to start job", e);
    }
    // Wait for the job end if asked
    if (!async) {
        try {
            job.join();
        } catch (InterruptedException e) {
            throw new XWikiRestException("The job as been interrupted", e);
        }
        // Fail the HTTP request if the job failed
        if (job.getStatus().getError() != null) {
            throw new XWikiRestException("The job failed (" + ExceptionUtils.getRootCauseMessage(job.getStatus().getError()) + ")", job.getStatus().getError());
        }
    }
    // Get job status
    org.xwiki.job.event.status.JobStatus status = job.getStatus();
    // Convert Job status
    return this.factory.toRestJobStatus(status, null, true, false, false, null);
}
Also used : JobException(org.xwiki.job.JobException) WebApplicationException(javax.ws.rs.WebApplicationException) DefaultRequest(org.xwiki.job.DefaultRequest) XWikiRestException(org.xwiki.rest.XWikiRestException) Job(org.xwiki.job.Job)

Example 3 with DefaultRequest

use of org.xwiki.job.DefaultRequest in project xwiki-platform by xwiki.

the class ExtensionJobEventConverter method fromRemote.

@Override
public boolean fromRemote(RemoteEventData remoteEvent, LocalEventData localEvent) {
    if (remoteEvent.getEvent() instanceof JobStartedEvent) {
        JobStartedEvent jobEvent = (JobStartedEvent) remoteEvent.getEvent();
        if (JOBS.contains(jobEvent.getJobType())) {
            Request request = jobEvent.getRequest();
            // Indicate the job has been triggered by a remote event
            if (!(request instanceof AbstractRequest)) {
                request = new DefaultRequest(request);
            }
            ((AbstractRequest) request).setRemote(true);
            // We don't want to directly simulate a new JobStartedEvent event but we want to start a new job
            // which will generate a new JobStartedEvent
            localEvent.setEvent(new RemoteExtensionJobStartedEvent(jobEvent.getJobType(), request));
            return true;
        }
    }
    return false;
}
Also used : DefaultRequest(org.xwiki.job.DefaultRequest) AbstractRequest(org.xwiki.job.AbstractRequest) Request(org.xwiki.job.Request) AbstractRequest(org.xwiki.job.AbstractRequest) DefaultRequest(org.xwiki.job.DefaultRequest) JobStartedEvent(org.xwiki.job.event.JobStartedEvent)

Aggregations

DefaultRequest (org.xwiki.job.DefaultRequest)3 WebApplicationException (javax.ws.rs.WebApplicationException)1 AbstractRequest (org.xwiki.job.AbstractRequest)1 Job (org.xwiki.job.Job)1 JobException (org.xwiki.job.JobException)1 Request (org.xwiki.job.Request)1 JobStartedEvent (org.xwiki.job.event.JobStartedEvent)1 XWikiRestException (org.xwiki.rest.XWikiRestException)1 MapEntry (org.xwiki.rest.model.jaxb.MapEntry)1