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.bundle;
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.client.rest.JsonBean;
026    import org.apache.oozie.command.CommandException;
027    import org.apache.oozie.command.PreconditionException;
028    import org.apache.oozie.command.XCommand;
029    import org.apache.oozie.executor.jpa.BulkDeleteForPurgeJPAExecutor;
030    import org.apache.oozie.executor.jpa.BundleJobsGetForPurgeJPAExecutor;
031    import org.apache.oozie.executor.jpa.JPAExecutorException;
032    import org.apache.oozie.service.JPAService;
033    import org.apache.oozie.service.Services;
034    
035    /**
036     * This class is used for bundle purge command
037     */
038    public class BundlePurgeXCommand extends XCommand<Void> {
039        private JPAService jpaService = null;
040        private final int olderThan;
041        private final int limit;
042        private List<? extends JsonBean> jobList = null;
043    
044        public BundlePurgeXCommand(int olderThan, int limit) {
045            super("bundle_purge", "bundle_purge", 0);
046            this.olderThan = olderThan;
047            this.limit = limit;
048        }
049    
050        /* (non-Javadoc)
051         * @see org.apache.oozie.command.XCommand#loadState()
052         */
053        @Override
054        protected void loadState() throws CommandException {
055            try {
056                jpaService = Services.get().get(JPAService.class);
057    
058                if (jpaService != null) {
059                    this.jobList = jpaService.execute(new BundleJobsGetForPurgeJPAExecutor(olderThan, limit));
060                }
061                else {
062                    throw new CommandException(ErrorCode.E0610);
063                }
064            }
065            catch (XException ex) {
066                throw new CommandException(ex);
067            }
068        }
069    
070        /* (non-Javadoc)
071         * @see org.apache.oozie.command.XCommand#execute()
072         */
073        @Override
074        protected Void execute() throws CommandException {
075            LOG.debug("STARTED Bundle-Purge to purge Jobs older than [{0}] days.", olderThan);
076    
077            int actionDeleted = 0;
078            if (jobList != null && jobList.size() != 0) {
079                try {
080                    actionDeleted = jpaService.execute(new BulkDeleteForPurgeJPAExecutor((Collection<JsonBean>) jobList));
081                }
082                catch (JPAExecutorException je) {
083                    throw new CommandException(je);
084                }
085                LOG.debug("ENDED Bundle-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted);
086            }
087            else {
088                LOG.debug("ENDED Bundle-Purge no Bundle job to be deleted");
089            }
090            return null;
091        }
092    
093        /* (non-Javadoc)
094         * @see org.apache.oozie.command.XCommand#getEntityKey()
095         */
096        @Override
097        public String getEntityKey() {
098            return null;
099        }
100    
101        /* (non-Javadoc)
102         * @see org.apache.oozie.command.XCommand#isLockRequired()
103         */
104        @Override
105        protected boolean isLockRequired() {
106            return false;
107        }
108    
109        /* (non-Javadoc)
110         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
111         */
112        @Override
113        protected void verifyPrecondition() throws CommandException, PreconditionException {
114        }
115    }