001    /**
002     * Copyright (c) 2010 Yahoo! Inc. All rights reserved.
003     * Licensed under the Apache License, Version 2.0 (the "License");
004     * you may not use this file except in compliance with the License.
005     * You may obtain a copy of the License at
006     *
007     *   http://www.apache.org/licenses/LICENSE-2.0
008     *
009     *  Unless required by applicable law or agreed to in writing, software
010     *  distributed under the License is distributed on an "AS IS" BASIS,
011     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012     *  See the License for the specific language governing permissions and
013     *  limitations under the License. See accompanying LICENSE file.
014     */
015    package org.apache.oozie.command.bundle;
016    
017    import java.util.List;
018    
019    import org.apache.oozie.BundleJobBean;
020    import org.apache.oozie.ErrorCode;
021    import org.apache.oozie.XException;
022    import org.apache.oozie.command.CommandException;
023    import org.apache.oozie.command.PreconditionException;
024    import org.apache.oozie.command.XCommand;
025    import org.apache.oozie.executor.jpa.BundleActionsDeleteForPurgeJPAExecutor;
026    import org.apache.oozie.executor.jpa.BundleJobDeleteJPAExecutor;
027    import org.apache.oozie.executor.jpa.BundleJobsGetForPurgeJPAExecutor;
028    import org.apache.oozie.executor.jpa.JPAExecutorException;
029    import org.apache.oozie.service.JPAService;
030    import org.apache.oozie.service.Services;
031    import org.apache.oozie.util.XLog;
032    
033    /**
034     * This class is used for bundle purge command
035     */
036    public class BundlePurgeXCommand extends XCommand<Void> {
037        private JPAService jpaService = null;
038        private final int olderThan;
039        private final int limit;
040        private List<BundleJobBean> jobList = null;
041    
042        public BundlePurgeXCommand(int olderThan, int limit) {
043            super("bundle_purge", "bundle_purge", 0);
044            this.olderThan = olderThan;
045            this.limit = limit;
046        }
047    
048        /* (non-Javadoc)
049         * @see org.apache.oozie.command.XCommand#loadState()
050         */
051        @Override
052        protected void loadState() throws CommandException {
053            try {
054                jpaService = Services.get().get(JPAService.class);
055    
056                if (jpaService != null) {
057                    this.jobList = jpaService.execute(new BundleJobsGetForPurgeJPAExecutor(olderThan, limit));
058                }
059                else {
060                    throw new CommandException(ErrorCode.E0610);
061                }
062            }
063            catch (XException ex) {
064                throw new CommandException(ex);
065            }
066        }
067    
068        /* (non-Javadoc)
069         * @see org.apache.oozie.command.XCommand#execute()
070         */
071        @Override
072        protected Void execute() throws CommandException {
073            LOG.debug("STARTED Bundle-Purge to purge Jobs older than [{0}] days.", olderThan);
074    
075            int actionDeleted = 0;
076            if (jobList != null && jobList.size() != 0) {
077                for (BundleJobBean bundle : jobList) {
078                    String jobId = bundle.getId();
079                    try {
080                        jpaService.execute(new BundleJobDeleteJPAExecutor(jobId));
081                        actionDeleted += jpaService.execute(new BundleActionsDeleteForPurgeJPAExecutor(jobId));
082                    }
083                    catch (JPAExecutorException e) {
084                        throw new CommandException(e);
085                    }
086                }
087                LOG.debug("ENDED Bundle-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted);
088            }
089            else {
090                LOG.debug("ENDED Bundle-Purge no Bundle job to be deleted");
091            }
092            return null;
093        }
094    
095        /* (non-Javadoc)
096         * @see org.apache.oozie.command.XCommand#getEntityKey()
097         */
098        @Override
099        protected String getEntityKey() {
100            return null;
101        }
102    
103        /* (non-Javadoc)
104         * @see org.apache.oozie.command.XCommand#isLockRequired()
105         */
106        @Override
107        protected boolean isLockRequired() {
108            return false;
109        }
110    
111        /* (non-Javadoc)
112         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
113         */
114        @Override
115        protected void verifyPrecondition() throws CommandException, PreconditionException {
116        }
117    }