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());
}
}
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;
}
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());
}
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"));
}
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;
}
Aggregations