File: schedule.c

package info (click to toggle)
metview 5.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 614,356 kB
  • sloc: cpp: 560,586; ansic: 44,641; xml: 19,933; f90: 17,984; sh: 7,454; python: 5,565; yacc: 2,318; lex: 1,372; perl: 701; makefile: 87
file content (335 lines) | stat: -rw-r--r-- 13,473 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * © Copyright 1996-2012 ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <sys/time.h>
#include <time.h>
#include "mars.h"

static void get_date_for_schedule(long* year, long* month, long* day, long* hour, long* minute, long* second) {
    char* schedule_fake_now = getenv("SCHEDULE_FAKE_NOW");
    const char* who         = user(NULL);
    time_t now;
    struct tm* t;
    time(&now);
    t = gmtime(&now);

    marslog(LOG_DBUG, "-> get_date_for_schedule");
    /* Only 'max' can fake current time in order to test the schedule */
    if ((schedule_fake_now != NULL) && EQ(who, "max")) {
        request* rnow = read_request_file(schedule_fake_now);
        *year         = atol(get_value(rnow, "year", 0));
        *month        = atol(get_value(rnow, "month", 0));
        *day          = atol(get_value(rnow, "day", 0));
        *hour         = atol(get_value(rnow, "hour", 0));
        *minute       = atol(get_value(rnow, "minute", 0));
        *second       = atol(get_value(rnow, "second", 0));
        marslog(LOG_WARN, "Date has been changed to: %d%02d%02d %02d:%02d:%02d", *year, *month, *day, *hour, *minute, *second);
        free_all_requests(rnow);
    }
    else {
        *year   = (t->tm_year + 1900);
        *month  = (t->tm_mon + 1);
        *day    = t->tm_mday;
        *hour   = t->tm_hour;
        *minute = t->tm_min;
        *second = t->tm_sec;
    }
    marslog(LOG_DBUG, "<- get_date_for_schedule");
}


static boolean user_category_allowed(request* allow, request* env) {
    request* r                   = env;
    int i                        = 0;
    const char* allowed_category = NULL;
    boolean ok                   = false;

    while ((allowed_category = get_value(allow, "category", i++)) && !ok) {
        int j           = 0;
        const char* cat = NULL;

        while ((cat = get_value(env, "category", j++)) && !ok)
            ok = EQ(cat, allowed_category);

        if (!ok)
            marslog(LOG_DBUG, "user_category_allowed did not match category '%s'", allowed_category);
        else
            marslog(LOG_DBUG, "User category '%s' allowed to retrieve products before schedule", allowed_category);
    }

    return ok;
}


static void debug_schedule() {

    if (mars.dissemination_schedule & (SCHEDULE_INFORM & SCHEDULE_INFORM_FUTURE_CHANGE)) {
        marslog(LOG_WARN, "MARS internal error: SCHEDULE_INFORM & SCHEDULE_INFORM_FUTURE_CHANGE set");
    }

    if (mars.debug) {
        if (mars.dissemination_schedule & SCHEDULE_INFORM)
            marslog(LOG_INFO, "mars.dissemination_schedule & SCHEDULE_INFORM set");
        if (mars.dissemination_schedule & SCHEDULE_INFORM_FUTURE_CHANGE)
            marslog(LOG_INFO, "mars.dissemination_schedule & SCHEDULE_INFORM_FUTURE_CHANGE set");
        if (mars.dissemination_schedule & SCHEDULE_FAIL)
            marslog(LOG_INFO, "mars.dissemination_schedule & SCHEDULE_FAIL set");
        if (mars.dissemination_schedule & SCHEDULE_LOG)
            marslog(LOG_INFO, "mars.dissemination_schedule & SCHEDULE_LOG set");
        if (mars.dissemination_schedule & SCHEDULE_MAIL)
            marslog(LOG_INFO, "mars.dissemination_schedule & SCHEDULE_MAIL set");
    }
}


boolean check_dissemination_schedule(request* user, request* env, boolean logstat) {
    static request* r                              = 0;
    request* schedule                              = NULL;
    err e                                          = NOERR;
    int i                                          = 0;
    hypercube* cube                                = new_hypercube_from_mars_request(user);
    long release_seconds                           = -1;
    request* max_schedule                          = NULL;
    request* allow                                 = NULL;
    boolean allowed                                = false;
    boolean dont_log_users_product_before_schedule = (getenv("MARS_DONT_LOG_USERS_PRODUCT_BEFORE_SCHEDULE") != NULL);
    char buf[80];


    start_timer();
    marslog(LOG_DBUG, "Enter 'check_dissemination_schedule'");

    debug_schedule();

    if (!r)
        r = read_request_file(mars.dissemination_schedule_file);

    allow = empty_request("allow");
    set_value(allow, "category", "product_before_schedule");

    if (!r) {
        FILE* f = mail_open(mars.authmail, "Error while reading MARS schedule");
        mail_msg(f, "Error while reading MARS schedule from '%s'", mars.dissemination_schedule_file);
        mail_request(f, "User:", env);
        mail_request(f, "User request:", user);
        mail_close(f);

        marslog(LOG_WARN, "Error while reading MARS schedule from '%s'", mars.dissemination_schedule_file);
        marslog(LOG_WARN, "Please, inform mars@ecmwf.int");

        /* Allowed users will carry on*/
        if ((allowed = user_category_allowed(allow, env))) {
            marslog(LOG_WARN, "MARS schedule ignored");
            return 0;
        }
        return -1;
    }
    else {
        schedule = r;
    }

    if (EQ("allow", r->name)) {
        free_all_requests(allow);
        allow    = r;
        schedule = r->next;
    }

    if ((allowed = user_category_allowed(allow, env)) && dont_log_users_product_before_schedule)
        return 0;

    while (schedule) {
        const request* schedule_request = get_subrequest(schedule, "request", 0);
        const request* release_request  = get_subrequest(schedule, "release", 0);
        int release_delta_day           = -1;

        if (cube_contains(cube, schedule_request)) {
            long t = atol(get_value(release_request, "release_seconds", 0));
            long d = atol(get_value(release_request, "release_delta_day", 0));

            t += d * 24 * 3600;

            if (t > release_seconds) {
                release_seconds = t;
                max_schedule    = schedule;
            }
            marslog(LOG_DBUG, "check_dissemination_schedule: cube_order %d", cube_contains(cube, schedule_request));

            if (mars.debug) {
                marslog(LOG_DBUG, "schedule request is:");
                print_all_requests(schedule_request);

                marslog(LOG_DBUG, "release request is:");
                print_all_requests(release_request);
            }
        }

        schedule = schedule->next;
    }
    marslog(LOG_DBUG, "Release seconds: %ld", release_seconds);

    stop_timer(buf);
    if (*buf)
        marslog(LOG_INFO, "Verify schedule: %s", buf);

    if (release_seconds != -1) {
        double jnow, judate, delta;
        long year, month, day, hour, minute, second;
        long user_date = 0;

        get_date_for_schedule(&year, &month, &day, &hour, &minute, &second);

        jnow = mars_date_to_julian(year * 10000 + month * 100 + day) + hour / 24.0 + minute / 24.0 / 60.0 + second / 24.0 / 60.0 / 60.0;

        /* This causes problems with Climatology, Monthly means and
           the like, although they should not match any dissemination schedule */
        for (i = 0; i < count_values(user, "DATE"); ++i) {
            const char* p = get_value(user, "DATE", i);
            long d        = 0;
            if (is_number(p))
                d = atol(p);
            else {
                long julian = 0, second = 0;
                boolean isjul;
                parsedate(p, &julian, &second, &isjul);
                d = mars_julian_to_date(julian, mars.y2k);
            }
            if (d > user_date)
                user_date = d;
        }
        /* For Hindcasts, date to block is REFDATE */
        for (i = 0; i < count_values(user, "REFDATE"); ++i) {
            const char* p = get_value(user, "REFDATE", i);
            long d        = 0;
            if (is_number(p))
                d = atol(p);
            else {
                long julian = 0, second = 0;
                boolean isjul;
                parsedate(p, &julian, &second, &isjul);
                d = mars_julian_to_date(julian, mars.y2k);
            }
            if (d > user_date)
                user_date = d;
        }

        judate = mars_date_to_julian(user_date) + release_seconds / 24.0 / 60.0 / 60.0;
        delta  = (judate - jnow) * 24 * 60 * 60;

        marslog(LOG_DBUG, "jnow: %0.30f judate : %0.30f", jnow, judate);
        if (judate <= jnow) {
            delta = -delta;
            /* printf("OK since %ld sec=%ld min=%ld hour\n",delta,delta/60,delta/60/60); */
            e = 0;
        }
        else {
            struct {
                long second;
                long minute;
                long hour;
                long days;
            } release;
            const request* schedule_request = get_subrequest(max_schedule, "request", 0);
            const request* release_request  = get_subrequest(max_schedule, "release", 0);
            long release_second             = delta + hour * 60 * 60 + minute * 60 + second + 0.5;
            long release_minute             = 0;
            long release_hour               = 0;
            long release_days               = 0;
            char msg[64];

            if (logstat) {
                const char* trigger = getenv("MSJ_PATH");
                boolean suite       = (trigger != NULL);

                if (allowed) {
                    log_statistics("schedule", "allowed");
                }
                else {
                    if (suite)
                        log_statistics("schedule", "%s", trigger);
                    else
                        log_statistics("schedule", "user");
                }
            }

            if (allowed) {
                /* We could print a message to tell allowed users that this data
                   should not be passed on externally, if HO wants */
                return 0;
            }

            release.second = delta + hour * 60 * 60 + minute * 60 + second + 0.5;
            release.minute = 0;
            release.hour   = 0;
            release.days   = 0;

            release.hour = release.second / 60 / 60;
            release.second -= release.hour * 60 * 60;
            if (release.hour >= 24) {
                release.days = (int)(release.hour / 24);
                release.hour -= release.days * 24;
                sprintf(msg, "after %ld day%s at", release.days, (release.days > 1) ? "s" : "");
            }
            else
                sprintf(msg, "for after");

            release.minute = release.second / 60;
            release.second -= release.minute * 60;

            if ((mars.dissemination_schedule & SCHEDULE_INFORM_FUTURE_CHANGE) && !allowed) {
                marslog(LOG_WARN, "On 1st February 2006, MARS access will be");
                marslog(LOG_WARN, "harmonised with the dissemination schedule");
                marslog(LOG_WARN, "Executing this request after that day will");
                marslog(LOG_WARN, "fail and show the following warning:");
            }

            if ((mars.dissemination_schedule & SCHEDULE_INFORM || mars.dissemination_schedule & SCHEDULE_INFORM_FUTURE_CHANGE) && !allowed) {
                int loglevel = LOG_WARN;

                if (mars.dissemination_schedule & SCHEDULE_FAIL)
                    loglevel = LOG_EROR;
                marslog(loglevel, "Data not yet available. Scheduled %s %02ld:%02ld:%02ld, (%s)",
                        msg, release.hour, release.minute, release.second,
                        get_value(release_request, "release_time", 0));

                marslog(loglevel, "User request matches the following schedule rule:");
                marslog(loglevel, "      DATE = %ld", user_date);
                marslog(loglevel, "      TIME = %s", get_value(schedule_request, "TIME", 0));
                if (get_value(schedule_request, "STEP", 0) != 0) {
                    if (count_values(schedule_request, "STEP") > 1)
                        marslog(loglevel, "      STEP = %s/...", get_value(schedule_request, "STEP", 0));
                    else
                        marslog(loglevel, "      STEP = %s", get_value(schedule_request, "STEP", 0));
                }
                marslog(loglevel, "      RELEASE = %s", get_value(release_request, "release_time", 0));
                /* printf("NOT OK %ld sec=%ld min=%ld hour\n",delta,delta/60,delta/60/60); */
            }

            if ((mars.dissemination_schedule & SCHEDULE_INFORM_FUTURE_CHANGE) && !allowed)
                marslog(LOG_WARN, "Continue with the request execution");

            if (mars.dissemination_schedule & SCHEDULE_MAIL) {
                FILE* f = mail_open(mars.authmail, "MARS request issued before schedule");
                marslog(LOG_DBUG, "check_dissemination_schedule: send email to '%s'", mars.authmail);
                mail_msg(f, "Request issued on %d%02d%02d at %02d:%02d:%02d", year, month, day, hour, minute, second);
                mail_request(f, "User:", env);
                mail_request(f, "Matching schedule:", schedule_request);
                mail_request(f, "User request:", user);
                mail_close(f);
            }
            e = 1;
        }
    }

    marslog(LOG_DBUG, "Exit 'check_dissemination_schedule'");

    free_hypercube(cube);

    return e;
}