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