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.wf; 016 017 import java.util.List; 018 019 import org.apache.oozie.ErrorCode; 020 import org.apache.oozie.WorkflowJobBean; 021 import org.apache.oozie.XException; 022 import org.apache.oozie.service.JPAService; 023 import org.apache.oozie.service.Services; 024 import org.apache.oozie.command.CommandException; 025 import org.apache.oozie.command.PreconditionException; 026 import org.apache.oozie.executor.jpa.JPAExecutorException; 027 import org.apache.oozie.executor.jpa.WorkflowActionsDeleteForPurgeJPAExecutor; 028 import org.apache.oozie.executor.jpa.WorkflowJobDeleteJPAExecutor; 029 import org.apache.oozie.executor.jpa.WorkflowJobsGetForPurgeJPAExecutor; 030 031 public class PurgeXCommand extends WorkflowXCommand<Void> { 032 private JPAService jpaService = null; 033 private int olderThan; 034 private int limit; 035 private List<WorkflowJobBean> jobList = null; 036 037 public PurgeXCommand(int olderThan, int limit) { 038 super("purge", "purge", 0); 039 this.olderThan = olderThan; 040 this.limit = limit; 041 } 042 043 @Override 044 protected Void execute() throws CommandException { 045 LOG.debug("STARTED Workflow-Purge Attempting to purge Jobs older than [{0}] days.", olderThan); 046 047 int actionDeleted = 0; 048 if (jobList != null && jobList.size() != 0) { 049 for (WorkflowJobBean w : jobList) { 050 String wfId = w.getId(); 051 try { 052 jpaService.execute(new WorkflowJobDeleteJPAExecutor(wfId)); 053 actionDeleted += jpaService.execute(new WorkflowActionsDeleteForPurgeJPAExecutor(wfId)); 054 } 055 catch (JPAExecutorException e) { 056 throw new CommandException(e); 057 } 058 } 059 LOG.debug("ENDED Workflow-Purge deleted jobs :" + jobList.size() + " and actions " + actionDeleted); 060 } 061 else { 062 LOG.debug("ENDED Workflow-Purge no workflow job to be deleted"); 063 } 064 return null; 065 } 066 067 @Override 068 protected String getEntityKey() { 069 return null; 070 } 071 072 @Override 073 protected boolean isLockRequired() { 074 return false; 075 } 076 077 @Override 078 protected void loadState() throws CommandException { 079 try { 080 jpaService = Services.get().get(JPAService.class); 081 082 if (jpaService != null) { 083 this.jobList = jpaService.execute(new WorkflowJobsGetForPurgeJPAExecutor(olderThan, limit)); 084 } 085 else { 086 throw new CommandException(ErrorCode.E0610); 087 } 088 } 089 catch (XException ex) { 090 throw new CommandException(ex); 091 } 092 093 } 094 095 @Override 096 protected void verifyPrecondition() throws CommandException, PreconditionException { 097 } 098 099 }