Search in sources :

Example 1 with TaskService

use of com.enonic.xp.task.TaskService in project xp by enonic.

the class ContentResourceTest method getResourceInstance.

@Override
protected Object getResourceInstance() {
    this.contentService = Mockito.mock(ContentService.class);
    this.taskService = Mockito.mock(TaskService.class);
    final ProjectService projectService = Mockito.mock(ProjectService.class);
    final SyncContentService syncContentService = Mockito.mock(SyncContentService.class);
    final ContentResource resource = new ContentResource();
    resource.setContentService(contentService);
    resource.setTaskService(taskService);
    resource.setProjectService(projectService);
    resource.setSyncContentService(syncContentService);
    return resource;
}
Also used : SyncContentService(com.enonic.xp.content.SyncContentService) TaskService(com.enonic.xp.task.TaskService) ProjectService(com.enonic.xp.project.ProjectService) ContentService(com.enonic.xp.content.ContentService) SyncContentService(com.enonic.xp.content.SyncContentService)

Example 2 with TaskService

use of com.enonic.xp.task.TaskService in project xp by enonic.

the class ExecuteFunctionHandler method executeFunction.

public String executeFunction() {
    final TaskService taskService = taskServiceSupplier.get();
    final TaskWrapper taskWrapper = new TaskWrapper(taskFunction, description);
    final TaskId taskId = taskService.submitTask(taskWrapper, description);
    return taskId.toString();
}
Also used : TaskId(com.enonic.xp.task.TaskId) TaskService(com.enonic.xp.task.TaskService)

Example 3 with TaskService

use of com.enonic.xp.task.TaskService in project xp by enonic.

the class SubmitTaskHandler method submitTask.

public String submitTask() {
    descriptor = descriptor == null ? "" : descriptor;
    final DescriptorKey taskKey;
    if (descriptor.contains(":")) {
        taskKey = DescriptorKey.from(descriptor);
    } else {
        final ApplicationKey app = getApplication();
        if (app == null) {
            throw new RuntimeException("Could not resolve current application for descriptord task: '" + descriptor + "'");
        }
        taskKey = DescriptorKey.from(app, descriptor);
    }
    final TaskService taskService = taskServiceSupplier.get();
    PropertyTree data = propertyTreeMarshallerServiceSupplier.get().marshal(Optional.ofNullable(config).map(ScriptValue::getMap).orElse(Map.of()));
    final SubmitTaskParams params = SubmitTaskParams.create().descriptorKey(taskKey).data(data).build();
    final TaskId taskId = taskService.submitTask(params);
    return taskId.toString();
}
Also used : ApplicationKey(com.enonic.xp.app.ApplicationKey) ScriptValue(com.enonic.xp.script.ScriptValue) TaskId(com.enonic.xp.task.TaskId) TaskService(com.enonic.xp.task.TaskService) PropertyTree(com.enonic.xp.data.PropertyTree) DescriptorKey(com.enonic.xp.page.DescriptorKey) SubmitTaskParams(com.enonic.xp.task.SubmitTaskParams)

Example 4 with TaskService

use of com.enonic.xp.task.TaskService in project xp by enonic.

the class SubmitTaskHandlerTest method initialize.

@Override
public void initialize() throws Exception {
    super.initialize();
    taskService = Mockito.mock(TaskService.class);
    addService(TaskService.class, taskService);
    final TaskDescriptorService taskDescriptorService = Mockito.mock(TaskDescriptorService.class);
    addService(TaskDescriptorService.class, taskDescriptorService);
    final MixinService mixinService = Mockito.mock(MixinService.class);
    addService(MixinService.class, mixinService);
    final Form cfg1 = Form.create().addFormItem(Input.create().name("count").label("Count").inputType(InputTypeName.LONG).required(true).build()).build();
    final Form cfg2 = Form.create().addFormItem(Input.create().name("values").label("Values").inputType(InputTypeName.TEXT_LINE).required(true).multiple(true).build()).build();
    final TaskDescriptor desc1 = TaskDescriptor.create().key(DescriptorKey.from("myapplication:job42")).config(cfg1).build();
    final TaskDescriptor desc2 = TaskDescriptor.create().key(DescriptorKey.from("myapplication:my-task")).config(cfg2).build();
    final TaskDescriptor desc3 = TaskDescriptor.create().key(DescriptorKey.from("other-app:some-task")).build();
    Mockito.when(taskDescriptorService.getTasks()).thenReturn(Descriptors.from(desc1, desc2, desc3));
    Mockito.when(mixinService.inlineFormItems(any(Form.class))).thenAnswer(invocation -> invocation.getArguments()[0]);
    addService(PropertyTreeMarshallerService.class, PropertyTreeMarshallerServiceFactory.newInstance(mixinService));
}
Also used : TaskDescriptorService(com.enonic.xp.task.TaskDescriptorService) TaskDescriptor(com.enonic.xp.task.TaskDescriptor) Form(com.enonic.xp.form.Form) TaskService(com.enonic.xp.task.TaskService) MixinService(com.enonic.xp.schema.mixin.MixinService)

Example 5 with TaskService

use of com.enonic.xp.task.TaskService in project xp by enonic.

the class SchedulableTaskImpl method run.

@Override
public void run() {
    try {
        final TaskId taskId = taskContext().callWith(() -> OsgiSupport.withService(TaskService.class, taskService -> taskService.submitTask(SubmitTaskParams.create().descriptorKey(job.getDescriptor()).data(job.getConfig()).build())));
        adminContext().runWith(() -> OsgiSupport.withService(NodeService.class, nodeService -> UpdateLastRunCommand.create().nodeService(nodeService).name(job.getName()).lastRun(Instant.now()).lastTaskId(taskId).build().execute()));
    } catch (Exception e) {
        LOG.warn("Error while running job [{}]", this.job.getName(), e);
    } catch (Throwable t) {
        LOG.error("Error while running job [{}], no further attempts will be made", this.job.getName(), t);
        throw t;
    }
}
Also used : SubmitTaskParams(com.enonic.xp.task.SubmitTaskParams) LoggerFactory(org.slf4j.LoggerFactory) UpdateLastRunCommand(com.enonic.xp.impl.scheduler.UpdateLastRunCommand) ScheduledJob(com.enonic.xp.scheduler.ScheduledJob) ContextAccessor(com.enonic.xp.context.ContextAccessor) NodeService(com.enonic.xp.node.NodeService) TaskService(com.enonic.xp.task.TaskService) ContextBuilder(com.enonic.xp.context.ContextBuilder) VerifiedUsernameAuthToken(com.enonic.xp.security.auth.VerifiedUsernameAuthToken) SecurityService(com.enonic.xp.security.SecurityService) PropertyTree(com.enonic.xp.data.PropertyTree) User(com.enonic.xp.security.User) Logger(org.slf4j.Logger) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Instant(java.time.Instant) OsgiSupport(com.enonic.xp.core.internal.osgi.OsgiSupport) Serializable(java.io.Serializable) TaskId(com.enonic.xp.task.TaskId) PrincipalKey(com.enonic.xp.security.PrincipalKey) DescriptorKey(com.enonic.xp.page.DescriptorKey) ScheduledJobName(com.enonic.xp.scheduler.ScheduledJobName) RoleKeys(com.enonic.xp.security.RoleKeys) Preconditions(com.google.common.base.Preconditions) Context(com.enonic.xp.context.Context) ScheduleCalendar(com.enonic.xp.scheduler.ScheduleCalendar) TaskId(com.enonic.xp.task.TaskId) TaskService(com.enonic.xp.task.TaskService) NodeService(com.enonic.xp.node.NodeService)

Aggregations

TaskService (com.enonic.xp.task.TaskService)7 TaskId (com.enonic.xp.task.TaskId)3 PropertyTree (com.enonic.xp.data.PropertyTree)2 DescriptorKey (com.enonic.xp.page.DescriptorKey)2 SubmitTaskParams (com.enonic.xp.task.SubmitTaskParams)2 TaskInfo (com.enonic.xp.task.TaskInfo)2 ApplicationKey (com.enonic.xp.app.ApplicationKey)1 ContentService (com.enonic.xp.content.ContentService)1 SyncContentService (com.enonic.xp.content.SyncContentService)1 Context (com.enonic.xp.context.Context)1 ContextAccessor (com.enonic.xp.context.ContextAccessor)1 ContextBuilder (com.enonic.xp.context.ContextBuilder)1 OsgiSupport (com.enonic.xp.core.internal.osgi.OsgiSupport)1 Form (com.enonic.xp.form.Form)1 UpdateLastRunCommand (com.enonic.xp.impl.scheduler.UpdateLastRunCommand)1 NodeService (com.enonic.xp.node.NodeService)1 ProjectService (com.enonic.xp.project.ProjectService)1 ScheduleCalendar (com.enonic.xp.scheduler.ScheduleCalendar)1 ScheduledJob (com.enonic.xp.scheduler.ScheduledJob)1 ScheduledJobName (com.enonic.xp.scheduler.ScheduledJobName)1