package us.kbase.narrativejobservice;

import com.mongodb.BulkWriteResult;
import org.ini4j.Ini;
import us.kbase.narrativejobservice.test.TesterUtils;
import us.kbase.narrativejobservice.ReaperService;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import java.io.File;



public class ReaperServiceServlet implements ServletContextListener {

    private Thread myThread = null;
    static Map<String, String> config;


    private ReaperService getReaperService() throws Exception {
        Ini config = new Ini(new File(System.getenv("KB_DEPLOYMENT_CONFIG")));
        String host = config.get("NarrativeJobService","ujs-mongodb-host");
        String dbName = config.get("NarrativeJobService","ujs-mongodb-database");
        String user = config.get("NarrativeJobService","ujs-mongodb-user");
        String pwd = config.get("NarrativeJobService", "ujs-mongodb-pwd");
        return new ReaperService(user, pwd, host, dbName);
    }

    public void contextInitialized(ServletContextEvent sce) {

        if ((myThread == null) || (!myThread.isAlive())) {
            final File file = new File("reaper.log");
            Thread myThread = new Thread(new Runnable() {
                public void run() {
                    try {
                        ReaperService r = getReaperService();

                        while (true) {
                            String time = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + "\n";
                            FileUtils.writeStringToFile(file, "RUNNING REAPER at " + time,true);
                            BulkWriteResult result = r.purgeGhostJobs();
                            if (result != null)
                                System.out.println(result);
                            else {
                                System.out.println("No Jobs To Purge.");
                            }
                            //30 Minutes Before Each Run
                            Thread.sleep(1000 * 60 * 30);
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            myThread.isDaemon();
          myThread.start();
        }
        else{
            System.out.println("FAILED TO RUN REAPER");
        }
    }

    public void contextDestroyed(ServletContextEvent sce) {
        try {
            myThread.interrupt();
        } catch (Exception ex) {
        }
    }
}



@@ -83,122 +83,57 @@
 org.
-ini4j.Ini;%0Aimport us.kbase.narrativejobservice.test.TesterUtils;%0Aimport us.kbase.narrativejobservice.ReaperService
+apache.commons.io.FileUtils;%0Aimport org.ini4j.Ini
 ;%0A%0Ai
@@ -218,16 +218,37 @@
 stener;%0A
+import java.io.File;%0A
 import j
@@ -323,70 +323,8 @@
 ap;%0A
-import org.apache.commons.io.FileUtils;%0Aimport java.io.File;%0A%0A
 %0A%0Apu
@@ -659,32 +659,33 @@
 tiveJobService%22,
+ 
 %22ujs-mongodb-hos
@@ -738,32 +738,33 @@
 tiveJobService%22,
+ 
 %22ujs-mongodb-dat
@@ -827,16 +827,17 @@
 ervice%22,
+ 
 %22ujs-mon
@@ -1162,24 +1162,86 @@
 aper.log%22);%0A
+            final File error_file = new File(%22reaper.error%22);%0A
             
@@ -1653,16 +1653,17 @@
  + time,
+ 
 true);%0A 
@@ -1781,16 +1781,110 @@
 != null)
+ %7B%0A                                FileUtils.writeStringToFile(file, result.toString(), true);
 %0A       
@@ -1959,24 +1959,26 @@
             
+ %7D
  else %7B%0A    
@@ -2005,35 +2005,50 @@
         
-System.out.println(
+FileUtils.writeStringToFile(file, 
 %22No Jobs
@@ -2058,16 +2058,22 @@
  Purge.%22
+, true
 );%0A     
@@ -2324,24 +2324,327 @@
 ackTrace();%0A
+                        try %7B%0A                            FileUtils.writeStringToFile(error_file, e.toString(), true);%0A                            FileUtils.writeStringToFile(error_file, e.getStackTrace().toString(), true);%0A                        %7D catch (Exception ignore) %7B%0A                        %7D%0A
             
@@ -2722,32 +2722,34 @@
 on();%0A          
+  
 myThread.start()
@@ -2763,21 +2763,14 @@
    %7D
-%0A       
  else
+ 
 %7B%0A  
@@ -2998,11 +2998,8 @@
 %0A    %7D%0A%7D
-%0A%0A%0A

Add Reaper Error Logging and format cleanup