root/src/noitd.c

Revision 3b3b432b41dd3bfb80c144aa7ba28e75daa2337f, 3.4 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

asynchronous job queues

  • Property mode set to 100644
Line 
1 #include "noit_defines.h"
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <sys/ioctl.h>
8 #include <fcntl.h>
9
10 #include "eventer/eventer.h"
11 #include "utils/noit_log.h"
12 #include "utils/noit_hash.h"
13 #include "noit_listener.h"
14 #include "noit_console.h"
15 #include "noit_module.h"
16 #include "noit_conf.h"
17
18 int test_asynch_cb(eventer_t e, int mask, void *closure, struct timeval *now) {
19   time_t tmp;
20   int seconds = (int)closure;
21
22   noitL(noit_error, "%d: test_asynch_cb fired on (%p) mask 0x%x\n",
23         (int)time(&tmp), e, mask);
24   if(mask & EVENTER_ASYNCH_WORK) {
25     noitL(noit_error, "%d: Starting test_asynch_cb(%p) for %d seconds\n",
26           (int)time(&tmp), e, seconds);
27     sleep(seconds);
28     noitL(noit_error, "%d: Finishing up test_asynch_cb(%p)\n", (int)time(&tmp), e);
29   }
30   if(mask & EVENTER_ASYNCH_CLEANUP) {
31     noitL(noit_error, "%d: Cleaning up test_asynch_cb(%p)\n", (int)time(&tmp), e);
32   }
33   e->mask = 0;
34   return 0;
35 }
36 void test_asynch() {
37   eventer_t e;
38
39   e = eventer_alloc();
40   e->mask = EVENTER_ASYNCH;
41   gettimeofday(&e->whence, NULL); e->whence.tv_sec += 10;
42   e->closure = (void *)5;
43   e->callback = test_asynch_cb;
44   eventer_add(e);
45
46   e = eventer_alloc();
47   e->mask = EVENTER_ASYNCH;
48   gettimeofday(&e->whence, NULL); e->whence.tv_sec += 2;
49   e->closure = (void *)10;
50   e->callback = test_asynch_cb;
51   eventer_add(e);
52 }
53
54 static char *config_file = ETC_DIR "/noit.conf";
55 static int debug = 0;
56
57 void parse_clargs(int argc, char **argv) {
58   int c;
59   while((c = getopt(argc, argv, "c:d")) != EOF) {
60     switch(c) {
61       case 'c':
62         config_file = strdup(optarg);
63         break;
64       case 'd':
65         debug++;
66         break;
67       default:
68         break;
69     }
70   }
71 }
72
73 static
74 int configure_eventer() {
75   int rv = 0;
76   noit_hash_table *table;
77   table = noit_conf_get_hash(NULL, "/noit/eventer/config/*");
78   if(table) {
79     noit_hash_iter iter = NOIT_HASH_ITER_ZERO;
80     const char *key, *value;
81     int klen;
82     while(noit_hash_next(table, &iter, &key, &klen, (void **)&value)) {
83       int subrv;
84       if((subrv = eventer_propset(key, value)) != 0)
85         rv = subrv;
86     }
87     noit_hash_destroy(table, free, free);
88     free(table);
89   }
90   return rv;
91 }
92
93 int main(int argc, char **argv) {
94   char conf_str[1024];
95
96   parse_clargs(argc, argv);
97
98   /* First initialize logging, so we can log errors */
99   noit_log_init();
100   if(debug) {
101     noit_log_stream_add_stream(noit_debug, noit_stderr);
102     noit_debug->enabled = 1;
103   }
104   else {
105     noit_debug->enabled = 0;
106   }
107   noit_log_stream_add_stream(noit_error, noit_stderr);
108
109   /* Next load the configs */
110   noit_conf_init();
111   if(noit_conf_load(config_file) == -1) {
112     fprintf(stderr, "Cannot load config: '%s'\n", config_file);
113   }
114
115   /* Lastly, run through all other system inits */
116   if(!noit_conf_get_stringbuf(NULL, "/noit/eventer/implementation",
117                               conf_str, sizeof(conf_str))) {
118     noitL(noit_stderr, "Cannot find '%s' in configuration\n",
119           "/noit/eventer/implementation");
120     exit(-1);
121   }
122   if(eventer_choose(conf_str) == -1) {
123     noitL(noit_stderr, "Cannot choose eventer %s\n", conf_str);
124     exit(-1);
125   }
126   if(configure_eventer() != 0) {
127     noitL(noit_stderr, "Cannot configure eventer\n");
128     exit(-1);
129   }
130   if(eventer_init() == -1) {
131     noitL(noit_stderr, "Cannot init eventer %s\n", conf_str);
132     exit(-1);
133   }
134   noit_console_init();
135   noit_module_init();
136   noit_poller_init();
137   noit_listener_init();
138
139   test_asynch();
140
141   eventer_loop();
142   return 0;
143 }
Note: See TracBrowser for help on using the browser.