File: cron.c

package info (click to toggle)
uwsgi 2.0.29-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,684 kB
  • sloc: ansic: 87,027; python: 7,001; cpp: 1,131; java: 708; perl: 678; sh: 585; ruby: 555; makefile: 148; xml: 130; cs: 121; objc: 37; php: 28; erlang: 20; javascript: 11
file content (321 lines) | stat: -rw-r--r-- 8,561 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "uwsgi.h"

extern struct uwsgi_server uwsgi;

struct uwsgi_cron *uwsgi_cron_add(char *crontab) {
	int i;
        struct uwsgi_cron *old_uc, *uc = uwsgi.crons;
        if (!uc) {
                uc = uwsgi_malloc(sizeof(struct uwsgi_cron));
                uwsgi.crons = uc;
        }
        else {
                old_uc = uc;
                while (uc->next) {
                        uc = uc->next;
                        old_uc = uc;
                }

                old_uc->next = uwsgi_malloc(sizeof(struct uwsgi_cron));
                uc = old_uc->next;
        }

        memset(uc, 0, sizeof(struct uwsgi_cron));

        if (sscanf(crontab, "%d %d %d %d %d %n", &uc->minute, &uc->hour, &uc->day, &uc->month, &uc->week, &i) != 5) {
                uwsgi_log("invalid cron syntax\n");
                exit(1);
        }
        uc->command = crontab + i;
        uc->pid = -1;
        return uc;
}


void uwsgi_opt_add_cron(char *opt, char *value, void *foobar) {
        uwsgi_cron_add(value);
}


void uwsgi_opt_add_unique_cron(char *opt, char *value, void *foobar) {
        struct uwsgi_cron *uc = uwsgi_cron_add(value);
        uc->unique = 1;
}


#ifdef UWSGI_SSL
void uwsgi_opt_add_legion_cron(char *opt, char *value, void *foobar) {
        char *space = strchr(value, ' ');
        if (!space) {
                uwsgi_log("invalid %s syntax, must be prefixed with a legion name\n", opt);
                exit(1);
        }
        char *legion = uwsgi_concat2n(value, space-value, "", 0);
        struct uwsgi_cron *uc = uwsgi_cron_add(space+1);
        uc->legion = legion;
}


void uwsgi_opt_add_unique_legion_cron(char *opt, char *value, void *foobar) {
        char *space = strchr(value, ' ');
        if (!space) {
                uwsgi_log("invalid %s syntax, must be prefixed with a legion name\n", opt);
                exit(1);
        }
        char *legion = uwsgi_concat2n(value, space-value, "", 0);
        struct uwsgi_cron *uc = uwsgi_cron_add(space+1);
        uc->legion = legion;
        uc->unique = 1;
}
#endif


void uwsgi_opt_add_cron2(char *opt, char *value, void *foobar) {

	char *c_minute = NULL;
	char *c_hour = NULL;
	char *c_day = NULL;
	char *c_month = NULL;
	char *c_week = NULL;
	char *c_unique = NULL;
	char *c_harakiri = NULL;
	char *c_legion = NULL;

	char *c_command = value;

	char *space = strchr(value, ' ');
	if (space) {
		if (uwsgi_str_contains(value, space - value, '=')) {
			// --cron2 key=val command
			*space = 0;
			c_command = space + 1;
		}

		// no point in parsing key=val list if there is none
		if (uwsgi_kvlist_parse(value, strlen(value), ',', '=',
			"minute", &c_minute,
			"hour", &c_hour,
			"day", &c_day,
			"month", &c_month,
			"week", &c_week,
			"unique", &c_unique,
			"harakiri", &c_harakiri,
			"legion", &c_legion,
			NULL)) {
			uwsgi_log("unable to parse cron definition: %s\n", value);
			exit(1);
		}
	}
	else {
		if (uwsgi_str_contains(value, strlen(value), '=')) {
			// --cron2 key=val
			uwsgi_log("unable to parse cron definition: %s\n", value);
			exit(1);
		}
	}

	struct uwsgi_cron *old_uc, *uc = uwsgi.crons;
	if (!uc) {
		uc = uwsgi_malloc(sizeof(struct uwsgi_cron));
		uwsgi.crons = uc;
	}
	else {
		old_uc = uc;
		while (uc->next) {
			uc = uc->next;
			old_uc = uc;
		}

		old_uc->next = uwsgi_malloc(sizeof(struct uwsgi_cron));
		uc = old_uc->next;
	}

	memset(uc, 0, sizeof(struct uwsgi_cron));

	uc->command = c_command;
	if (!uc->command) {
		uwsgi_log("[uwsgi-cron] invalid command in cron definition: %s\n", value);
		exit(1);
	}

	// defaults
	uc->minute = -1;
	uc->hour = -1;
	uc->day = -1;
	uc->month = -1;
	uc->week = -1;

	uc->unique = 0;
	uc->mercy = 0;
	uc->harakiri = 0;
	uc->pid = -1;

#ifdef UWSGI_SSL
	uc->legion = c_legion;
#endif

	if (c_minute)
		uc->minute = atoi(c_minute);

	if (c_hour)
		uc->hour = atoi(c_hour);

	if (c_day)
		uc->day = atoi(c_day);

	if (c_month)
		uc->month = atoi(c_month);

	if (c_week)
		uc->week = atoi(c_week);

	if (c_unique)
		uc->unique = atoi(c_unique);

	if (c_harakiri) {
		if (atoi(c_harakiri)) {
			// harakiri > 0
			uc->mercy = atoi(c_harakiri);
		}
		else {
			// harakiri == 0
			uc->mercy = -1;
		}
	}
	else if (uwsgi.cron_harakiri) {
		uc->harakiri = uwsgi.cron_harakiri;
	}
}


int uwsgi_signal_add_cron(uint8_t sig, int minute, int hour, int day, int month, int week) {

        if (!uwsgi.master_process)
                return -1;

        uwsgi_lock(uwsgi.cron_table_lock);

        if (ushared->cron_cnt < MAX_CRONS) {

                ushared->cron[ushared->cron_cnt].sig = sig;
                ushared->cron[ushared->cron_cnt].minute = minute;
                ushared->cron[ushared->cron_cnt].hour = hour;
                ushared->cron[ushared->cron_cnt].day = day;
                ushared->cron[ushared->cron_cnt].month = month;
                ushared->cron[ushared->cron_cnt].week = week;
                ushared->cron_cnt++;
        }
        else {
                uwsgi_log("you can register max %d cron !!!\n", MAX_CRONS);
                uwsgi_unlock(uwsgi.cron_table_lock);
                return -1;
        }

        uwsgi_unlock(uwsgi.cron_table_lock);

        return 0;
}

void uwsgi_manage_signal_cron(time_t now) {

        struct tm *uwsgi_cron_delta;
        int i;

        uwsgi_cron_delta = localtime(&now);

        if (uwsgi_cron_delta) {

                // fix month
                uwsgi_cron_delta->tm_mon++;

                uwsgi_lock(uwsgi.cron_table_lock);
                for (i = 0; i < ushared->cron_cnt; i++) {

                        struct uwsgi_cron *ucron = &ushared->cron[i];

                        int run_task = uwsgi_cron_task_needs_execution(uwsgi_cron_delta, ucron->minute, ucron->hour, ucron->day, ucron->month, ucron->week);

                        if (run_task == 1) {
                                // date match, signal it ?
                                if (now - ucron->last_job >= 60) {
                                        uwsgi_log_verbose("[uwsgi-cron] routing signal %d\n", ucron->sig);
                                        uwsgi_route_signal(ucron->sig);
                                        ucron->last_job = now;
                                }
                        }

                }
                uwsgi_unlock(uwsgi.cron_table_lock);
        }
        else {
                uwsgi_error("localtime()");
        }

}

void uwsgi_manage_command_cron(time_t now) {

        struct tm *uwsgi_cron_delta;

        struct uwsgi_cron *current_cron = uwsgi.crons;

        uwsgi_cron_delta = localtime(&now);


        if (!uwsgi_cron_delta) {
                uwsgi_error("uwsgi_manage_command_cron()/localtime()");
                return;
        }

        // fix month
        uwsgi_cron_delta->tm_mon++;

        while (current_cron) {

#ifdef UWSGI_SSL
                // check for legion cron
                if (current_cron->legion) {
                        if (!uwsgi_legion_i_am_the_lord(current_cron->legion))
                            goto next;
                }
#endif

		// skip unique crons that are still running
		if (current_cron->unique && current_cron->pid >= 0)
			goto next;

                int run_task = uwsgi_cron_task_needs_execution(uwsgi_cron_delta, current_cron->minute, current_cron->hour, current_cron->day, current_cron->month, current_cron->week);
                if (run_task == 1) {

                        // date match, run command ?
                        if (now - current_cron->last_job >= 60) {
                                //call command
                                if (current_cron->command) {
					if (current_cron->func) {
						current_cron->func(current_cron, now);
					}
					else {
						pid_t pid = uwsgi_run_command(current_cron->command, NULL, -1);
						if (pid >= 0) {
							current_cron->pid = pid;
							current_cron->started_at = now;
							uwsgi_log_verbose("[uwsgi-cron] running \"%s\" (pid %d)\n", current_cron->command, current_cron->pid);
							if (current_cron->mercy) {
								//uwsgi_cron->mercy can be negative to inform master that harakiri should be disabled for this cron
								if (current_cron->mercy > 0)
									current_cron->harakiri = now + current_cron->mercy;
							}
							else if (uwsgi.cron_harakiri)
								current_cron->harakiri = now + uwsgi.cron_harakiri;
						}
					}
                                }
                                current_cron->last_job = now;
                        }
                }

next:
                current_cron = current_cron->next;
        }
}