X-Git-Url: https://git.toastfreeware.priv.at/gregoa/zavai.git/blobdiff_plain/0d1445c62994145f78d086d08aebe00f92303778..ebe578b1c360938144b6083ef032f3b612abe9b0:/src/at.vala diff --git a/src/at.vala b/src/at.vala index 0eb17bb..33cdf27 100644 --- a/src/at.vala +++ b/src/at.vala @@ -22,10 +22,18 @@ using GLib; 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) @@ -43,18 +51,16 @@ public static int earliestID(string? 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) @@ -65,15 +71,20 @@ public static int earliestID(string? queue = 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);