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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
/*
* Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
* This file is part of the "cimple" project.
* For details, see https://github.com/egor-tensin/cimple.
* Distributed under the MIT License.
*/
#include "server.h"
#include "ci_queue.h"
#include "compiler.h"
#include "const.h"
#include "log.h"
#include "msg.h"
#include "signal.h"
#include "storage.h"
#include "storage_sqlite.h"
#include "tcp_server.h"
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
struct server {
pthread_mutex_t server_mtx;
pthread_cond_t server_cv;
int stopping;
struct storage storage;
struct tcp_server *tcp_server;
struct ci_queue ci_queue;
};
int server_create(struct server **_server, const struct settings *settings)
{
struct server *server;
struct storage_settings storage_settings;
int ret = 0;
*_server = malloc(sizeof(struct server));
if (!*_server) {
log_errno("malloc");
return -1;
}
server = *_server;
ret = pthread_mutex_init(&server->server_mtx, NULL);
if (ret) {
pthread_errno(ret, "pthread_mutex_init");
goto free;
}
ret = pthread_cond_init(&server->server_cv, NULL);
if (ret) {
pthread_errno(ret, "pthread_cond_init");
goto destroy_mtx;
}
server->stopping = 0;
ret = storage_settings_create_sqlite(&storage_settings, settings->sqlite_path);
if (ret < 0)
goto destroy_cv;
ret = storage_create(&server->storage, &storage_settings);
storage_settings_destroy(&storage_settings);
if (ret < 0)
goto destroy_cv;
ret = tcp_server_create(&server->tcp_server, settings->port);
if (ret < 0)
goto destroy_storage;
ci_queue_create(&server->ci_queue);
return ret;
destroy_storage:
storage_destroy(&server->storage);
destroy_cv:
pthread_errno_if(pthread_cond_destroy(&server->server_cv), "pthread_cond_destroy");
destroy_mtx:
pthread_errno_if(pthread_mutex_destroy(&server->server_mtx), "pthread_mutex_destroy");
free:
free(server);
return ret;
}
void server_destroy(struct server *server)
{
log("Shutting down\n");
ci_queue_destroy(&server->ci_queue);
tcp_server_destroy(server->tcp_server);
storage_destroy(&server->storage);
pthread_errno_if(pthread_cond_destroy(&server->server_cv), "pthread_cond_destroy");
pthread_errno_if(pthread_mutex_destroy(&server->server_mtx), "pthread_mutex_destroy");
free(server);
}
static int server_has_runs(const struct server *server)
{
return !ci_queue_is_empty(&server->ci_queue);
}
static int worker_ci_run(int fd, const struct ci_queue_entry *ci_run)
{
struct msg request, response;
int ret = 0;
const char *argv[] = {CMD_CI_RUN, ci_queue_entry_get_url(ci_run),
ci_queue_entry_get_rev(ci_run), NULL};
ret = msg_from_argv(&request, argv);
if (ret < 0)
return ret;
ret = msg_send_and_wait(fd, &request, &response);
msg_free(&request);
if (ret < 0)
return ret;
if (response.argc < 0) {
log_err("Failed to schedule a CI run: worker is busy?\n");
ret = -1;
goto free_response;
}
/* TODO: handle the response. */
free_response:
msg_free(&response);
return ret;
}
static int worker_dequeue_run(struct server *server, struct ci_queue_entry **ci_run)
{
int ret = 0;
ret = pthread_mutex_lock(&server->server_mtx);
if (ret) {
pthread_errno(ret, "pthread_mutex_lock");
return ret;
}
while (!server->stopping && !server_has_runs(server)) {
ret = pthread_cond_wait(&server->server_cv, &server->server_mtx);
if (ret) {
pthread_errno(ret, "pthread_cond_wait");
goto unlock;
}
}
if (server->stopping) {
ret = -1;
goto unlock;
}
*ci_run = ci_queue_remove_first(&server->ci_queue);
log("Removed a CI run for repository %s from the queue\n", ci_queue_entry_get_url(*ci_run));
goto unlock;
unlock:
pthread_errno_if(pthread_mutex_unlock(&server->server_mtx), "pthread_mutex_unlock");
return ret;
}
static int worker_requeue_run(struct server *server, struct ci_queue_entry *ci_run)
{
int ret = 0;
ret = pthread_mutex_lock(&server->server_mtx);
if (ret) {
pthread_errno(ret, "pthread_mutex_lock");
return ret;
}
ci_queue_add_first(&server->ci_queue, ci_run);
log("Requeued a CI run for repository %s\n", ci_queue_entry_get_url(ci_run));
ret = pthread_cond_signal(&server->server_cv);
if (ret) {
pthread_errno(ret, "pthread_cond_signal");
ret = 0;
goto unlock;
}
unlock:
pthread_errno_if(pthread_mutex_unlock(&server->server_mtx), "pthread_mutex_unlock");
return ret;
}
static int worker_iteration(struct server *server, int fd)
{
struct ci_queue_entry *ci_run = NULL;
int ret = 0;
ret = worker_dequeue_run(server, &ci_run);
if (ret < 0)
return ret;
ret = worker_ci_run(fd, ci_run);
if (ret < 0)
goto requeue_run;
ci_queue_entry_destroy(ci_run);
return ret;
requeue_run:
worker_requeue_run(server, ci_run);
return ret;
}
static int worker_thread(struct server *server, int fd)
{
int ret = 0;
while (1) {
ret = worker_iteration(server, fd);
if (ret < 0)
return ret;
}
return ret;
}
static int msg_new_worker_handler(struct server *server, int client_fd,
UNUSED const struct msg *request)
{
return worker_thread(server, client_fd);
}
static int msg_new_worker_parser(UNUSED const struct msg *msg)
{
return 1;
}
static int msg_ci_run_queue(struct server *server, const char *url, const char *rev)
{
struct ci_queue_entry *entry;
int ret = 0;
ret = pthread_mutex_lock(&server->server_mtx);
if (ret) {
pthread_errno(ret, "pthread_mutex_lock");
return ret;
}
ret = ci_queue_entry_create(&entry, url, rev);
if (ret < 0)
goto unlock;
ci_queue_add_last(&server->ci_queue, entry);
log("Added a new CI run for repository %s to the queue\n", url);
ret = pthread_cond_signal(&server->server_cv);
if (ret) {
pthread_errno(ret, "pthread_cond_signal");
ret = 0;
goto unlock;
}
unlock:
pthread_errno_if(pthread_mutex_unlock(&server->server_mtx), "pthread_mutex_unlock");
return ret;
}
static int msg_ci_run_handler(struct server *server, int client_fd, const struct msg *msg)
{
struct msg response;
int ret = 0;
ret = msg_ci_run_queue(server, msg->argv[1], msg->argv[2]);
if (ret < 0)
ret = msg_error(&response);
else
ret = msg_success(&response);
if (ret < 0)
return ret;
ret = msg_send(client_fd, &response);
msg_free(&response);
return ret;
}
static int msg_ci_run_parser(const struct msg *msg)
{
if (msg->argc != 3) {
log_err("Invalid number of arguments for a message: %d\n", msg->argc);
return 0;
}
return 1;
}
typedef int (*msg_parser)(const struct msg *msg);
typedef int (*msg_handler)(struct server *, int client_fd, const struct msg *msg);
struct msg_descr {
const char *cmd;
msg_parser parser;
msg_handler handler;
};
struct msg_descr messages[] = {
{CMD_NEW_WORKER, msg_new_worker_parser, msg_new_worker_handler},
{CMD_CI_RUN, msg_ci_run_parser, msg_ci_run_handler},
};
static int server_msg_handler(struct server *server, int client_fd, const struct msg *request)
{
if (request->argc == 0)
goto unknown_request;
size_t numof_messages = sizeof(messages) / sizeof(messages[0]);
for (size_t i = 0; i < numof_messages; ++i) {
if (strcmp(messages[i].cmd, request->argv[0]))
continue;
if (!messages[i].parser(request))
continue;
return messages[i].handler(server, client_fd, request);
}
unknown_request:
log_err("Received an unknown message\n");
msg_dump(request);
struct msg response;
msg_error(&response);
return msg_send(client_fd, &response);
}
static int server_conn_handler(int client_fd, void *_server)
{
struct server *server = (struct server *)_server;
struct msg request;
int ret = 0;
ret = msg_recv(client_fd, &request);
if (ret < 0)
return ret;
ret = server_msg_handler(server, client_fd, &request);
msg_free(&request);
return ret;
}
static int server_set_stopping(struct server *server)
{
int ret = 0;
ret = pthread_mutex_lock(&server->server_mtx);
if (ret) {
pthread_errno(ret, "pthread_mutex_lock");
return ret;
}
server->stopping = 1;
ret = pthread_cond_broadcast(&server->server_cv);
if (ret) {
pthread_errno(ret, "pthread_cond_signal");
goto unlock;
}
unlock:
pthread_errno_if(pthread_mutex_unlock(&server->server_mtx), "pthread_mutex_unlock");
return ret;
}
int server_main(struct server *server)
{
int ret = 0;
ret = signal_install_global_handler();
if (ret < 0)
return ret;
while (!global_stop_flag) {
log("Waiting for new connections\n");
ret = tcp_server_accept(server->tcp_server, server_conn_handler, server);
if (ret < 0) {
if (errno == EINVAL && global_stop_flag) {
ret = 0;
break;
}
return ret;
}
}
return server_set_stopping(server);
}
|