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.Collection;
021    import java.util.List;
022    
023    import org.apache.oozie.ErrorCode;
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.client.rest.JsonBean;
028    import org.apache.oozie.command.CommandException;
029    import org.apache.oozie.command.PreconditionException;
030    import org.apache.oozie.executor.jpa.BulkDeleteForPurgeJPAExecutor;
031    import org.apache.oozie.executor.jpa.JPAExecutorException;
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<? extends JsonBean> 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                try {
053                    actionDeleted = jpaService.execute(new BulkDeleteForPurgeJPAExecutor((Collection<JsonBean>) jobList));
054                }
055                catch (JPAExecutorException je) {
056                    throw new CommandException(je);
057                }
058                LOG.debug("ENDED Workflow-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted);
059            }
060            else {
061                LOG.debug("ENDED Workflow-Purge no workflow job to be deleted");
062            }
063            return null;
064        }
065    
066        @Override
067        public String getEntityKey() {
068            return null;
069        }
070    
071        @Override
072        protected boolean isLockRequired() {
073            return false;
074        }
075    
076        @Override
077        protected void loadState() throws CommandException {
078            try {
079                jpaService = Services.get().get(JPAService.class);
080    
081                if (jpaService != null) {
082                    this.jobList = jpaService.execute(new WorkflowJobsGetForPurgeJPAExecutor(olderThan, limit));
083                }
084                else {
085                    throw new CommandException(ErrorCode.E0610);
086                }
087            }
088            catch (XException ex) {
089                throw new CommandException(ex);
090            }
091    
092        }
093    
094        @Override
095        protected void verifyPrecondition() throws CommandException, PreconditionException {
096        }
097    
098    }