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