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.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.executor.jpa.BulkDeleteForPurgeJPAExecutor;
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<? extends JsonBean> 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                try {
059                    actionDeleted = jpaService.execute(new BulkDeleteForPurgeJPAExecutor((Collection<JsonBean>) jobList));
060                }
061                catch (JPAExecutorException je) {
062                    throw new CommandException(je);
063                }
064                LOG.debug("ENDED Coord-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted);
065            }
066            else {
067                LOG.debug("ENDED Coord-Purge no Coord job to be deleted");
068            }
069            return null;
070        }
071    
072        /* (non-Javadoc)
073         * @see org.apache.oozie.command.XCommand#getEntityKey()
074         */
075        @Override
076        public String getEntityKey() {
077            return null;
078        }
079    
080        /* (non-Javadoc)
081         * @see org.apache.oozie.command.XCommand#isLockRequired()
082         */
083        @Override
084        protected boolean isLockRequired() {
085            return false;
086        }
087    
088        /* (non-Javadoc)
089         * @see org.apache.oozie.command.XCommand#loadState()
090         */
091        @Override
092        protected void loadState() throws CommandException {
093            try {
094                jpaService = Services.get().get(JPAService.class);
095    
096                if (jpaService != null) {
097                    this.jobList = jpaService.execute(new CoordJobsGetForPurgeJPAExecutor(olderThan, limit));
098                }
099                else {
100                    throw new CommandException(ErrorCode.E0610);
101                }
102            }
103            catch (XException ex) {
104                throw new CommandException(ex);
105            }
106        }
107    
108        /* (non-Javadoc)
109         * @see org.apache.oozie.command.XCommand#verifyPrecondition()
110         */
111        @Override
112        protected void verifyPrecondition() throws CommandException, PreconditionException {
113        }
114    }