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)
Pid pid;
int stdout;
- if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
- return -1;
+ try
+ {
+ if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
+ return res;
+ } catch (SpawnError e) {
+ stderr.printf("Cannot run 'at -q': %s\n", e.message);
+ 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)
if (!line[0].isdigit()) continue;
weak string rest;
ulong id = line.to_ulong(out rest, 10);
- Time t = new Time();
+ 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);
Pid pid;
int stdoutfd;
- if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdoutfd, null))
+ try
+ {
+ if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdoutfd, null))
+ return false;
+ } catch (SpawnError e) {
+ stderr.printf("Cannot run 'at -c': %s\n", e.message);
return false;
+ }
bool res = parser(stdoutfd);