summaryrefslogtreecommitdiff
path: root/src/worker.c
blob: d0b4f521d6f5aa40fd796b52528a3b0f320c42a0 (plain)
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
#include "worker.h"

#include "nanomsg/nn.h"

#include "port.h"

#include "pthread.h"

#include <glib.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <signal.h>

const char *worker_exection_mode_str( worker_execution_mode_t mode )
{
	switch( mode ) {
		case WORKER_EXECUTION_DISABLED: return "disabled";
		case WORKER_EXECUTION_DIRECT: return "direct";
		default: return "<unknown>";
	}
}

const char *worker_state_str( worker_state_t state )
{
	switch( state ) {
		case WORKER_STATE_STOPPED: return "stopped";
		case WORKER_STATE_RUNNING: return "running";
		default: return "<unknown>";
	}
}

worker_execution_mode_t worker_execution_mode_from_str( const char *s )
{
	if( strcasecmp( s, "disabled" ) == 0 ) {
		return WORKER_EXECUTION_DISABLED;
	} else if( strcasecmp( s, "direct" ) == 0 ) {
		return WORKER_EXECUTION_DIRECT;
	} else {
		fprintf( stderr, "Warning: unknown worker execution mode '%s'!\n", s );
		return WORKER_EXECUTION_DISABLED;
	}
}

worker_state_t worker_state_from_str( const char *s )
{
	if( strcasecmp( s, "stopped" ) == 0 ) {
		return WORKER_STATE_STOPPED;
	} else if( strcasecmp( s, "running" ) == 0 ) {
		return WORKER_STATE_RUNNING;
	} else {
		fprintf( stderr, "Warning: unknown worker state '%s' (assuming 'stopped')!\n", s );
		return WORKER_STATE_STOPPED;
	}
}		

typedef struct {
	GPid pid;
	gint out, err;
	GIOChannel *out_channel;
	GIOChannel *err_channel;
	worker_t *worker;
	pthread_t thread;
	GMainLoop *main_loop;
	
} direct_glib_execution_worker_data_t;

static void watch_child( GPid pid, gint status, gpointer *data )
{
	direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)data;

	printf( "Worker child with PID %d terminated.\n", wed->pid );

	g_spawn_close_pid( pid );
	
	g_main_loop_quit( wed->main_loop );
}

static gboolean watch_output( GIOChannel *channel, GIOCondition condition, gpointer *data )
{
	direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)data;
	worker_t *worker = wed->worker;

	bool isStdout = ( wed->out_channel == channel );
	
	gchar *buf;
	gsize len;
	
	if( condition == G_IO_HUP ) {
		g_io_channel_unref( channel );
		return FALSE;
	}
	
	g_io_channel_read_line( channel, &buf, &len, NULL, NULL );
	
	fprintf( stderr, "worker %s %s: %*s\n", worker->name,
		isStdout ? "stdout" : "stderr", len, buf );

	g_free( buf );

	return TRUE;
}

static void *worker_func( void *thread_data )
{
	direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)thread_data;
	worker_t *worker = wed->worker;

	gchar **args;
	gint nof_args;
	GError *error = NULL;
	if( !g_shell_parse_argv( worker->command, &nof_args, &args, &error ) ) {
		fprintf( stderr, "ERROR: Failed to parse command of worker '%s', '%s': %s\n",
			worker->name, worker->command, error->message );
		g_strfreev( args );
		return NULL;
	}
	
	error = NULL;
	gboolean ret = g_spawn_async_with_pipes( NULL,
		args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL,
		NULL, &wed->pid, NULL, &wed->out, &wed->err, &error );
	if( !ret ) {
		fprintf( stderr, "ERROR: Starting worker failed: %s\n", error->message );
		g_strfreev( args );
		return NULL;
	}

	printf( "Worker child with PID %d created: %s.\n",
		wed->pid, worker->command );
	
	GMainContext *context = g_main_context_default( );
	wed->main_loop = g_main_loop_new( context, TRUE );
                                                                                             
	g_child_watch_add( wed->pid, (GChildWatchFunc)watch_child, (gpointer *)wed );


#ifdef G_OS_WIN32
	wed->out_channel = g_io_channel_win32_new_fd( wed->out );
	wed->err_channel = g_io_channel_win32_new_fd( wed->err );
#else
	wed->out_channel = g_io_channel_unix_new( wed->out );
	wed->err_channel = g_io_channel_unix_new( wed->err );
#endif

	GIOCondition cond = (GIOCondition)( G_IO_IN | G_IO_HUP );
	g_io_add_watch( wed->out_channel, cond, (GIOFunc)watch_output, (gpointer *)wed );
	g_io_add_watch( wed->err_channel, cond, (GIOFunc)watch_output, (gpointer *)wed );

	worker->state = WORKER_STATE_RUNNING;

	printf( "worker %s: entering glib worker main loop..\n", worker->name );
	g_main_loop_run( wed->main_loop );
	printf( "worker %s: leaving glib worker main loop..\n", worker->name );

	worker->state = WORKER_STATE_STOPPED;

	free( worker->execution_data );
	worker->execution_data = NULL;

	g_strfreev( args );
	
	return NULL;
}
                    
int worker_init( worker_t *worker )
{
	switch( worker->state ) {
		case WORKER_STATE_RUNNING:
			fprintf( stderr, "worker %s is already running\n", worker->name );
			return 0;
			
		case WORKER_STATE_STOPPED:
			// execute, skip
			break;
	}
	
	switch( worker->mode ) {
		case WORKER_EXECUTION_DIRECT:
			// execute, skip
			break;
			
		case WORKER_EXECUTION_DISABLED:
			fprintf( stderr, "worker %s is disabled, won't execute\n", worker->name );
			return 0;
	}

	pthread_attr_t attr;
	int res;
	
	res = pthread_attr_init( &attr );
	if( res != 0 ) {
		return 1;
	}

	worker->execution_data = malloc( sizeof( direct_glib_execution_worker_data_t ) );
	direct_glib_execution_worker_data_t *wed = 
		(direct_glib_execution_worker_data_t *)worker->execution_data;
	wed->worker = worker;
	
	res = pthread_create( &wed->thread, &attr, worker_func, (void *)wed );
	if( res != 0 ) {
		return 1;
	}
	
	pthread_detach( wed->thread );

	return 0;
}

void worker_terminate( worker_t *worker )
{
	direct_glib_execution_worker_data_t *wed = (direct_glib_execution_worker_data_t *)worker->execution_data;

	switch( worker->state ) {
		case WORKER_STATE_RUNNING:
			break;
			
		case WORKER_STATE_STOPPED:
			fprintf( stderr, "worker %s is already stopped\n", worker->name );
			return;
	}

#ifndef _WIN32
	(void)kill( wed->pid, SIGTERM );
#else
	(void)TerminateProcess( wed->pid, 0 );
#endif
}

int worker_free( worker_t *worker )
{
	return 0;
}