4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 // Return the earliest ID in the queue, or -1 if none are found
26 // Queue is null for all queues, otherwise a queue name
27 public static int earliestID(string? queue = null)
30 argv[0] = "/usr/bin/atq";
45 if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
47 } catch (SpawnError e) {
48 stderr.printf("Cannot run 'at -q': %s\n", e.message);
52 FileStream fs = FileStream.fdopen(stdout, "r");
60 while ((line = fs.gets(buf)) != null)
62 if (!line[0].isdigit()) continue;
64 ulong id = line.to_ulong(out rest, 10);
66 rest = t.strptime(rest.offset(1), "%a %b %d %H:%M:%S %Y");
67 if (rest == null) continue;
68 time_t tt = t.mktime();
69 if (first_ts == 0 || tt < first_ts)
75 Process.close_pid(pid);
79 public delegate bool jobParser(int fd);
81 // Get the contents of a job given its id
82 public static bool jobContents(int id, jobParser parser)
85 argv[0] = "/usr/bin/at";
87 argv[2] = id.to_string();
95 if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdoutfd, null))
97 } catch (SpawnError e) {
98 stderr.printf("Cannot run 'at -c': %s\n", e.message);
102 bool res = parser(stdoutfd);
104 Process.close_pid(pid);
110 TODO: schedule alarms via at
114 atq -q z can be used to list the jobs (for example, at startup, or after a job has run)
115 at -c id can be used to query a job, parsing its contents (which can have
116 comments or variables being set)
117 zavai --notify ... can be used to notify the job (and start zavai if it's not running)
119 Alarm needs to be able to serialize itself to an at invocation and to
120 deserialize itself from the output of at -c
122 Alarm needs to deserialize also a job with no special markers whatsoever: a
128 public class Alarm : Object
130 // TODO: make a factory method to construct from an "at -c" output
132 // TODO: make a method that provides an at invocation
134 public signal void trigger(Alarm a);
136 public time_t deadline;
139 public Alarm(time_t deadline, string label)
141 this.deadline = deadline;
147 // Schedule a task that notifies a string
148 // TODO public void schedule_label(const Alarm a);
150 // Return the next item in the queue due to be run
151 // TODO public Alarm? next_scheduled();