-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager.js
165 lines (143 loc) · 3.92 KB
/
manager.js
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
const Redis = require('ioredis');
const Docker = require('dockerode');
const Stream = require('stream');
const IO = require('socket.io');
const Fs = require('fs');
const Request = require('request');
//socket.io init
const io = IO.listen(8000);
let store = {};
io.on('connection', (socket) => {
console.log('connect');
socket.on('register', (msg) => {
const usrobj = {
'xid': msg.xid
};
store[msg.xid] = usrobj;
socket.join(msg.xid);
console.log('connection accept');
console.log('xid: ' + msg.xid);
console.log(store);
});
socket.on('process-message', (msg) => {
io.to(store[msg.xid].xid).emit('process-message', msg.body);
});
});
//Docker init
const docker = new Docker({
socketPath: '/var/run/docker.sock'
});
//redis init
const redisConfig = require('./redis-config.json');
const redis = new Redis(redisConfig);
const sub = new Redis(redisConfig);
const pub = new Redis(redisConfig);
let waiting = 0;
let processing = 0;
const maxProcessing = 4;
//start redus subscribe
sub.subscribe('waiting-queue-event', 'processing-queue-event', 'next-process-event', (err, count) => {
if (err) {
console.log(err+':'+count);
}
});
sub.on('message', (channel, message) => {
console.log('Receive %s, %s', channel, message);
switch (channel) {
case 'waiting-queue-event':
waiting += 1;
if (processing < maxProcessing) {
waiting -= 1;
redis.pipeline().lpop('waitQueue').exec().then((result) => {
pub.publish('processing-queue-event', result[0][1]);
console.log(result[0]);
});
}
break;
case 'processing-queue-event':
//create and run container
docker.createContainer({
Image: 'gw000/keras-full',
Cmd: ['python', '/src/'+message+'.py'],
'Volumes': {
'/src': {}
},
'HostConfig': {
'Binds': [ __dirname+'/examples:/src'],
'Dns': ['8.8.8.8']
},
'DefaultRuntime': 'nvidia'
}, (err, container) => {
console.log(err);
container.start({}, (err, data) => {
containerLogs(container, message);
});
});
processing += 1;
break;
case 'next-process-event':
processing -= 1;
redis.pipeline().lpop('waitQueue').exec().then((result) => {
if (result[0][1] != null) {
pub.publish('processing-queue-event', result[0][1]);
}
});
break;
}
});
const containerLogs = (container, transaction) => {
console.log('docker container xid:' + transaction);
let loss = null;
let val_loss = null;
const logStream = new Stream.PassThrough();
logStream.on('data', (chunk) => {
//add websocket function
io.emit('process-message', {
xid: transaction,
body: chunk.toString('utf8')
});
console.log(chunk.toString('utf8'));
if (chunk.toString('utf8').match(/gb_loss:/)) {
loss = chunk.toString('utf8').split(':')[1];
}
if (chunk.toString('utf8').match(/gb_val_loss:/)) {
val_loss = chunk.toString('utf8').split(':')[1];
}
});
container.logs({
follow: true,
stdout: true,
stderr: true
}, (err, stream) => {
if(err) {
console.log(err);
}
container.modem.demuxStream(stream, logStream, logStream);
stream.on('end', () => {
//and add process next step from waiting queue
Fs.unlink('examples/'+transaction+'.py', (err) => {
if (err)
console.log(err);
});
pub.publish('next-process-event', 'next process start');
logStream.end('process finish');
const options = {
url: 'http://localhost/api/data',
method: 'POST',
json: true,
headers: {
'Content-Type':'application/json'
},
form: {
'xid': transaction,
'loss': loss,
'val_loss': val_loss
}
}
Request(options, (error, response, body) => {
console.log(body);
console.log(error)
});
});
});
};