package org.ngbw.utils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.ngbw.sdk.Workbench; import org.ngbw.sdk.database.RunningTask; import org.ngbw.sdk.database.Task; import org.ngbw.sdk.tool.BaseProcessWorker; /** Stand alone program to retrieve the results for a single task using information in the running task table. */ public class StoreRTaskOutput { private static final Log log = LogFactory.getLog(StoreRTaskOutput.class.getName()); public static void main(String[] args) { try { Workbench.getInstance(); if (args.length != 1) throw new Exception("usage: StoreRTaskOutput running_task_id"); long running_task_id = new Long(args[0]); System.out.printf("running_task_id = %d\n", running_task_id); processTask(running_task_id, false, null); } catch(Exception e) { e.printStackTrace(); } } static void processTask(long running_task_id, boolean recover, String localPath) throws Exception { log.debug("Processing running_task_id " + running_task_id); try { // Getting processworker will throw an exception if the task has been deleted. // Or, getResults will too if it gets deleted after process worker is created. BaseProcessWorker pw = BaseProcessWorker.getProcessWorker(running_task_id); if (!pw.findWorkingDir(localPath)) { log.error("ProcessWorker failed to load results because job's working dir wasn't found."); } else { pw.getResults(); log.debug("Results retrieved."); } } catch(Exception e) { // BaseProcessWorker.getProcessWorker will throw if task has been deleted. if (handleMissingTask(running_task_id)) { log.debug("Task was deleted."); return; } else { log.error("ProcessWorker failed to load results", e); } } } static boolean handleMissingTask(long running_task_id) throws Exception { boolean taskMissing = false; RunningTask rt = new RunningTask(running_task_id); try { new Task(rt.getTaskId()); } catch (Exception e) { taskMissing = true; } if (!taskMissing) { return false; } // task is missing. Update the running_task_record. if (!RunningTask.changeStatus(running_task_id, RunningTask.STATUS_TASK_DELETED)) { log.error("RunningTask changeStatus denied for " + running_task_id + ", must already be DONE or have results stored."); } return true; } }