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