Search in sources :

Example 1 with Config

use of com.adobe.acs.commons.workflow.bulk.execution.model.Config in project acs-aem-commons by Adobe-Consulting-Services.

the class StatusServlet method doGet.

@Override
@SuppressWarnings({ "squid:S3776", "squid:S1192" })
protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy hh:mm:ss aaa");
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    Config config = request.getResource().adaptTo(Config.class);
    Workspace workspace = config.getWorkspace();
    final JSONObject json = new JSONObject();
    try {
        json.put("initialized", workspace.isInitialized());
        json.put("status", workspace.getStatus());
        if (workspace.getSubStatus() != null) {
            json.put("subStatus", workspace.getSubStatus());
        }
        json.put("runnerType", config.getRunnerType());
        json.put("queryType", config.getQueryType());
        json.put("queryStatement", config.getQueryStatement());
        json.put("workflowModel", StringUtils.removeEnd(config.getWorkflowModelId(), "/jcr:content/model"));
        json.put("batchSize", config.getBatchSize());
        json.put("autoThrottle", config.isAutoThrottle());
        json.put("purgeWorkflow", config.isPurgeWorkflow());
        json.put("interval", config.getInterval());
        json.put("retryCount", config.getRetryCount());
        json.put("timeout", config.getTimeout());
        json.put("throttle", config.getThrottle());
        json.put("message", workspace.getMessage());
        if (config.isUserEventData()) {
            json.put("userEventData", config.getUserEventData());
        }
        ActionManager actionManager = actionManagerFactory.getActionManager(workspace.getActionManagerName());
        if (actionManager != null && !Status.COMPLETED.equals(workspace.getStatus())) {
            // If Complete, then look to JCR for final accounts as ActionManager may be gone
            addActionManagerTrackedCounts(workspace.getActionManagerName(), json);
            for (com.adobe.acs.commons.fam.Failure failure : actionManager.getFailureList()) {
                JSONObject failureJSON = new JSONObject();
                failureJSON.put(Failure.PN_PAYLOAD_PATH, failure.getNodePath());
                failureJSON.put(Failure.PN_FAILED_AT, sdf.format(failure.getTime().getTime()));
                json.accumulate("failures", failureJSON);
            }
        } else {
            addWorkspaceTrackedCounts(workspace, json);
            // Failures
            for (Failure failure : workspace.getFailures()) {
                json.accumulate("failures", failure.toJSON());
            }
        }
        // Times
        if (workspace.getStartedAt() != null) {
            json.put("startedAt", sdf.format(workspace.getStartedAt().getTime()));
            json.put("timeTakenInMillis", (Calendar.getInstance().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
        }
        if (workspace.getStoppedAt() != null) {
            json.put("stoppedAt", sdf.format(workspace.getStoppedAt().getTime()));
            json.put("timeTakenInMillis", (workspace.getStoppedAt().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
        }
        if (workspace.getCompletedAt() != null) {
            json.put("completedAt", sdf.format(workspace.getCompletedAt().getTime()));
            json.put("timeTakenInMillis", (workspace.getCompletedAt().getTime().getTime() - workspace.getStartedAt().getTime().getTime()));
        }
        if (AEMWorkflowRunnerImpl.class.getName().equals(config.getRunnerType())) {
            for (Payload payload : config.getWorkspace().getActivePayloads()) {
                json.accumulate("activePayloads", payload.toJSON());
            }
        }
        json.put("systemStats", getSystemStats());
        response.getWriter().write(json.toString());
    } catch (JSONException e) {
        log.error("Could not collect Bulk Workflow status due to: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not collect Bulk Workflow status.", e.getMessage());
    }
}
Also used : Config(com.adobe.acs.commons.workflow.bulk.execution.model.Config) JSONException(org.apache.sling.commons.json.JSONException) AEMWorkflowRunnerImpl(com.adobe.acs.commons.workflow.bulk.execution.impl.runners.AEMWorkflowRunnerImpl) ActionManager(com.adobe.acs.commons.fam.ActionManager) JSONObject(org.apache.sling.commons.json.JSONObject) Payload(com.adobe.acs.commons.workflow.bulk.execution.model.Payload) SimpleDateFormat(java.text.SimpleDateFormat) Failure(com.adobe.acs.commons.workflow.bulk.execution.model.Failure) Workspace(com.adobe.acs.commons.workflow.bulk.execution.model.Workspace)

Example 2 with Config

use of com.adobe.acs.commons.workflow.bulk.execution.model.Config in project acs-aem-commons by Adobe-Consulting-Services.

the class StartServlet method doPost.

@Override
@SuppressWarnings("squid:S1192")
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    try {
        final JSONObject params = new JSONObject(request.getParameter("params"));
        final ModifiableValueMap properties = request.getResource().adaptTo(ModifiableValueMap.class);
        properties.put("runnerType", params.getString("runnerType"));
        properties.put("queryType", params.getString("queryType"));
        properties.put("queryStatement", params.getString("queryStatement"));
        properties.put("relativePath", StringUtils.removeStart(params.optString("relativePath", ""), "/"));
        properties.put("workflowModel", params.getString("workflowModelId"));
        properties.put("interval", params.optInt("interval", 10));
        properties.put("timeout", params.optInt("timeout", 30));
        properties.put("throttle", params.optInt("throttle", 10));
        properties.put("retryCount", params.optInt("retryCount", 0));
        properties.put("batchSize", params.optInt("batchSize", 10));
        String userEventData = params.optString("userEventData", null);
        if (userEventData != null && !userEventData.isEmpty()) {
            properties.put("userEventData", userEventData);
        }
        properties.put("purgeWorkflow", params.optBoolean("purgeWorkflow", false));
        properties.put("autoThrottle", params.optBoolean("autoThrottle", true));
        if (AEMWorkflowRunnerImpl.class.getName().equals(properties.get("runnerType", String.class)) && isTransient(request.getResourceResolver(), properties.get("workflowModel", String.class))) {
            properties.put("runnerType", AEMTransientWorkflowRunnerImpl.class.getName());
        }
        // If FAM retires are enabled, then force BatchSize to be 1
        if (FastActionManagerRunnerImpl.class.getName().equals(properties.get("runnerType", "")) && properties.get("retryCount", 0) > 0) {
            properties.put("batchSize", 1);
        }
        request.getResourceResolver().commit();
        Config config = request.getResource().adaptTo(Config.class);
        bulkWorkflowEngine.initialize(config);
        bulkWorkflowEngine.start(config);
        response.sendRedirect(request.getResourceResolver().map(request, request.getResource().getPath()) + ".status.json");
    } catch (JSONException e) {
        log.error("Could not parse HTTP Request params: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not initialize Bulk Workflow due to invalid parameters." + " Please review the form and try again.", e.getMessage());
    } catch (RepositoryException e) {
        log.error("Could not initialize Bulk Workflow: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not initialize Bulk Workflow.", e.getMessage());
    } catch (IllegalArgumentException e) {
        log.warn("Could not initialize Bulk Workflow due to invalid arguments: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not initialize Bulk Workflow due to invalid arguments.", e.getMessage());
    } catch (Exception e) {
        log.error("Could not initialize Bulk Workflow due to unexpected error: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not start Bulk Workflow.", e.getMessage());
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Config(com.adobe.acs.commons.workflow.bulk.execution.model.Config) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) JSONException(org.apache.sling.commons.json.JSONException) RepositoryException(javax.jcr.RepositoryException) AEMTransientWorkflowRunnerImpl(com.adobe.acs.commons.workflow.bulk.execution.impl.runners.AEMTransientWorkflowRunnerImpl)

Example 3 with Config

use of com.adobe.acs.commons.workflow.bulk.execution.model.Config in project acs-aem-commons by Adobe-Consulting-Services.

the class ResumeServlet method doPost.

@Override
protected final void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    response.setCharacterEncoding("UTF-8");
    final JSONObject params;
    try {
        params = new JSONObject(request.getParameter("params"));
        final Config config = request.getResource().adaptTo(Config.class);
        int throttle = params.optInt("throttle", -1);
        int interval = params.optInt("interval", -1);
        if (throttle > -1) {
            config.setThrottle(throttle);
            config.commit();
        } else if (interval > -1) {
            config.setInterval(interval);
            config.commit();
        }
        bulkWorkflowEngine.resume(config);
        response.sendRedirect(request.getResourceResolver().map(request, request.getResource().getPath()) + ".status.json");
    } catch (JSONException e) {
        log.error("Could not resume Bulk Workflow due to: {}", e);
        JSONErrorUtil.sendJSONError(response, SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Could not resume Bulk Workflow.", e.getMessage());
    }
}
Also used : JSONObject(org.apache.sling.commons.json.JSONObject) Config(com.adobe.acs.commons.workflow.bulk.execution.model.Config) JSONException(org.apache.sling.commons.json.JSONException)

Aggregations

Config (com.adobe.acs.commons.workflow.bulk.execution.model.Config)3 JSONException (org.apache.sling.commons.json.JSONException)3 JSONObject (org.apache.sling.commons.json.JSONObject)3 ActionManager (com.adobe.acs.commons.fam.ActionManager)1 AEMTransientWorkflowRunnerImpl (com.adobe.acs.commons.workflow.bulk.execution.impl.runners.AEMTransientWorkflowRunnerImpl)1 AEMWorkflowRunnerImpl (com.adobe.acs.commons.workflow.bulk.execution.impl.runners.AEMWorkflowRunnerImpl)1 Failure (com.adobe.acs.commons.workflow.bulk.execution.model.Failure)1 Payload (com.adobe.acs.commons.workflow.bulk.execution.model.Payload)1 Workspace (com.adobe.acs.commons.workflow.bulk.execution.model.Workspace)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 RepositoryException (javax.jcr.RepositoryException)1 ServletException (javax.servlet.ServletException)1 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)1