Marcus has proposed merging lp:~daniel-sonck/upstart/cron-replacement into 
lp:upstart.

Requested reviews:
  Scott James Remnant (scott)

For more details, see:
https://code.launchpad.net/~daniel-sonck/upstart/cron-replacement/+merge/118942

Add cron support soon!
-- 
https://code.launchpad.net/~daniel-sonck/upstart/cron-replacement/+merge/118942
Your team Upstart Reviewers is subscribed to branch lp:upstart.
=== modified file 'init/events.h'
--- init/events.h	2009-06-23 09:29:35 +0000
+++ init/events.h	2012-08-09 12:39:33 +0000
@@ -93,5 +93,12 @@
  **/
 #define JOB_STOPPED_EVENT "stopped"
 
+/**
+ * TIME_EVENT:
+ *
+ * Name of the event we generate every minute, which includes arguments and
+ * environment indictating when the event was generated.
+ **/
+#define TIME_EVENT "time"
 
 #endif /* INIT_EVENTS_H */

=== modified file 'init/main.c'
--- init/main.c	2012-08-07 16:39:00 +0000
+++ init/main.c	2012-08-09 12:39:33 +0000
@@ -46,6 +46,7 @@
 #include <nih/list.h>
 #include <nih/timer.h>
 #include <nih/signal.h>
+#include <nih/string.h>
 #include <nih/child.h>
 #include <nih/option.h>
 #include <nih/main.h>
@@ -54,6 +55,7 @@
 
 #include "paths.h"
 #include "events.h"
+#include "environ.h"
 #include "system.h"
 #include "job_class.h"
 #include "job_process.h"
@@ -71,6 +73,7 @@
 static void pwr_handler     (void *data, NihSignal *signal);
 static void hup_handler     (void *data, NihSignal *signal);
 static void usr1_handler    (void *data, NihSignal *signal);
+static void timer_handler    (void *data, NihTimer  *timer);
 #endif /* DEBUG */
 
 static void handle_confdir      (void);
@@ -172,7 +175,7 @@
 	argv0 = argv[0];
 	nih_main_init (argv0);
 
-	nih_option_set_synopsis (_("Process management daemon."));
+	nih_option_set_synopsis (_("Process management daemon. (cron-replacement version)"));
 	nih_option_set_help (
 		_("This daemon is normally executed by the kernel and given "
 		  "process id 1 to denote its special status.  When executed "
@@ -423,6 +426,8 @@
 	NIH_MUST (nih_main_loop_add_func (NULL, (NihMainLoopCb)event_poll,
 					  NULL));
 
+	/* Initialize the timers */
+        nih_timer_init();
 
 	/* Adjust our OOM priority to the default, which will be inherited
 	 * by all jobs.
@@ -514,7 +519,7 @@
 #endif /* DEBUG */
 
 
-	/* Generate and run the startup event or read the state from the
+	/* Generate and run the startup and time event or read the state from the
 	 * init daemon that exec'd us
 	 */
 	if (! restart) {
@@ -526,6 +531,7 @@
 				? initial_event
 				: STARTUP_EVENT,
 				NULL));
+			timer_handler(NULL,NULL);
 		}
 
 	} else {
@@ -775,6 +781,53 @@
 		}
 	}
 }
+
+/**
+ * timer_handler:
+ * @data: an integer which specifies the timer phase: initial, next minute, every minute,
+ * @timer: timer that called this handler.
+ *
+ * Handle having recieved the minute-wise timer event, which we use to
+ * generate a TIME event.
+ **/
+static void
+timer_handler (void     *data,
+               NihTimer *timer)
+{
+	nih_local char **env = NULL;
+	size_t           len;
+	time_t           rawtime;
+	struct tm       *timeinfo;
+
+	time ( &rawtime );
+	timeinfo = localtime ( &rawtime );
+
+	len = 0;
+	env = NIH_MUST (nih_str_array_new (NULL));
+
+	/* Add the job and instance name */
+	NIH_MUST (environ_set (&env, NULL, &len, TRUE,
+			       "TIME=%.2d:%.2d", timeinfo->tm_hour,timeinfo->tm_min));
+	NIH_MUST (environ_set (&env, NULL, &len, TRUE,
+			       "DAY_OF_WEEK=%.1d", timeinfo->tm_wday+1));
+	NIH_MUST (environ_set (&env, NULL, &len, TRUE,
+			       "DATE=%.2d/%.2d/%.4d", timeinfo->tm_mon+1,timeinfo->tm_mday,timeinfo->tm_year+1900));
+
+	NIH_MUST (event_new (NULL, TIME_EVENT, env));
+
+	if(data == (void*)0)
+	{
+	/* Start at next minute a new time event */
+	NihTimer *timer = nih_timer_add_timeout(NULL, 61 - (rawtime % 60), timer_handler,
+			       (void*)1);
+	}
+	if(data == (void*)1)
+	{
+	/* Every minute a new time event */
+	NihTimer *timer = nih_timer_add_periodic(NULL, 60, timer_handler,
+			       (void*)2);
+	}
+}
 #endif /* DEBUG */
 
 /**

-- 
upstart-devel mailing list
[email protected]
Modify settings or unsubscribe at: 
https://lists.ubuntu.com/mailman/listinfo/upstart-devel

Reply via email to