mirror of
https://github.com/troglobit/libuev.git
synced 2026-11-17 19:31:18 +00:00
Run cron watcher with start time zero when the event loop starts
A cron watcher with start time zero, documented to mean 'now', never
runs:
uev_cron_init(ctx, w, cb, NULL, 0, 3600);
Zero is passed as is to the timerfd, where it disarms the timer.
Replace it with the current time when the event loop is running. This
also goes for uev_cron_set(), where zero no longer disarms the watcher,
use uev_cron_stop() for that.
The event loop used a start time of zero to mark an at job as done.
Stop the watcher there instead and keep its time, so restarting an at
job that was interrupted by a clock change does not run it at once.
Closes #30
This commit is contained in:
1 parent
28643fa599
commit
397247d567
5 files changed
+44
-8
No files matched your search
+9
-5
@@ -25,6 +25,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h> /* memset() */
|
#include <string.h> /* memset() */
|
||||||
#include <sys/timerfd.h>
|
#include <sys/timerfd.h>
|
||||||
|
#include <time.h> /* time() */
|
||||||
#include <unistd.h> /* close(), read() */
|
#include <unistd.h> /* close(), read() */
|
||||||
|
|
||||||
#include "uev.h"
|
#include "uev.h"
|
||||||
@@ -92,9 +93,12 @@ int uev_cron_init(uev_ctx_t *ctx, uev_t *w, uev_cb_t *cb, void *arg, time_t when
|
|||||||
/**
|
/**
|
||||||
* Reset an at/cron job watcher
|
* Reset an at/cron job watcher
|
||||||
* @param w Watcher to reset
|
* @param w Watcher to reset
|
||||||
* @param when First point in time to call @p cb
|
* @param when First point in time to call @p cb, zero for 'now'
|
||||||
* @param interval For an at job this is zero, for cron the offset interval
|
* @param interval For an at job this is zero, for cron the offset interval
|
||||||
*
|
*
|
||||||
|
* Note, unlike uev_timer_set() a @p when of zero does not disarm the
|
||||||
|
* watcher, use uev_cron_stop() for that.
|
||||||
|
*
|
||||||
* @return POSIX OK(0) or non-zero with @p errno set on error.
|
* @return POSIX OK(0) or non-zero with @p errno set on error.
|
||||||
*/
|
*/
|
||||||
int uev_cron_set(uev_t *w, time_t when, time_t interval)
|
int uev_cron_set(uev_t *w, time_t when, time_t interval)
|
||||||
@@ -110,12 +114,12 @@ int uev_cron_set(uev_t *w, time_t when, time_t interval)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Zero is 'now', when the event loop is started */
|
||||||
|
if (!when && w->ctx->running)
|
||||||
|
when = time(NULL);
|
||||||
|
|
||||||
/* Handle stopped timers */
|
/* Handle stopped timers */
|
||||||
if (w->fd < 0) {
|
if (w->fd < 0) {
|
||||||
/* Timer already stopped */
|
|
||||||
if (!when && !interval)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
if (uev_cron_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, when, interval))
|
if (uev_cron_init(w->ctx, w, (uev_cb_t *)w->cb, w->arg, when, interval))
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -420,11 +420,9 @@ int uev_run(uev_ctx_t *ctx, int flags)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!w->u.c.interval)
|
if (!w->u.c.interval)
|
||||||
w->u.c.when = 0;
|
uev_timer_stop(w);
|
||||||
else
|
else
|
||||||
w->u.c.when += w->u.c.interval;
|
w->u.c.when += w->u.c.interval;
|
||||||
if (!w->u.c.when)
|
|
||||||
uev_timer_stop(w);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case UEV_EVENT_TYPE:
|
case UEV_EVENT_TYPE:
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
active
|
active
|
||||||
api
|
api
|
||||||
complete
|
complete
|
||||||
|
cronnow
|
||||||
cronrun
|
cronrun
|
||||||
cronstart
|
cronstart
|
||||||
error
|
error
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ TESTS =
|
|||||||
TESTS += active
|
TESTS += active
|
||||||
TESTS += api
|
TESTS += api
|
||||||
TESTS += complete
|
TESTS += complete
|
||||||
|
TESTS += cronnow
|
||||||
TESTS += cronrun
|
TESTS += cronrun
|
||||||
TESTS += cronstart
|
TESTS += cronstart
|
||||||
TESTS += error
|
TESTS += error
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
/* Verifies that a cron watcher with start time zero runs now */
|
||||||
|
#include "check.h"
|
||||||
|
|
||||||
|
static int fired;
|
||||||
|
|
||||||
|
static void cb(uev_t *w, void *arg, int events)
|
||||||
|
{
|
||||||
|
fired++;
|
||||||
|
uev_exit(w->ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
uev_ctx_t ctx;
|
||||||
|
uev_t w;
|
||||||
|
|
||||||
|
fail_unless(uev_init(&ctx) == 0);
|
||||||
|
fail_unless(uev_cron_init(&ctx, &w, cb, NULL, 0, 0) == 0);
|
||||||
|
|
||||||
|
alarm(2);
|
||||||
|
fail_unless(uev_run(&ctx, 0) == 0);
|
||||||
|
fail_unless(fired == 1);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Variables:
|
||||||
|
* indent-tabs-mode: t
|
||||||
|
* c-file-style: "linux"
|
||||||
|
* End:
|
||||||
|
*/
|
||||||
Reference in new issue
Block a user