forked from bloomberg/comdb2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.h
140 lines (121 loc) · 3.93 KB
/
cron.h
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
/*
Copyright 2019 Bloomberg Finance L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef __CRON_H_
#define __CRON_H_
#include "comdb2.h"
#include "comdb2uuid.h"
#include "cron_systable.h"
/**
* Primitive cron job that monitors a ordered list of epoch marked events,
* each having a callback function associated
* Sleep until next event is to be triggered
*
*/
enum {
CRON_NOERR,
CRON_ERR_CREATE = -1,
CRON_ERR_MALLOC = -2,
CRON_ERR_BUG = -3,
CRON_ERR_EXIST = -4
};
/* opaque schedule */
typedef struct cron_sched cron_sched_t;
/**
* Define the multiple types of schedulers
*
*/
enum cron_type { CRON_TIMEPART = 0, CRON_LOGICAL = 1 };
/* this is the callback prototype for each event */
typedef void *(*FCRON)(uuid_t source_id, void *arg1, void *arg2, void *arg3,
struct errstat *err);
struct cron_event {
int epoch; /* when this event should run */
FCRON func; /* what function should run */
void *
arg1; /* arguments 1-3 for "func"; note: 2-3 are just for convenience */
void *arg2;
void *arg3;
uuid_t source_id; /* source id, if any, used to map events to sources */
LINKC_T(struct cron_event) lnk;
};
typedef struct cron_event cron_event_t;
struct sched_if {
cron_sched_t *sched;
enum cron_type type;
char *name;
int default_sleep_idle;
/* check if it is time to execute event "event" */
int (*is_exec_time)(struct sched_if *impl, cron_event_t *event);
/* sleep until a signal or next event is due */
int (*wait_next_event)(struct sched_if *impl, cron_event_t *event);
/* describe the scheduler */
char *(*describe)(struct sched_if *impl);
/* describe the event function */
char *(*event_describe)(struct sched_if *impl, cron_event_t *event);
void *state;
};
typedef struct sched_if sched_if_t;
/**
* Add a new event to a scheduler, and create a scheduler if needed.
* NOTE: to create a scheduler, "sched"== NULL, and "intf"!=NULL
* to add to an existing scheduler, "sched"!=NULL and "intf"==NULL
*
*/
cron_sched_t *cron_add_event(cron_sched_t *sched, const char *name, int epoch,
FCRON func, void *arg1, void *arg2, void *arg3,
uuid_t *source_id, struct errstat *err,
sched_if_t *intf);
/**
* Initialize crons system
*/
void init_cron(void);
/**
* Signal looping worker, maybe db is exiting
*
*/
void cron_signal_worker(cron_sched_t *sched);
/**
* Clear queue of events
*
*/
void cron_clear_queue(cron_sched_t *sched);
/**
* Lock/unlock scheduler so I can look at events
*
* NOTE: locking waits for the running to complete
*
*/
void cron_lock(cron_sched_t *sched);
void cron_unlock(cron_sched_t *sched);
int cron_timedwait(cron_sched_t *sched, struct timespec *ts);
/**
* Creates a snapshot of the events of a single scheduler, used
* to create individual comdb2_.. systables displaying events
* for specific schedulers
*
*/
int cron_systable_sched_events_collect(cron_sched_t *sched,
systable_cron_events_t **parr,
int *nrecords, int *pnsize);
/**
* Create a time scheduler implementation
*
*/
void time_cron_create(sched_if_t *impl, char *(*describe)(sched_if_t *),
char *(*event_describe)(sched_if_t *, cron_event_t *));
/**
* Return specific scheduler implementation
*
*/
sched_if_t *cron_impl(cron_sched_t *sched);
#endif