فهرست منبع

Add cron.minutely

ithewei 4 سال پیش
والد
کامیت
8b10104625
4فایلهای تغییر یافته به همراه16 افزوده شده و 9 حذف شده
  1. 5 6
      base/htime.c
  2. 2 0
      base/htime.h
  3. 1 0
      event/hloop.h
  4. 8 3
      examples/hloop_test.c

+ 5 - 6
base/htime.c

@@ -214,13 +214,13 @@ datetime_t hv_compile_datetime() {
 
 time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
     enum {
-        UNKOWN,
+        MINUTELY,
         HOURLY,
         DAILY,
         WEEKLY,
         MONTHLY,
         YEARLY,
-    } period_type = UNKOWN;
+    } period_type = MINUTELY;
     struct tm tm;
     time_t tt;
     time(&tt);
@@ -248,10 +248,6 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
         }
     }
 
-    if (period_type == UNKOWN) {
-        return -1;
-    }
-
     tt_round = mktime(&tm);
     if (week >= 0) {
         tt_round = tt + (week-tm.tm_wday)*SECONDS_PER_DAY;
@@ -261,6 +257,9 @@ time_t cron_next_timeout(int minute, int hour, int day, int week, int month) {
     }
 
     switch(period_type) {
+    case MINUTELY:
+        tt_round += SECONDS_PER_MINUTE;
+        return tt_round;
     case HOURLY:
         tt_round += SECONDS_PER_HOUR;
         return tt_round;

+ 2 - 0
base/htime.h

@@ -6,6 +6,7 @@
 
 BEGIN_EXTERN_C
 
+#define SECONDS_PER_MINUTE  60
 #define SECONDS_PER_HOUR    3600
 #define SECONDS_PER_DAY     86400   // 24*3600
 #define SECONDS_PER_WEEK    604800  // 7*24*3600
@@ -98,6 +99,7 @@ HV_EXPORT datetime_t hv_compile_datetime();
 /*
  * minute   hour    day     week    month       action
  * 0~59     0~23    1~31    0~6     1~12
+ *  -1      -1      -1      -1      -1          cron.minutely
  *  30      -1      -1      -1      -1          cron.hourly
  *  30      1       -1      -1      -1          cron.daily
  *  30      1       15      -1      -1          cron.monthly

+ 1 - 0
event/hloop.h

@@ -160,6 +160,7 @@ HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, ui
 /*
  * minute   hour    day     week    month       cb
  * 0~59     0~23    1~31    0~6     1~12
+ *  -1      -1      -1      -1      -1          cron.minutely
  *  30      -1      -1      -1      -1          cron.hourly
  *  30      1       -1      -1      -1          cron.daily
  *  30      1       15      -1      -1          cron.monthly

+ 8 - 3
examples/hloop_test.c

@@ -35,10 +35,14 @@ void on_timer(htimer_t* timer) {
         LLU(hloop_now(loop)), LLU(hloop_now_hrtime(loop)));
 }
 
+void cron_minutely(htimer_t* timer) {
+    time_t now = time(NULL);
+    printf("cron_minutely: %s\n", ctime(&now));
+}
+
 void cron_hourly(htimer_t* timer) {
-    time_t tt;
-    time(&tt);
-    printf("cron_hourly: %s\n", ctime(&tt));
+    time_t now = time(NULL);
+    printf("cron_hourly: %s\n", ctime(&now));
 }
 
 void timer_write_log(htimer_t* timer) {
@@ -80,6 +84,7 @@ int main() {
 
     // test timer period
     int minute = time(NULL)%3600/60;
+    htimer_add_period(loop, cron_minutely, -1, -1, -1, -1, -1, INFINITE);
     htimer_add_period(loop, cron_hourly, minute+1, -1, -1, -1, -1, INFINITE);
 
     // test network_logger