package Sight.util;
import Sight.Agents.util.Pind;
import Sight.io.Output;
import java.io.*;
import java.util.*;
public class ListProcessor implements Runnable {
public static ListProcessor instance = null;
public ArrayList list;
public int cursor = 0;
public ListProcessor()
{
if (instance!=null)
throw new java.lang.UnsupportedOperationException(
"Only one instance of ListProcessor is allowed in this Sight version.");
instance = this;
};
public void setList(Reader r) throws java.io.IOException
{
setList(new BufferedReader(r));
};
public void setList(File r) throws FileNotFoundException, java.io.IOException
{
setList(new BufferedReader(new FileReader(r)));
};
public void setList(BufferedReader r)
throws java.io.IOException
{
reader = r;
BufferedReader rr = null;
try {
rr = Sight.Log.LogFileOpener.OpenRead(this.getBackupFileName());
} catch (Exception exc) { rr = null; };
if (rr==null)
{ System.out.println("Cold start.");
this.setLastDone(null);
do_on_start();
};
String skip = null;
if (rr!=null)
{ skip = rr.readLine().trim();
rr.close();
if (skip.equalsIgnoreCase("task_finished"))
{ System.out.println("Task finished, delete the file "+
this.getBackupFileName()+".log to re-caclulate.");
finish();
};
if (!skip.equalsIgnoreCase("null"))
{
System.out.println("Hot restart from "+skip);
do_on_hot_start();
}
else
{
System.out.println("Hot restart information was not saved, performing cold start.");
skip = null;
};
this.setLastDone(skip);
PrintStream restarts = Sight.Log.LogFileOpener.Append(this.getRestartsLogFile());
restarts.println("after "+skip+" at "+new java.util.Date().toString());
restarts.close();
};
};
void loadList() throws java.io.IOException
{
if (reader==null)
throw new Error("Call setList(...) before calling loadList()");
String skip = this.getLastDone();
String s;
list = new java.util.ArrayList(2000);
while ( (s=reader.readLine() ) != null) { s=transformTaskIdentifier(s); if (s.length()==0) continue;
if (!list.contains(s)) list.add(s);
};
if (skip==null) cursor = 0; else
{ cursor = -1;
for (int i = 0; i < list.size(); i++) {
if ( ((String)list.get(i)).equalsIgnoreCase(skip))
{
cursor = i;
break;
};
}
if (cursor<0)
throw new Error("Id "+skip+" was not found in the task list of size "+list.size());
cursor++;
};
Sight.Agents.util.Pind.setBatch(list);
};
private int totalFailExitCode = 99;;
private int allFinishedExitCode = 0;
private int restartExitCode = 55;
private int totalStopExitCode = 54;
private String backupFileName ="hot";
private boolean terminating;
private String LastDone;
private String restartsLogFile = "Restarts";
private boolean paused;
private BufferedReader reader;
public void setTotalFailExitCode(int newTotalFailExitCode) {
totalFailExitCode = newTotalFailExitCode;
}
public int getTotalFailExitCode() {
return totalFailExitCode;
}
public void setAllFinishedExitCode(int newAllFinishedExitCode) {
allFinishedExitCode = newAllFinishedExitCode;
}
public int getAllFinishedExitCode() {
return allFinishedExitCode;
}
public void setRestartExitCode(int newRestartExitCode) {
restartExitCode = newRestartExitCode;
}
public int getRestartExitCode() {
return restartExitCode;
}
public void setBackupFileName(String newBackupFileName) {
backupFileName = newBackupFileName;
}
public String getBackupFileName() {
return backupFileName;
}
public void setTerminating(boolean newTerminating) {
terminating = newTerminating;
Sight.Agents.util.Pind.setRegisterExit(newTerminating);
}
public boolean isTerminating() {
return terminating || Sight.Agents.util.Pind.registerExit;
}
public void settotalStopExitCode(int newtotalStopExitCode) {
totalStopExitCode = newtotalStopExitCode;
}
public int gettotalStopExitCode() {
return totalStopExitCode;
}
public void setLastDone(String newLastDone) {
LastDone = newLastDone;
}
public String getLastDone() {
return LastDone;
}
void finish()
{ balastas = null; System.gc(); Runtime.getRuntime().gc();
PrintStream r = Sight.Log.LogFileOpener.Open(this.getBackupFileName());
r.println("task_finished");
r.close();
do_on_finish();
System.exit(this.getAllFinishedExitCode()); }
void restart(int exitCode, Object reason)
{
PrintStream restarts = Sight.Log.LogFileOpener.Append(this.getRestartsLogFile());
restarts.println("Terminating at "+this.getLastDone()+" code "+
this.getRestartExitCode()+" at "+new java.util.Date().toString());
restarts.println("Reason:");
if (reason!=null)
{
if (reason instanceof Throwable)
((Throwable) reason).printStackTrace(restarts);
else restarts.println(reason.toString());
};
restarts.close();
PrintStream hts = Sight.Log.LogFileOpener.Open(this.getBackupFileName());
hts.println(this.getLastDone());
hts.close();
System.exit(exitCode); }
public void setRestartsLogFile(String newRestartsLogFile) {
restartsLogFile = newRestartsLogFile;
}
public String getRestartsLogFile() {
return restartsLogFile;
}
public String transformTaskIdentifier(String id)
{
return id;
};
static byte []balastas = new byte[8000000];
private String doneLogFileName = "done";
private String bugFileName = "not_done";
public void run()
{ String current;
if (reader==null)
throw new Error("Call setList(...) before calling loadList()");
try {
loadList();
for (cursor=cursor; cursor<list.size(); cursor++)
{
current = (String) list.get(cursor);
current=transformTaskIdentifier(current); if (current.length()==0) continue;
try {
process(current, cursor);
done(current);
this.setLastDone(current);
} catch (java.lang.OutOfMemoryError err)
{
balastas = null;
list = null;
System.gc();
Runtime.getRuntime().gc();
restart(this.getRestartExitCode(), err);
}
catch (Exception err)
{
balastas = null;
System.gc();
Runtime.getRuntime().gc();
restart(this.getRestartExitCode(), err);
};
LastDone = current;
if (this.isTerminating()) restart(this.gettotalStopExitCode(), null);
while (isPaused())
{ try { Sleeper.Sleep(1000);
if (this.isTerminating()) restart(this.gettotalStopExitCode(), null);
} catch (InterruptedException ex) { } };
}
}
catch (Exception io)
{ restart(this.getTotalFailExitCode(), io); };
instance = null;
reader = null;
done("Task completed at "+new java.util.Date().toString());
finish();
};
public void setPaused(boolean newPaused) {
paused = newPaused;
}
public boolean isPaused() {
return paused;
}
public void process(String current, int cur) throws Exception, Error
{
try {
Pind.Batch(cur, Pind.PROCESSING);
process(current);
Pind.Batch(cur, Pind.CHAIN_OK);
} catch (Exception exc)
{
Output.e(exc);
Pind.Batch(cur, Pind.BUG);
bug(current, exc);
};
};
public void process(String id) throws Exception, Error
{
try {
Sleeper.Sleep(1000);
}
catch (InterruptedException ex) {
}
System.out.println(id+" processed");
Sight.io.Output.a(id+" processed");
if (Math.random()<0.05)
throw new java.lang.OutOfMemoryError("Demo out of memory error while processing "+id);
if (Math.random()<0.12)
throw new java.lang.Exception("Demo exception from the default method while processing "+id);
};
static public void main(String[] args) {
try {
StringBuffer b = new StringBuffer();
for (int i = 0; i < 100; i++) {
b.append("task_"+i+"\n");
}
StringReader s = new StringReader(b.toString());
ListProcessor lp = new ListProcessor();
lp.setList(s);
lp.run();
} catch (Exception exc)
{ if (exc!=null) System.out.println(exc.getMessage());
exc.printStackTrace();
};
}
void done(String id)
{
PrintStream lg = Sight.Log.LogFileOpener.Append(this.getdoneLogFileName());
lg.println(id);
lg.close();
};
void bug(String id, Throwable reason)
{ PrintStream lg = Sight.Log.LogFileOpener.Append(this.getBugFileName());
lg.println(id);
lg.close();
};
public void setdoneLogFileName(String newdoneLogFileName) {
doneLogFileName = newdoneLogFileName;
}
public String getdoneLogFileName() {
return doneLogFileName;
}
public void setBugFileName(String newBugFileName) {
bugFileName = newBugFileName;
}
public String getBugFileName() {
return bugFileName;
}
public void do_on_start()
{
};
public void do_on_finish()
{
};
public void do_on_hot_start()
{
};
}