package com.hubspot.singularity.executor.task;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.slf4j.Logger;

import com.google.common.collect.ImmutableList;
import com.hubspot.singularity.executor.config.SingularityExecutorConfiguration;
import com.hubspot.singularity.s3.base.SimpleProcessManager;

public class SingularityExecutorTaskCleanup extends SimpleProcessManager {

  private final SingularityExecutorTaskDefinition taskDefinition;
  private final SingularityExecutorTaskLogManager taskLogManager;
  private final SingularityExecutorConfiguration configuration;
  private final Logger log;

  public SingularityExecutorTaskCleanup(SingularityExecutorTaskLogManager taskLogManager, SingularityExecutorConfiguration configuration, SingularityExecutorTaskDefinition taskDefinition, Logger log) {
    super(log);
    this.configuration = configuration;
    this.taskLogManager = taskLogManager;
    this.taskDefinition = taskDefinition;
    this.log = log;
  }

  public boolean cleanup() {
    boolean logTearDownSuccess = taskLogManager.teardown();
    boolean cleanupTaskAppDirectorySuccess = cleanupTaskAppDirectory();

    log.info("Cleaned up logs ({}) and task app directory ({})", logTearDownSuccess, cleanupTaskAppDirectorySuccess);

    if (logTearDownSuccess && cleanupTaskAppDirectorySuccess) {
      Path taskDefinitionPath = configuration.getTaskDefinitionPath(taskDefinition.getTaskId());

      log.info("Successfull cleanup, deleting file {}", taskDefinitionPath);

      try {
        boolean deleted = Files.deleteIfExists(taskDefinitionPath);

        log.info("File deleted ({})", deleted);

        return true;
      } catch (IOException e) {
        log.error("Failed deleting {}", taskDefinitionPath, e);
        return false;
      }
    }

    return false;
  }

  private boolean cleanupTaskAppDirectory() {
    final String pathToDelete = taskDefinition.getTaskAppDirectory();

    log.info("Deleting: {}", pathToDelete);

    try {
      final List<String> cmd = ImmutableList.of(
          "rm",
          "-rf",
          pathToDelete
      );

      super.runCommand(cmd);
      return true;
    } catch (Throwable t) {
      log.error("While deleting directory {}", pathToDelete, t);
    }

    return false;
  }

}

@@ -124,16 +124,44 @@
 e.Path;%0A
+import java.nio.file.Paths;%0A
 import j
@@ -1086,24 +1086,269 @@
 cleanup() %7B%0A
+    final Path taskDirectory = Paths.get(taskDefinition.getTaskDirectory());%0A    %0A    if (!Files.exists(taskDirectory)) %7B%0A      log.info(%22Directory %7B%7D didn't exist for cleanup%22, taskDirectory);%0A      return cleanTaskDefinitionFile();%0A    %7D%0A    %0A
     boolean 
@@ -1649,24 +1649,139 @@
 uccess) %7B%0A  
+    return cleanTaskDefinitionFile();%0A    %7D%0A%0A    return false;%0A  %7D%0A  %0A  public boolean cleanTaskDefinitionFile() %7B%0A
     Path tas
@@ -1864,26 +1864,24 @@
 Id());%0A%0A    
-  
 log.info(%22Su
@@ -1936,26 +1936,24 @@
 tionPath);%0A%0A
-  
     try %7B%0A  
@@ -1956,18 +1956,16 @@
 %7B%0A      
-  
 boolean 
@@ -2019,26 +2019,24 @@
 th);%0A%0A      
-  
 log.info(%22Fi
@@ -2066,26 +2066,24 @@
 ed);%0A%0A      
- 
- 
 return true;
@@ -2083,18 +2083,16 @@
 n true;%0A
-  
     %7D ca
@@ -2109,26 +2109,24 @@
 eption e) %7B%0A
-  
       log.er
@@ -2177,26 +2177,24 @@
 , e);%0A      
-  
 return false
@@ -2203,36 +2203,9 @@
     
-  %7D%0A    %7D%0A%0A    return false;
+%7D
 %0A  %7D

handle common error case