001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.command.wf;
020
021import java.util.ArrayList;
022import java.util.Date;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026
027import org.apache.oozie.ErrorCode;
028import org.apache.oozie.WorkflowActionBean;
029import org.apache.oozie.WorkflowJobBean;
030import org.apache.oozie.client.rest.JsonTags;
031import org.apache.oozie.client.rest.JsonUtils;
032import org.apache.oozie.command.CommandException;
033import org.apache.oozie.command.wf.ActionXCommand.ActionExecutorContext;
034import org.apache.oozie.executor.jpa.JPAExecutorException;
035import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor;
036import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor;
037import org.apache.oozie.executor.jpa.WorkflowActionQueryExecutor.WorkflowActionQuery;
038import org.apache.oozie.executor.jpa.WorkflowJobQueryExecutor.WorkflowJobQuery;
039import org.apache.oozie.util.JobUtils;
040import org.apache.oozie.util.LogUtils;
041
042public class WorkflowActionRetryInfoXCommand extends WorkflowXCommand<List<Map<String, String>>> {
043    private String actionId;
044    private WorkflowJobBean wfJob;
045    protected WorkflowActionBean wfAction = null;
046
047    public WorkflowActionRetryInfoXCommand(String id) {
048        super("action.retries.info", "action.retries.info", 1);
049        this.actionId = id;
050    }
051
052    @Override
053    protected List<Map<String, String>> execute() throws CommandException {
054        List<Map<String, String>> retriesList = new ArrayList<Map<String, String>>();
055        ActionExecutorContext context = new ActionXCommand.ActionExecutorContext(wfJob, wfAction);
056        for (int i = 0; i < wfAction.getUserRetryCount(); i++) {
057            Map<String, String> retries = new HashMap<String, String>();
058            String value = context.getVar(JobUtils.getRetryKey(JsonTags.WORKFLOW_ACTION_EXTERNAL_CHILD_IDS, i));
059            if (value != null) {
060                retries.put(JsonTags.WORKFLOW_ACTION_EXTERNAL_CHILD_IDS, value);
061            }
062            value = context.getVar(JobUtils.getRetryKey(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, i));
063            if (value != null) {
064                retries.put(JsonTags.WORKFLOW_ACTION_CONSOLE_URL, value);
065            }
066            value = context.getVar(JobUtils.getRetryKey(JsonTags.WORKFLOW_ACTION_START_TIME, i));
067            if (value != null) {
068                retries.put(JsonTags.WORKFLOW_ACTION_START_TIME,
069                        JsonUtils.formatDateRfc822(new Date(Long.parseLong(value))));
070            }
071            value = context.getVar(JobUtils.getRetryKey(JsonTags.WORKFLOW_ACTION_END_TIME, i));
072            if (value != null) {
073                retries.put(JsonTags.WORKFLOW_ACTION_END_TIME,
074                        JsonUtils.formatDateRfc822(new Date(Long.parseLong(value))));
075            }
076            retries.put(JsonTags.ACTION_ATTEMPT, String.valueOf(i + 1));
077            retriesList.add(retries);
078        }
079        return retriesList;
080    }
081
082    @Override
083    public String getEntityKey() {
084        return null;
085    }
086
087    @Override
088    protected void loadState() throws CommandException {
089        try {
090            this.wfAction = WorkflowActionQueryExecutor.getInstance().get(WorkflowActionQuery.GET_ACTION_CHECK,
091                    actionId);
092            this.wfJob = WorkflowJobQueryExecutor.getInstance().get(WorkflowJobQuery.GET_WORKFLOW_DEFINITION,
093                    actionId.substring(0, actionId.indexOf("@")));
094            LogUtils.setLogInfo(wfAction);
095        }
096        catch (JPAExecutorException ex) {
097            if (ex.getErrorCode() == ErrorCode.E0605) {
098                throw new CommandException(ErrorCode.E0605, actionId);
099            }
100            else {
101                throw new CommandException(ex);
102            }
103        }
104    }
105
106    @Override
107    protected void verifyPrecondition() throws CommandException {
108    }
109
110    @Override
111    protected boolean isLockRequired() {
112        return false;
113    }
114
115}