-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.js
226 lines (203 loc) · 8.14 KB
/
controller.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
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
const child_process = require("child_process")
const fs = require('fs')
const path = require('path')
const dateformat = require('dateformat')
const schedule = require("node-schedule")
const db = require('./db')
const providers = require('./providers')
const config = require("./config")
const queue = require("async").queue;
var AsyncLock = require('async-lock');
const { timeStamp } = require("console")
function getLogPath(id) {
fs.mkdirSync(path.join(config.log_dir, id), { recursive: true })
return path.join(config.log_dir, id,
`${dateformat(new Date(), 'yyyy-mm-dd-HH-MM-ss')}.log`)
}
function placeholder() { }
class Controller {
constructor() {
this.timeout = 3000
this.jobs = {}
this.lock = new AsyncLock();
this.run_queue = queue(
(fun, next) => { fun(next) },
config.concurrency);
var provider_all_params = "";
for (var i in config.mirrors) {
if (config.mirrors[i].id=="all") provider_all_params = config.mirrors[i].params;
}
this.static_mirror_list = {}
for (var i in config.mirrors) {
// init database entry
var t = config.mirrors[i]
if (t.id=="all") { continue; }
console.log(`adding job ${t.id}`)
if (t.hasOwnProperty('metadata')) {
this.static_mirror_list[t.id] = t.metadata
}
(async (id) => {
if (await db.exists(id)) {
if (await db.get('state', id) === 'sync') {
db.set('state', 'error', id)
}
} else {
db.insert(id)
}
})(t.id);
if (provider_all_params=="") {
this.jobs[t.id] = providers[t.provider](t.params, t.id, provider_all_params)
} else {
this.jobs[t.id] = providers["rsync"](t.params, t.id, provider_all_params)
}
console.log(`Sync cmd: ${this.jobs[t.id]}`);
console.log(this.jobs[t.id]);
this.jobs[t.id]['sched'] = schedule.scheduleJob(
t.schedule,
((id) => { return () => { this.start(id); } })(t.id));
}
//schedule.scheduleJob("*/10 * * * *", (() => { return () => { this.searchall(); } })());
if (config.hasOwnProperty('nginx-index-port')) {
this.nginx_conf = ''
this.nginx_conf += 'server {\n'
this.nginx_conf += `\tlisten ${config['nginx-index-port']};\n`
this.nginx_conf += `\tlisten [::]:${config['nginx-index-port']};\n`
this.nginx_conf += '\troot /data/repos/;\n'
this.nginx_conf += ` location /pypi/simple/ {
alias /data/repos/pypi/web/simple/;
autoindex on;
autoindex_format json;
}
location /pypi/json/ {
alias /data/repos/pypi/web/json/;
autoindex on;
autoindex_format json;
}
location /pypi/packages/ {
alias /data/repos/pypi/web/packages/;
autoindex on;
autoindex_format json;
}
location /pypi/pypi/ {
alias /data/repos/pypi/web/pypi/;
autoindex on;
autoindex_format json;
}\n`
for (var i in config.mirrors) {
// init nginx settings
var t = config.mirrors[i]
console.log(`generating nginx configure for ${t.id}`)
if (t.hasOwnProperty('file-show') && t['file-show'] === false) {
continue
}
if (t.hasOwnProperty('metadata') && t.metadata.hasOwnProperty('url')) {
this.nginx_conf += `\tlocation ${t.metadata.url} {\n`
if (t.hasOwnProperty('file-custom')) {
for (var j in t['file-custom']) {
this.nginx_conf += `\t\t${t['file-custom'][j]}`
if (t['file-custom'][j].endsWith(';') === false) {
t.nginx_conf += ';'
}
t.nginx_conf += '\n'
}
}
this.nginx_conf += '\t\tautoindex on;\n'
this.nginx_conf += '\t\tautoindex_format json;\n'
this.nginx_conf += '\t}\n'
}
}
this.nginx_conf += '}\n'
fs.writeFileSync('./nginx_index.conf', this.nginx_conf)
}
if (config.hasOwnProperty('rsyncd-port')) {
this.rsyncd_conf = ''
this.rsyncd_conf += ''
for (var i in config.mirrors) {
var t = config.mirrors[i]
console.log(`generating rsyncd configure for ${t.id}`)
this.rsyncd_conf += `[${t.id}]\n`
this.rsyncd_conf += `comment = ${t.metadata.describe}\n`
this.rsyncd_conf += 'path = ' + (t.params.dest || `${path.join(config.repo_dir,t.id)}`) + '\n'
this.rsyncd_conf += '\n'
}
fs.writeFileSync('./rsyncd.conf', this.rsyncd_conf)
}
}
exec(args, logPath, id) {
var job = this.jobs[id]
return new Promise((resolve, reject) => {
var logStream = fs.createWriteStream(logPath, { flags: 'a' });
if(id == 'ubuntu'){
job['proc'] = child_process.exec(
args.join(' ')+' ; '+ args.join(' '), { maxBuffer: 1024 * 1024 * 1024 , timeout: 1000*60*60*48}, (err, stdout, stderr) => { job['proc'] = undefined; resolve(err) }
)
}else{
job['proc'] = child_process.exec(
args.join(' '), { maxBuffer: 1024 * 1024 * 1024 , timeout: 1000*60*60*48}, (err, stdout, stderr) => { job['proc'] = undefined; resolve(err) }
)
}
job['proc'].stdout.on('data', (data) => {
logStream.write(data)
})
job['proc'].stderr.on('data', (data) => {
logStream.write(data)
})
})
}
async start(id) {
if (await db.get('state', id) !== 'sync') {
this.run_queue.push(async (callback) => {
console.log(`running job ${id}`)
var logPath = getLogPath(id)
var job = this.jobs[id]
db.set('nextSyncTime',
job.sched.nextInvocation().getTime(), id)
db.set('laststate', await db.get('state',id), id)
db.set('state', 'sync', id)
db.set('logPath', logPath, id)
this.exec(job.args, logPath, id)
.then(async (err) => {
console.log(`job ${id} done with exit msg '${err}'`)
if(err){
try {
var out = fs.readFileSync(await db.get('logPath',id));
if(out.includes("@ERROR: max connections") && (new Date().getTime() - await db.get('lastSyncTime',id) <= 172800000) ){
db.set('state', await db.get('laststate',id), id)
}else{
db.set('state', 'error', id)
}
} catch {
db.set('state', 'error', id)
}
}else{
db.set('lastSyncTime', new Date().getTime(), id);
db.set('state', 'done', id)
}
(job.after || placeholder)(err)
}).then(callback)
})
}
}
async stop(id) {
if (await db.get('state', id) === 'sync') {
var job = this.jobs[id]
if (job.proc) {
job.proc.kill('SIGTERM')
setTimeout(() => {
if (job.proc)
job.proc.kill('SIGKILL')
}, this.timeout)
}
}
}
async searchall(){
for (var i in config.mirrors) {
var id = config.mirrors[i].id;
if (await db.get('state', id) === 'error' && this.run_queue.length() < config.concurrency -1) {
console.log(`rerunning job ${id}`);
await this.start(id);
}
}
}
}
module.exports = new Controller()