001/*
002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nour Al Kotob
018 */
019package org.nuxeo.ecm.restapi.server.jaxrs.management;
020
021import java.io.Serializable;
022import java.util.HashMap;
023import java.util.Map;
024
025import javax.ws.rs.FormParam;
026import javax.ws.rs.POST;
027import javax.ws.rs.Path;
028import javax.ws.rs.Produces;
029import javax.ws.rs.core.MediaType;
030
031import org.apache.commons.lang3.StringUtils;
032import org.nuxeo.ecm.automation.AutomationService;
033import org.nuxeo.ecm.automation.OperationContext;
034import org.nuxeo.ecm.automation.OperationException;
035import org.nuxeo.ecm.automation.core.operations.services.workmanager.WorkManagerRunWorkInFailure;
036import org.nuxeo.ecm.core.api.Blob;
037import org.nuxeo.ecm.webengine.model.WebObject;
038import org.nuxeo.ecm.webengine.model.impl.AbstractResource;
039import org.nuxeo.ecm.webengine.model.impl.ResourceTypeImpl;
040import org.nuxeo.runtime.api.Framework;
041
042/**
043 * @since 11.3
044 */
045@WebObject(type = ManagementObject.MANAGEMENT_OBJECT_PREFIX + "work-manager")
046public class WorkManagerObject extends AbstractResource<ResourceTypeImpl> {
047
048    public static final String TIMEOUT_SECONDS_PARAM_KEY = "timeoutSeconds";
049
050    /**
051     * Executes Works stored in the dead letter queue (DLQ) after failure.
052     *
053     * @param timeoutSeconds a timeout for the works to end.
054     * @return The result of the rerun
055     */
056    @POST
057    @Produces(MediaType.APPLICATION_JSON)
058    @Path("run-works-in-failure")
059    public Blob launch(@FormParam(TIMEOUT_SECONDS_PARAM_KEY) String timeoutSeconds) throws OperationException {
060        AutomationService automationService = Framework.getService(AutomationService.class);
061        try (OperationContext operationCtx = new OperationContext(this.ctx.getCoreSession())) {
062            Map<String, Serializable> params = new HashMap<>();
063            if (StringUtils.isNotBlank(timeoutSeconds)) {
064                params.put(TIMEOUT_SECONDS_PARAM_KEY, timeoutSeconds);
065            }
066            return (Blob) automationService.run(operationCtx, WorkManagerRunWorkInFailure.ID, params);
067        }
068    }
069}