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