/** * <p> * Builds a task list reading it from a reader. * </p> * * <p> * Syntax and semantics errors in the source reader are not blocking. * Invalid lines are discarded, and they cause just a stack trace to be * printed in the standard error channel as a notification. * </p> * * @param reader * The reader. * @return The task table parsed from the contents in the reader. * @throws IOException * I/O error. */ public static TaskTable parse(TextReader reader) { TaskTable table = new TaskTable(); try { string line; while ((line = reader.ReadLine()) != null) { try { parseLine(table, line); } catch (Exception e) { Console.WriteLine(e); continue; } } } finally { reader.Close(); } return(table); }
/** * Implements {@link TaskCollector#getTasks()}. */ public TaskTable GetTasks() { TaskTable ret = new TaskTable(); lock (files) { int size = files.Count; for (int i = 0; i < size; i++) { FileInfo f = (FileInfo)files[i]; TaskTable aux = null; try { aux = CronParser.parse(f); } catch (IOException e) { Exception e1 = new Exception("Cannot parse cron file: " + f.FullName, e); Console.WriteLine(e1.ToString()); } if (aux != null) { int auxSize = aux.Size(); for (int j = 0; j < auxSize; j++) { ret.Add(aux.GetSchedulingPattern(j), aux.GetTask(j)); } } } } return(ret); }
/** * Implements {@link TaskCollector#getTasks()}. */ public TaskTable GetTasks() { TaskTable ret = new TaskTable(); lock (patterns) { foreach (var item in patterns.Values) { ret.Add(item.Key, item.Value); } } return(ret); }
/** * Overrides {@link Thread#run()}. */ public void Run() { //outer: for (int i = 0; i < collectors.Length; i++) { TaskTable taskTable = collectors[i].GetTasks(); int size = taskTable.Size(); for (int j = 0; j < size; j++) { //if (cThread.isInterrupted()) { // goto outer; //} SchedulingPattern pattern = taskTable.GetSchedulingPattern(j); if (pattern.Match(scheduler.getTimeZone(), referenceTimeInMillis)) { Task task = taskTable.GetTask(j); scheduler.spawnExecutor(task); } } } // Notifies completed. scheduler.notifyLauncherCompleted(this); }
/** * Implements {@link TaskCollector#getTasks()}. */ public TaskTable GetTasks() { TaskTable ret = new TaskTable(); lock (files) { int size = files.Count; for (int i = 0; i < size; i++) { FileInfo f = (FileInfo)files[i]; TaskTable aux = null; try { aux = CronParser.parse(f); } catch (IOException e) { Exception e1 = new Exception("Cannot parse cron file: " + f.FullName, e); Console.WriteLine(e1.ToString()); } if (aux != null) { int auxSize = aux.Size(); for (int j = 0; j < auxSize; j++) { ret.Add(aux.GetSchedulingPattern(j), aux.GetTask(j)); } } } } return ret; }
/** * Parses a crontab-like line. * * @param table * The table on which the parsed task will be stored, by * side-effect. * @param line * The crontab-like line. * @throws Exception * The supplied line doesn't represent a valid task line. */ public static void parseLine(TaskTable table, string line) { if (string.IsNullOrEmpty(line)) return; line = line.Trim(); if (line.Length == 0 || line[0] == '#') { return; } // Detecting the pattern. int size = line.Length; string pattern = null; for (int i = size; i >= 0; i--) { string aux = line.Substring(0, i); if (SchedulingPattern.Validate(aux)) { pattern = aux; break; } } if (pattern == null) { throw new Exception("Invalid cron line: " + line); } line = line.Substring(pattern.Length); size = line.Length; // Splitting the line List<string> splitted = new List<string>(); StringBuilder current = null; bool quotes = false; for (int i = 0; i < size; i++) { char c = line[i]; if (current == null) { if (c == '"') { current = new StringBuilder(); quotes = true; } else if (c > ' ') { current = new StringBuilder(); current.Append(c); quotes = false; } } else { bool closeCurrent; if (quotes) { closeCurrent = (c == '"'); } else { closeCurrent = (c <= ' '); } if (closeCurrent) { if (current != null && current.Length > 0) { string str = current.ToString(); if (quotes) { str = escape(str); } splitted.Add(str); } current = null; } else { current.Append(c); } } } if (current != null && current.Length > 0) { string str = current.ToString(); if (quotes) { str = escape(str); } splitted.Add(str); current = null; } // Analyzing size = splitted.Count; int status = 0; // Status values: // 0 -> fetching environment variables, working directory and channels // 1 -> fetching the command and its arguments string dirString = null; FileInfo stdinFile = null; FileInfo stdoutFile = null; FileInfo stderrFile = null; List<string> envsList = new List<string>(); string command = null; List<string> argsList = new List<string>(); for (int i = 0; i < size; i++) { string tk = (string)splitted[i]; // Check the local status. if (status == 0) { // Environment variables, working directory and channels if (tk.StartsWith("ENV:")) { envsList.Add(tk.Substring(4)); continue; } else if (tk.StartsWith("DIR:")) { dirString = tk.Substring(4); continue; } else if (tk.StartsWith("IN:")) { stdinFile = new FileInfo(tk.Substring(3)); continue; } else if (tk.StartsWith("OUT:")) { stdoutFile = new FileInfo(tk.Substring(4)); continue; } else if (tk.StartsWith("ERR:")) { stderrFile = new FileInfo(tk.Substring(4)); continue; } else { status = 1; } } if (status == 1) { // Command or argument? if (command == null) { command = tk; } else { argsList.Add(tk); } } } // Task preparing. Task task = null; // Command evaluation. if (command == null) { // No command! throw new Exception("Invalid cron line: " + line); } else if (command.StartsWith("dotnet:")) //INFO in cron4net use dotnet mark .NET Type { // Java inner-process. string className = command.Substring(7); if (className.Length == 0) { throw new Exception("Invalid .NET type name on line: " + line); } string methodName; int sep = className.IndexOf('#'); if (sep == -1) { methodName = "Main"; } else { methodName = className.Substring(sep + 1); className = className.Substring(0, sep); if (methodName.Length == 0) { throw new Exception("Invalid .NET method name on line: " + line); } } string[] args = argsList.ToArray(); task = new StaticMethodTask(className, methodName, args); } else { // External command. string[] cmdarray = new string[1 + argsList.Count]; cmdarray[0] = command; for (int i = 0; i < argsList.Count; i++) { cmdarray[i + 1] = argsList[i]; } // Environments. string[] envs = null; size = envsList.Count; if (size > 0) { envs = envsList.ToArray(); } // Working directory. DirectoryInfo dir = null; if (dirString != null) { dir = new DirectoryInfo(dirString); if (!dir.Exists) { throw new Exception( "Invalid cron working directory parameter at line: " + line, new FileNotFoundException(dirString + " doesn't exist or it is not a directory")); } } //TODO ProcessTask //// Builds the task. //ProcessTask process = new ProcessTask(cmdarray, envs, dir); //// Channels. //if (stdinFile != null) //{ // process.setStdinFile(stdinFile); //} //if (stdoutFile != null) //{ // process.setStdoutFile(stdoutFile); //} //if (stderrFile != null) //{ // process.setStderrFile(stderrFile); //} //task = process; } // End. table.Add(new SchedulingPattern(pattern), task); }
/** * <p> * Builds a task list reading it from a reader. * </p> * * <p> * Syntax and semantics errors in the source reader are not blocking. * Invalid lines are discarded, and they cause just a stack trace to be * printed in the standard error channel as a notification. * </p> * * @param reader * The reader. * @return The task table parsed from the contents in the reader. * @throws IOException * I/O error. */ public static TaskTable parse(TextReader reader) { TaskTable table = new TaskTable(); try { string line; while ((line = reader.ReadLine()) != null) { try { parseLine(table, line); } catch (Exception e) { Console.WriteLine(e); continue; } } } finally { reader.Close(); } return table; }
/** * Parses a crontab-like line. * * @param table * The table on which the parsed task will be stored, by * side-effect. * @param line * The crontab-like line. * @throws Exception * The supplied line doesn't represent a valid task line. */ public static void parseLine(TaskTable table, string line) { if (string.IsNullOrEmpty(line)) { return; } line = line.Trim(); if (line.Length == 0 || line[0] == '#') { return; } // Detecting the pattern. int size = line.Length; string pattern = null; for (int i = size; i >= 0; i--) { string aux = line.Substring(0, i); if (SchedulingPattern.Validate(aux)) { pattern = aux; break; } } if (pattern == null) { throw new Exception("Invalid cron line: " + line); } line = line.Substring(pattern.Length); size = line.Length; // Splitting the line List <string> splitted = new List <string>(); StringBuilder current = null; bool quotes = false; for (int i = 0; i < size; i++) { char c = line[i]; if (current == null) { if (c == '"') { current = new StringBuilder(); quotes = true; } else if (c > ' ') { current = new StringBuilder(); current.Append(c); quotes = false; } } else { bool closeCurrent; if (quotes) { closeCurrent = (c == '"'); } else { closeCurrent = (c <= ' '); } if (closeCurrent) { if (current != null && current.Length > 0) { string str = current.ToString(); if (quotes) { str = escape(str); } splitted.Add(str); } current = null; } else { current.Append(c); } } } if (current != null && current.Length > 0) { string str = current.ToString(); if (quotes) { str = escape(str); } splitted.Add(str); current = null; } // Analyzing size = splitted.Count; int status = 0; // Status values: // 0 -> fetching environment variables, working directory and channels // 1 -> fetching the command and its arguments string dirString = null; FileInfo stdinFile = null; FileInfo stdoutFile = null; FileInfo stderrFile = null; List <string> envsList = new List <string>(); string command = null; List <string> argsList = new List <string>(); for (int i = 0; i < size; i++) { string tk = (string)splitted[i]; // Check the local status. if (status == 0) { // Environment variables, working directory and channels if (tk.StartsWith("ENV:")) { envsList.Add(tk.Substring(4)); continue; } else if (tk.StartsWith("DIR:")) { dirString = tk.Substring(4); continue; } else if (tk.StartsWith("IN:")) { stdinFile = new FileInfo(tk.Substring(3)); continue; } else if (tk.StartsWith("OUT:")) { stdoutFile = new FileInfo(tk.Substring(4)); continue; } else if (tk.StartsWith("ERR:")) { stderrFile = new FileInfo(tk.Substring(4)); continue; } else { status = 1; } } if (status == 1) { // Command or argument? if (command == null) { command = tk; } else { argsList.Add(tk); } } } // Task preparing. Task task = null; // Command evaluation. if (command == null) { // No command! throw new Exception("Invalid cron line: " + line); } else if (command.StartsWith("dotnet:")) //INFO in cron4net use dotnet mark .NET Type { // Java inner-process. string className = command.Substring(7); if (className.Length == 0) { throw new Exception("Invalid .NET type name on line: " + line); } string methodName; int sep = className.IndexOf('#'); if (sep == -1) { methodName = "Main"; } else { methodName = className.Substring(sep + 1); className = className.Substring(0, sep); if (methodName.Length == 0) { throw new Exception("Invalid .NET method name on line: " + line); } } string[] args = argsList.ToArray(); task = new StaticMethodTask(className, methodName, args); } else { // External command. string[] cmdarray = new string[1 + argsList.Count]; cmdarray[0] = command; for (int i = 0; i < argsList.Count; i++) { cmdarray[i + 1] = argsList[i]; } // Environments. string[] envs = null; size = envsList.Count; if (size > 0) { envs = envsList.ToArray(); } // Working directory. DirectoryInfo dir = null; if (dirString != null) { dir = new DirectoryInfo(dirString); if (!dir.Exists) { throw new Exception( "Invalid cron working directory parameter at line: " + line, new FileNotFoundException(dirString + " doesn't exist or it is not a directory")); } } //TODO ProcessTask //// Builds the task. //ProcessTask process = new ProcessTask(cmdarray, envs, dir); //// Channels. //if (stdinFile != null) //{ // process.setStdinFile(stdinFile); //} //if (stdoutFile != null) //{ // process.setStdoutFile(stdoutFile); //} //if (stderrFile != null) //{ // process.setStderrFile(stderrFile); //} //task = process; } // End. table.Add(new SchedulingPattern(pattern), task); }