Search in sources :

Example 16 with Job

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

the class SaveAction method startCreateJob.

private Job startCreateJob(EntityReference entityReference, EditForm editForm) throws XWikiException {
    if (StringUtils.isBlank(editForm.getTemplate())) {
        // No template specified, nothing more to do.
        return null;
    }
    // If a template is set in the request, then this is a create action which needs to be handled by a create job,
    // but skipping the target document, which is now already saved by the save action.
    RefactoringScriptService refactoring = (RefactoringScriptService) Utils.getComponent(ScriptService.class, "refactoring");
    CreateRequest request = refactoring.createCreateRequest(Arrays.asList(entityReference));
    // Set the target document.
    request.setEntityReferences(Arrays.asList(entityReference));
    // Set the template to use.
    DocumentReferenceResolver<String> resolver = Utils.getComponent(DocumentReferenceResolver.TYPE_STRING, "currentmixed");
    EntityReference templateReference = resolver.resolve(editForm.getTemplate());
    request.setTemplateReference(templateReference);
    // We`ve already created and populated the fields of the target document, focus only on the remaining children
    // specified in the template.
    request.setSkippedEntities(Arrays.asList(entityReference));
    Job createJob = refactoring.create(request);
    if (createJob != null) {
        return createJob;
    } else {
        throw new XWikiException(String.format("Failed to schedule the create job for [%s]", entityReference), refactoring.getLastError());
    }
}
Also used : RefactoringScriptService(org.xwiki.refactoring.script.RefactoringScriptService) ScriptService(org.xwiki.script.service.ScriptService) CreateRequest(org.xwiki.refactoring.job.CreateRequest) EntityReference(org.xwiki.model.reference.EntityReference) RefactoringScriptService(org.xwiki.refactoring.script.RefactoringScriptService) Job(org.xwiki.job.Job) XWikiException(com.xpn.xwiki.XWikiException)

Example 17 with Job

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

the class UndeleteAction method restoreDocument.

private boolean restoreDocument(XWikiDeletedDocument deletedDocument, XWikiContext context) throws XWikiException {
    Job restoreJob = startRestoreJob(deletedDocument, context);
    // If the user asked for an asynchronous action...
    if (isAsync(context.getRequest())) {
        List<String> jobId = restoreJob.getRequest().getId();
        // Note: We use spaceRedirect=false to fix the link to the document when view mode would normally try to
        // modify the context document to a non-terminal one but the restored document is actually terminal.
        String queryString = "xpage=restore&jobId=" + serializeJobId(jobId) + "&spaceRedirect=false";
        // We redirect to the view action and accept the edge case when the restored document's rights might prevent
        // the restoring user to view the result. In that case, an admin must be contacted to fix the rights.
        sendRedirect(context.getResponse(), Utils.getRedirect(VIEW_ACTION, queryString, context));
        // A redirect has been performed.
        return true;
    }
    // Otherwise...
    try {
        restoreJob.join();
    } catch (InterruptedException e) {
        throw new XWikiException(String.format("Failed to restore [%s] from batch [%s]", deletedDocument.getFullName(), deletedDocument.getBatchId()), e);
    }
    // No redirect has been performed.
    return false;
}
Also used : Job(org.xwiki.job.Job) XWikiException(com.xpn.xwiki.XWikiException)

Example 18 with Job

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

the class DefaultWikiCreatorTest method createWiki.

@Test
public void createWiki() throws Exception {
    WikiCreationRequest request = new WikiCreationRequest();
    request.setWikiId("wikiId");
    // Mock
    Job job = mock(Job.class);
    when(jobExecutor.execute("wikicreationjob", request)).thenReturn(job);
    // Test
    assertEquals(job, mocker.getComponentUnderTest().createWiki(request));
    // Verify
    assertEquals(Arrays.asList("wikicreation", "createandinstall", "wikiId"), request.getId());
}
Also used : WikiCreationRequest(org.xwiki.platform.wiki.creationjob.WikiCreationRequest) Job(org.xwiki.job.Job) Test(org.junit.Test)

Example 19 with Job

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

the class DefaultWikiCreatorTest method getJobStatus.

@Test
public void getJobStatus() throws Exception {
    // Mocks
    Job job = mock(Job.class);
    JobStatus jobStatus1 = mock(JobStatus.class);
    when(job.getStatus()).thenReturn(jobStatus1);
    when(jobExecutor.getJob(Arrays.asList("wikicreation", "createandinstall", "wiki1"))).thenReturn(job);
    JobStatus jobStatus2 = mock(JobStatus.class);
    when(jobStatusStore.getJobStatus(Arrays.asList("wikicreation", "createandinstall", "wiki2"))).thenReturn(jobStatus2);
    // Tests
    assertEquals(jobStatus1, mocker.getComponentUnderTest().getJobStatus("wiki1"));
    assertEquals(jobStatus2, mocker.getComponentUnderTest().getJobStatus("wiki2"));
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) Job(org.xwiki.job.Job) Test(org.junit.Test)

Example 20 with Job

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

the class DefaultDistributionManager method startFarmJob.

@Override
public DefaultDistributionJob startFarmJob() {
    try {
        this.farmDistributionJob = this.componentManager.getInstance(Job.class, DefaultDistributionJob.HINT);
        XWikiContext xcontext = this.xcontextProvider.get();
        final DistributionRequest request = new DistributionRequest();
        request.setId(getFarmJobId());
        request.setWiki(xcontext.getMainXWiki());
        request.setUserReference(xcontext.getUserReference());
        request.setInteractive(this.configuration.getProperty("distribution.job.interactive", true));
        Thread distributionJobThread = new Thread(new Runnable() {

            @Override
            public void run() {
                // Create a clean Execution Context
                ExecutionContext context = new ExecutionContext();
                try {
                    executionContextManager.initialize(context);
                } catch (ExecutionContextException e) {
                    throw new RuntimeException("Failed to initialize farm distribution job execution context", e);
                }
                farmDistributionJob.initialize(request);
                farmDistributionJob.run();
            }
        });
        distributionJobThread.setDaemon(true);
        distributionJobThread.setName("Farm distribution initialization");
        distributionJobThread.start();
        // Wait until the job is ready (or finished)
        this.farmDistributionJob.awaitReady();
        return this.farmDistributionJob;
    } catch (Exception e) {
        this.logger.error("Failed to create farm distribution job", e);
    }
    return null;
}
Also used : DistributionRequest(org.xwiki.extension.distribution.internal.job.DistributionRequest) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) DistributionJob(org.xwiki.extension.distribution.internal.job.DistributionJob) Job(org.xwiki.job.Job) DefaultDistributionJob(org.xwiki.extension.distribution.internal.job.DefaultDistributionJob) ExecutionContextException(org.xwiki.context.ExecutionContextException) XWikiException(com.xpn.xwiki.XWikiException) InitializationException(org.xwiki.component.phase.InitializationException) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Aggregations

Job (org.xwiki.job.Job)41 Test (org.junit.Test)11 JobStatus (org.xwiki.job.event.status.JobStatus)10 AbstractExtensionJob (org.xwiki.extension.job.internal.AbstractExtensionJob)8 InstallJob (org.xwiki.extension.job.internal.InstallJob)8 UninstallJob (org.xwiki.extension.job.internal.UninstallJob)8 JobException (org.xwiki.job.JobException)8 DocumentReference (org.xwiki.model.reference.DocumentReference)8 InstallRequest (org.xwiki.extension.job.InstallRequest)7 UpgradePlanJob (org.xwiki.extension.job.internal.UpgradePlanJob)7 InstallPlanJob (org.xwiki.extension.job.internal.InstallPlanJob)6 UninstallPlanJob (org.xwiki.extension.job.internal.UninstallPlanJob)6 XWikiException (com.xpn.xwiki.XWikiException)5 LogEvent (org.xwiki.logging.event.LogEvent)5 InstallException (org.xwiki.extension.InstallException)4 UninstallException (org.xwiki.extension.UninstallException)4 XarInstalledExtension (org.xwiki.extension.xar.internal.repository.XarInstalledExtension)4 EntityReference (org.xwiki.model.reference.EntityReference)4 WikiReference (org.xwiki.model.reference.WikiReference)4 IOException (java.io.IOException)3