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
|
/*
* 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 "worker.h"
#include "ci.h"
#include "command.h"
#include "compiler.h"
#include "const.h"
#include "git.h"
#include "log.h"
#include "msg.h"
#include "net.h"
#include "process.h"
#include "signal.h"
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
struct worker {
int fd;
/* TODO: these are not used, but they should be! */
pthread_mutex_t task_mtx;
pthread_t task;
int task_active;
};
int worker_create(struct worker **_worker, const struct settings *settings)
{
int ret = 0;
struct worker *worker = malloc(sizeof(struct worker));
if (!worker) {
log_errno("malloc");
return -1;
}
ret = libgit_init();
if (ret < 0)
goto free;
ret = net_connect(settings->host, settings->port);
if (ret < 0)
goto git_shutdown;
worker->fd = ret;
*_worker = worker;
return ret;
git_shutdown:
libgit_shutdown();
free:
free(worker);
return ret;
}
void worker_destroy(struct worker *worker)
{
log("Shutting down\n");
log_errno_if(close(worker->fd), "close");
libgit_shutdown();
free(worker);
}
static int msg_send_new_worker(const struct worker *worker)
{
static const char *argv[] = {CMD_NEW_WORKER, NULL};
struct msg *msg = NULL;
int ret = 0;
ret = msg_from_argv(&msg, argv);
if (ret < 0)
return ret;
ret = msg_send(worker->fd, msg);
msg_free(msg);
return ret;
}
static int msg_ci_run_do(const char *url, const char *rev, struct proc_output *result)
{
int ret = 0;
ret = ci_run_git_repo(url, rev, result);
if (ret < 0) {
log_err("Run failed with an error\n");
return ret;
}
log("Process exit code: %d\n", result->ec);
log("Process output:\n%s", result->output);
if (!result->output || !result->output_len ||
result->output[result->output_len - 1] != '\n')
log("\n");
return 0;
}
static int msg_ci_run_handler(UNUSED int conn_fd, const struct msg *request, UNUSED void *_worker,
struct msg **response)
{
struct proc_output result;
int ret = 0;
if (msg_get_length(request) != 3) {
log_err("Invalid number of arguments for a message\n");
msg_dump(request);
return -1;
}
const char **words = msg_get_words(request);
proc_output_init(&result);
ret = msg_ci_run_do(words[1], words[2], &result);
proc_output_free(&result);
if (ret < 0)
return ret;
return msg_success(response);
}
static struct cmd_desc cmds[] = {
{CMD_RUN, msg_ci_run_handler},
};
int worker_main(struct worker *worker, UNUSED int argc, UNUSED char *argv[])
{
struct cmd_dispatcher *dispatcher = NULL;
int ret = 0;
ret = signal_install_global_handler();
if (ret < 0)
return ret;
ret = msg_send_new_worker(worker);
if (ret < 0)
return ret;
ret = cmd_dispatcher_create(&dispatcher, cmds, sizeof(cmds) / sizeof(cmds[0]), worker);
if (ret < 0)
return ret;
while (!global_stop_flag) {
struct msg *request = NULL;
log("Waiting for a new command\n");
ret = msg_recv(worker->fd, &request);
if (ret < 0) {
if (errno == EINVAL && global_stop_flag)
ret = 0;
goto dispatcher_destroy;
}
ret = cmd_dispatcher_handle_msg(dispatcher, worker->fd, request);
msg_free(request);
if (ret < 0)
goto dispatcher_destroy;
}
dispatcher_destroy:
cmd_dispatcher_destroy(dispatcher);
return ret;
}
|