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    package org.apache.oozie.command.wf;
019    
020    import java.util.List;
021    
022    import org.apache.oozie.ErrorCode;
023    import org.apache.oozie.WorkflowJobBean;
024    import org.apache.oozie.XException;
025    import org.apache.oozie.service.JPAService;
026    import org.apache.oozie.service.Services;
027    import org.apache.oozie.command.CommandException;
028    import org.apache.oozie.command.PreconditionException;
029    import org.apache.oozie.executor.jpa.JPAExecutorException;
030    import org.apache.oozie.executor.jpa.WorkflowActionsDeleteForPurgeJPAExecutor;
031    import org.apache.oozie.executor.jpa.WorkflowJobDeleteJPAExecutor;
032    import org.apache.oozie.executor.jpa.WorkflowJobsGetForPurgeJPAExecutor;
033    
034    public class PurgeXCommand extends WorkflowXCommand<Void> {
035        private JPAService jpaService = null;
036        private int olderThan;
037        private int limit;
038        private List<WorkflowJobBean> jobList = null;
039    
040        public PurgeXCommand(int olderThan, int limit) {
041            super("purge", "purge", 0);
042            this.olderThan = olderThan;
043            this.limit = limit;
044        }
045    
046        @Override
047        protected Void execute() throws CommandException {
048            LOG.debug("STARTED Workflow-Purge Attempting to purge Jobs older than [{0}] days.", olderThan);
049    
050            int actionDeleted = 0;
051            if (jobList != null && jobList.size() != 0) {
052                for (WorkflowJobBean w : jobList) {
053                    String wfId = w.getId();
054                    try {
055                        jpaService.execute(new WorkflowJobDeleteJPAExecutor(wfId));
056                        actionDeleted += jpaService.execute(new WorkflowActionsDeleteForPurgeJPAExecutor(wfId));
057                    }
058                    catch (JPAExecutorException e) {
059                        throw new CommandException(e);
060                    }
061                }
062                LOG.debug("ENDED Workflow-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted);
063            }
064            else {
065                LOG.debug("ENDED Workflow-Purge no workflow job to be deleted");
066            }
067            return null;
068        }
069    
070        @Override
071        public String getEntityKey() {
072            return null;
073        }
074    
075        @Override
076        protected boolean isLockRequired() {
077            return false;
078        }
079    
080        @Override
081        protected void loadState() throws CommandException {
082            try {
083                jpaService = Services.get().get(JPAService.class);
084    
085                if (jpaService != null) {
086                    this.jobList = jpaService.execute(new WorkflowJobsGetForPurgeJPAExecutor(olderThan, limit));
087                }
088                else {
089                    throw new CommandException(ErrorCode.E0610);
090                }
091            }
092            catch (XException ex) {
093                throw new CommandException(ex);
094            }
095    
096        }
097    
098        @Override
099        protected void verifyPrecondition() throws CommandException, PreconditionException {
100        }
101    
102    }