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";
43 if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdout, null))
46 FileStream fs = FileStream.fdopen(stdout, "r");
54 while ((line = fs.gets(buf)) != null)
56 if (!line[0].isdigit()) continue;
58 ulong id = line.to_ulong(out rest, 10);
60 rest = t.strptime(rest.offset(1), "%a %b %d %H:%M:%S %Y");
61 if (rest == null) continue;
62 time_t tt = t.mktime();
63 if (first_ts == 0 || tt < first_ts)
69 Process.close_pid(pid);
73 public delegate bool jobParser(int fd);
75 // Get the contents of a job given its id
76 public static bool jobContents(int id, jobParser parser)
79 argv[0] = "/usr/bin/at";
81 argv[2] = id.to_string();
87 if (!Process.spawn_async_with_pipes("/", argv, null, SpawnFlags.STDERR_TO_DEV_NULL, null, out pid, null, out stdoutfd, null))
90 bool res = parser(stdoutfd);
92 Process.close_pid(pid);
98 TODO: schedule alarms via at
102 atq -q z can be used to list the jobs (for example, at startup, or after a job has run)
103 at -c id can be used to query a job, parsing its contents (which can have
104 comments or variables being set)
105 zavai --notify ... can be used to notify the job (and start zavai if it's not running)
107 Alarm needs to be able to serialize itself to an at invocation and to
108 deserialize itself from the output of at -c
110 Alarm needs to deserialize also a job with no special markers whatsoever: a
116 public class Alarm : Object
118 // TODO: make a factory method to construct from an "at -c" output
120 // TODO: make a method that provides an at invocation
122 public signal void trigger(Alarm a);
124 public time_t deadline;
127 public Alarm(time_t deadline, string label)
129 this.deadline = deadline;
135 // Schedule a task that notifies a string
136 // TODO public void schedule_label(const Alarm a);
138 // Return the next item in the queue due to be run
139 // TODO public Alarm? next_scheduled();