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.io.IOException; 018 import java.io.StringReader; 019 import java.net.URI; 020 import java.net.URISyntaxException; 021 022 import org.apache.hadoop.conf.Configuration; 023 import org.apache.hadoop.fs.FileSystem; 024 import org.apache.hadoop.fs.Path; 025 import org.apache.oozie.ErrorCode; 026 import org.apache.oozie.client.WorkflowJob; 027 import org.apache.oozie.command.CommandException; 028 import org.apache.oozie.command.PreconditionException; 029 import org.apache.oozie.service.HadoopAccessorException; 030 import org.apache.oozie.service.HadoopAccessorService; 031 import org.apache.oozie.service.Services; 032 import org.apache.oozie.util.XConfiguration; 033 034 /** 035 * This Command is expected to be called when a Workflow moves to any terminal 036 * state ( such as SUCCEEDED, KILLED, FAILED). This class primarily removes the 037 * temporary directory created for specific workflow id 038 */ 039 public class WfEndXCommand extends WorkflowXCommand<Void> { 040 041 private WorkflowJob job = null; 042 043 public WfEndXCommand(WorkflowJob job) { 044 super("wf_end", "wf_end", 1); 045 this.job = job; 046 } 047 048 @Override 049 protected Void execute() throws CommandException { 050 LOG.debug("STARTED WFEndXCommand " + job.getId()); 051 deleteWFDir(); 052 LOG.debug("ENDED WFEndXCommand " + job.getId()); 053 return null; 054 } 055 056 private void deleteWFDir() throws CommandException { 057 FileSystem fs; 058 try { 059 fs = getAppFileSystem(job); 060 String wfDir = Services.get().getSystemId() + "/" + job.getId(); 061 Path wfDirPath = new Path(fs.getHomeDirectory(), wfDir); 062 LOG.debug("WF tmp dir :" + wfDirPath); 063 if (fs.exists(wfDirPath)) { 064 fs.delete(wfDirPath, true); 065 } 066 else { 067 LOG.debug("Tmp dir doesn't exist :" + wfDirPath); 068 } 069 } 070 catch (Exception e) { 071 LOG.error("Unable to delete WF temp dir of wf id :" + job.getId(), e); 072 throw new CommandException(ErrorCode.E0819); 073 } 074 075 } 076 077 protected FileSystem getAppFileSystem(WorkflowJob workflow) throws HadoopAccessorException, IOException, 078 URISyntaxException { 079 XConfiguration jobConf = new XConfiguration(new StringReader(workflow.getConf())); 080 Configuration fsConf = new Configuration(); 081 XConfiguration.copy(jobConf, fsConf); 082 return Services.get().get(HadoopAccessorService.class).createFileSystem(workflow.getUser(), 083 workflow.getGroup(), new URI(workflow.getAppPath()), fsConf); 084 } 085 086 @Override 087 protected String getEntityKey() { 088 return job.getId(); 089 } 090 091 @Override 092 protected boolean isLockRequired() { 093 return false; 094 } 095 096 @Override 097 protected void loadState() throws CommandException { 098 099 } 100 101 @Override 102 protected void verifyPrecondition() throws CommandException, PreconditionException { 103 104 } 105 106 }