namespace at {
+public struct Event
+{
+ int id;
+ time_t deadline;
+}
+
// Return the earliest ID in the queue, or -1 if none are found
// Queue is null for all queues, otherwise a queue name
-public static int earliestID(string? queue = null)
+public static Event earliestID(string? queue = null)
{
+ Event res = { -1, 0 };
+
string argv[4];
argv[0] = "/usr/bin/atq";
if (queue == null)
try
{
if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
- return -1;
+ return res;
} catch (SpawnError e) {
stderr.printf("Cannot run 'at -q': %s\n", e.message);
- return -1;
+ return res;
}
FileStream fs = FileStream.fdopen(stdout, "r");
if (fs == null)
- return -1;
+ return res;
- int first_id = -1;
- time_t first_ts = 0;
char buf[200];
string? line;
while ((line = fs.gets(buf)) != null)
Time t = Time();
rest = t.strptime(rest.offset(1), "%a %b %d %H:%M:%S %Y");
if (rest == null) continue;
+ if (rest.size() < 2) continue;
+ //stderr.printf("PARSE QUEUE rest %s\n", rest);
+ // Skip the queue of tasks currently being executed
+ //if (rest[1] == '=') continue;
+ // Skip entries not in the wanted queue
+ if (queue != null && rest[1] != queue[0]) continue;
time_t tt = t.mktime();
- if (first_ts == 0 || tt < first_ts)
- {
- first_ts = tt;
- first_id = (int)id;
+ if (res.deadline == 0 || tt < res.deadline) {
+ res.id = (int)id;
+ res.deadline = tt;
}
}
Process.close_pid(pid);
- return first_id;
+ return res;
}
public delegate bool jobParser(int fd);