source: ogServer-Git/sources/schedule.c @ b31e7dd

Last change on this file since b31e7dd was 85b345d, checked in by OpenGnSys Support Team <soporte-og@…>, 4 years ago

#942 Add tasks to queue

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[83b242c]1#include "schedule.h"
2#include "list.h"
3#include <sys/types.h>
4#include <stdbool.h>
5#include <stdint.h>
6#include <stdlib.h>
7#include <syslog.h>
8#include <time.h>
9#include <ev.h>
10
11struct og_schedule *current_schedule = NULL;
12static LIST_HEAD(schedule_list);
13
14static void og_schedule_add(struct og_schedule *new)
15{
16        struct og_schedule *schedule, *next;
17        time_t now;
18
19        now = time(NULL);
20        if (new->seconds < now) {
21                free(new);
22                return;
23        }
24        list_for_each_entry_safe(schedule, next, &schedule_list, list) {
25                if (new->seconds < schedule->seconds) {
26                        list_add_tail(&new->list, &schedule->list);
27                        return;
28                }
29        }
30        list_add_tail(&new->list, &schedule_list);
31}
32
33/* Returns the days in a month from the weekday. */
34static void get_days_from_weekday(struct tm *tm, int wday, int *days, int *j)
35{
36        int i, mday = 0;
37
38        tm->tm_mday = 1;
39
40        //Shift week to start on Sunday instead of Monday
41        if (wday == 6)
42                wday = 0;
43        else
44                wday++;
45
46        /* A bit bruteforce, but simple. */
47        for (i = 0; i <= 30; i++) {
48                mktime(tm);
49                /* Not this weekday, skip. */
50                if (tm->tm_wday != wday) {
51                        tm->tm_mday++;
52                        continue;
53                }
54                /* Not interested in next month. */
55                if (tm->tm_mday < mday)
56                        break;
57
58                /* Found a matching. */
59                mday = tm->tm_mday;
60                days[(*j)++] = tm->tm_mday;
61                tm->tm_mday++;
62        }
63}
64
65/* Returns the days in the given week. */
66static void get_days_from_week(struct tm *tm, int week, int *days, int *k)
67{
68        int i, j, week_counter = 0;
69        bool week_over = false;
70
71        tm->tm_mday = 1;
72
73        /* Remaining days of this month. */
74        for (i = 0; i <= 30; i++) {
75                mktime(tm);
76
77                /* Last day of this week? */
78                if (tm->tm_wday == 6)
79                        week_over = true;
80
81                /* Not the week we are searching for. */
82                if (week != week_counter) {
83                        tm->tm_mday++;
84                        if (week_over) {
85                                week_counter++;
86                                week_over = false;
87                        }
88                        continue;
89                }
90
91                /* Found matching. */
92                for (j = tm->tm_wday; j <= 6; j++) {
93                        days[(*k)++] = tm->tm_mday++;
94                        mktime(tm);
95                }
96                break;
97        }
98}
99
100static int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
101
102static int last_month_day(struct tm *tm)
103{
104        /* Leap year? Adjust it. */
105        if (tm->tm_mon == 1) {
106                tm->tm_mday = 29;
107                mktime(tm);
108                if (tm->tm_mday == 29)
109                        return 29;
110
111                tm->tm_mon = 1;
112        }
113
114        return monthdays[tm->tm_mon];
115}
116
117/* Returns the days in the given week. */
118static void get_last_week(struct tm *tm, int *days, int *j)
119{
120        int i, last_day;
121
122        last_day = last_month_day(tm);
123        tm->tm_mday = last_day;
124
125        for (i = last_day; i >= last_day - 6; i--) {
126                mktime(tm);
127
128                days[(*j)++] = tm->tm_mday;
129
130
131                /* Last day of this week? */
[130b6ff]132                if (tm->tm_wday == 1)
[83b242c]133                        break;
134
135                tm->tm_mday--;
136        }
137}
138
139static void og_parse_years(uint16_t years_mask, int years[])
140{
141        int i, j = 0;
142
143        for (i = 0; i < 16; i++) {
144                if ((1 << i) & years_mask)
145                        years[j++] = 2010 + i - 1900;
146        }
147}
148
149static void og_parse_months(uint16_t months_mask, int months[])
150{
151        int i, j = 0;
152
153        for (i = 0; i < 12; i++) {
154                if ((1 << i) & months_mask)
155                        months[j++] = i + 1;
156        }
157}
158
159static void og_parse_days(uint32_t days_mask, int *days)
160{
161        int i, j = 0;
162
163        for (i = 0; i < 31; i++) {
164                if ((1 << i) & days_mask)
165                        days[j++] = i + 1;
166        }
167}
168
169static void og_parse_hours(uint16_t hours_mask, uint8_t am_pm, int hours[])
170{
171        int pm = 12 * am_pm;
172        int i, j = 0;
173
174        for (i = 0; i < 12; i++) {
175                if ((1 << i) & hours_mask)
176                        hours[j++] = i + pm + 1;
177        }
178}
179
180static void og_schedule_remove_duplicates()
181{
182        struct og_schedule *schedule, *next, *prev = NULL;
183
184        list_for_each_entry_safe(schedule, next, &schedule_list, list) {
185                if (!prev) {
186                        prev = schedule;
187                        continue;
188                }
189                if (prev->seconds == schedule->seconds &&
190                    prev->task_id == schedule->task_id) {
191                        list_del(&prev->list);
192                        free(prev);
193                }
194                prev = schedule;
195        }
196}
197
[130b6ff]198static void og_schedule_create_weekdays(int month, int year,
199                                        int *hours, int minutes, int week_days,
200                                        uint32_t task_id, uint32_t schedule_id)
201{
202        struct og_schedule *schedule;
203        int month_days[5];
204        int n_month_days;
205        uint32_t wday;
206        struct tm tm;
207        int k, l;
208
209        for (wday = 0; wday < 7; wday++) {
210                if (!((1 << wday) & week_days))
211                        continue;
212
213                memset(&tm, 0, sizeof(tm));
214                tm.tm_mon = month;
215                tm.tm_year = year;
216
217                n_month_days = 0;
218                memset(month_days, 0, sizeof(month_days));
219                get_days_from_weekday(&tm, wday, month_days, &n_month_days);
220
221                for (k = 0; month_days[k] != 0 && k < n_month_days; k++) {
222                        for (l = 0; hours[l] != 0 && l < 31; l++) {
223                                schedule = (struct og_schedule *)
224                                        calloc(1, sizeof(struct og_schedule));
225                                if (!schedule)
226                                        return;
227
228                                memset(&tm, 0, sizeof(tm));
229                                tm.tm_year = year;
230                                tm.tm_mon = month;
231                                tm.tm_mday = month_days[k];
232                                tm.tm_hour = hours[l] - 1;
233                                tm.tm_min = minutes;
234
235                                schedule->seconds = mktime(&tm);
236                                schedule->task_id = task_id;
237                                schedule->schedule_id = schedule_id;
238                                og_schedule_add(schedule);
239                        }
240                }
241        }
242}
243
244static void og_schedule_create_weeks(int month, int year,
245                                     int *hours, int minutes, int weeks,
246                                     uint32_t task_id, uint32_t schedule_id)
247{
248        struct og_schedule *schedule;
249        int month_days[7];
250        int n_month_days;
251        struct tm tm;
252        int week;
253        int k, l;
254
255        for (week = 0; week < 5; week++) {
256                if (!((1 << week) & weeks))
257                        continue;
258
259                memset(&tm, 0, sizeof(tm));
260                tm.tm_mon = month;
261                tm.tm_year = year;
262
263                n_month_days = 0;
264                memset(month_days, 0, sizeof(month_days));
265                if (week == 5)
266                        get_last_week(&tm, month_days, &n_month_days);
267                else
268                        get_days_from_week(&tm,  week, month_days, &n_month_days);
269
270                for (k = 0; month_days[k] != 0 && k < n_month_days; k++) {
271                        for (l = 0; hours[l] != 0 && l < 31; l++) {
272                                schedule = (struct og_schedule *)
273                                        calloc(1, sizeof(struct og_schedule));
274                                if (!schedule)
275                                        return;
276
277                                memset(&tm, 0, sizeof(tm));
278                                tm.tm_year = year;
279                                tm.tm_mon = month;
280                                tm.tm_mday = month_days[k];
281                                tm.tm_hour = hours[l] - 1;
282                                tm.tm_min = minutes;
283
284                                schedule->seconds = mktime(&tm);
285                                schedule->task_id = task_id;
286                                schedule->schedule_id = schedule_id;
287                                og_schedule_add(schedule);
288                        }
289                }
290        }
291}
292
293static void og_schedule_create_days(int month, int year,
294                                    int *hours, int minutes, int *days,
295                                    uint32_t task_id, uint32_t schedule_id)
296{
297        struct og_schedule *schedule;
298        struct tm tm;
299        int k, l;
300
301        for (k = 0; days[k] != 0 && k < 31; k++) {
302                for (l = 0; hours[l] != 0 && l < 31; l++) {
303                        schedule = (struct og_schedule *)
304                                calloc(1, sizeof(struct og_schedule));
305                        if (!schedule)
306                                return;
307
308                        memset(&tm, 0, sizeof(tm));
309                        tm.tm_year = year;
310                        tm.tm_mon = month;
311                        tm.tm_mday = days[k];
312                        tm.tm_hour = hours[l] - 1;
313                        tm.tm_min = minutes;
314
315                        schedule->seconds = mktime(&tm);
316                        schedule->task_id = task_id;
317                        schedule->schedule_id = schedule_id;
318                        og_schedule_add(schedule);
319                }
320        }
321}
322
[83b242c]323void og_schedule_create(unsigned int schedule_id, unsigned int task_id,
324                        struct og_schedule_time *time)
325{
[130b6ff]326        int year, month, minutes;
[83b242c]327        int months[12] = {};
328        int years[12] = {};
329        int hours[12] = {};
330        int days[31] = {};
[130b6ff]331        int i, j;
[83b242c]332
333        og_parse_years(time->years, years);
334        og_parse_months(time->months, months);
335        og_parse_days(time->days, days);
336        og_parse_hours(time->hours, time->am_pm, hours);
337        minutes = time->minutes;
338
339        for (i = 0; years[i] != 0 && i < 12; i++) {
340                for (j = 0; months[j] != 0 && j < 12; j++) {
[130b6ff]341                        month = months[j] - 1;
342                        year = years[i];
343
344                        if (time->week_days)
345                                og_schedule_create_weekdays(month, year,
346                                                            hours, minutes,
347                                                            time->week_days,
348                                                            task_id,
349                                                            schedule_id);
350
351                        if (time->weeks)
352                                og_schedule_create_weeks(month, year,
353                                                         hours, minutes,
354                                                         time->weeks,
355                                                         task_id,
356                                                         schedule_id);
357
358                        if (time->days)
359                                og_schedule_create_days(month, year,
360                                                        hours, minutes,
361                                                        days,
362                                                        task_id,
363                                                        schedule_id);
[83b242c]364                }
365        }
366
367        og_schedule_remove_duplicates();
368}
369
370void og_schedule_delete(struct ev_loop *loop, uint32_t schedule_id)
371{
372        struct og_schedule *schedule, *next;
373
374        list_for_each_entry_safe(schedule, next, &schedule_list, list) {
375                if (schedule->schedule_id != schedule_id)
376                        continue;
377
378                list_del(&schedule->list);
379                if (current_schedule == schedule) {
380                        ev_timer_stop(loop, &schedule->timer);
381                        current_schedule = NULL;
382                        og_schedule_refresh(loop);
383                }
384                free(schedule);
385        }
386}
387
388void og_schedule_update(struct ev_loop *loop, unsigned int schedule_id,
389                        unsigned int task_id, struct og_schedule_time *time)
390{
391        og_schedule_delete(loop, schedule_id);
392        og_schedule_create(schedule_id, task_id, time);
393}
394
395static void og_agent_timer_cb(struct ev_loop *loop, ev_timer *timer, int events)
396{
397        struct og_schedule *current;
398
399        current = container_of(timer, struct og_schedule, timer);
[85b345d]400        og_dbi_schedule_task(current->task_id, current->schedule_id);
[83b242c]401
402        ev_timer_stop(loop, timer);
403        list_del(&current->list);
404        free(current);
405
406        og_schedule_next(loop);
407}
408
409void og_schedule_next(struct ev_loop *loop)
410{
411        struct og_schedule *schedule;
412        time_t now, seconds;
413
414        if (list_empty(&schedule_list)) {
415                current_schedule = NULL;
416                return;
417        }
418
419        schedule = list_first_entry(&schedule_list, struct og_schedule, list);
420        now = time(NULL);
421        if (schedule->seconds <= now)
422                seconds = 0;
423        else
424                seconds = schedule->seconds - now;
425
426        ev_timer_init(&schedule->timer, og_agent_timer_cb, seconds, 0.);
427        ev_timer_start(loop, &schedule->timer);
428        current_schedule = schedule;
429}
430
431void og_schedule_refresh(struct ev_loop *loop)
432{
433        if (current_schedule)
434                ev_timer_stop(loop, &current_schedule->timer);
435
436        og_schedule_next(loop);
437}
Note: See TracBrowser for help on using the repository browser.