forked from Chinachu/Chinachu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-operator.js
592 lines (489 loc) · 16.2 KB
/
app-operator.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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*!
* Chinachu Task Operator Service (chinachu-operator)
*
* Copyright (c) 2016 Yuki KAN and Chinachu Project Contributors
* https://chinachu.moe/
**/
'use strict';
process.env.PATH = `${__dirname}/usr/bin:${process.env.PATH}`;
const CONFIG_FILE = __dirname + '/config.json';
const RESERVES_DATA_FILE = __dirname + '/data/reserves.json';
const RECORDING_DATA_FILE = __dirname + '/data/recording.json';
const RECORDED_DATA_FILE = __dirname + '/data/recorded.json';
// 標準モジュールのロード
const path = require('path');
const url = require('url');
const fs = require('fs');
const util = require('util');
const child_process = require('child_process');
// ディレクトリチェック
if (!fs.existsSync('./data/') || !fs.existsSync('./log/') || !fs.existsSync('./web/')) {
console.error('必要なディレクトリが存在しないか、カレントワーキングディレクトリが不正です。');
process.exit(1);
}
// 終了処理
process.on('SIGQUIT', () => {
setTimeout(() => {
process.exit(0);
}, 0);
});
// 例外処理
process.on('uncaughtException', (err) => {
console.error('uncaughtException: ' + err.stack);
});
// 追加モジュールのロード
const dateFormat = require('dateformat');
const mkdirp = require('mkdirp');
const Mtwitter = require('mtwitter');
const disk = require('diskusage');
const nodemailer = require("nodemailer");
const sendmail = require("nodemailer-sendmail-transport");
const chinachu = require('chinachu-common');
const mirakurun = new (require("mirakurun").default)();
//
let reserves = [];
let recorded = [];
let recording = [];
// 設定の読み込み
const pkg = require("./package.json");
const config = require(CONFIG_FILE);
// settings
const schedulerIntervalTime = 1000 * 60 * 10;// 最長10分毎
const notifyIntervalTime = 1000 * 60 * 60 * 3;// 3時間毎
const prepTime = 1000 * 15;// 15秒前
const recordingPriority = config.recordingPriority || 2;
const conflictedPriority = config.conflictedPriority || 1;
const storageLowSpaceThresholdMB = config.storageLowSpaceThresholdMB || 3000;// 3 GB
const storageLowSpaceAction = config.storageLowSpaceAction || "remove"; // "none" | "stop" | "remove"
const storageLowSpaceNotifyTo = config.storageLowSpaceNotifyTo;// e-mail address
const storageLowSpaceCommand = config.storageLowSpaceCommand || null;// command
// setuid
if (process.platform !== "win32") {
if (process.getuid() === 0) {
if (typeof config.gid === "string" || typeof config.gid === "number") {
process.setgid(config.gid);
} else {
process.setgid('video');
}
if (typeof config.uid === "string" || typeof config.uid === "number") {
process.setuid(config.uid);
} else {
console.error("[fatal] 'uid' required in config.");
process.exit(1);
}
}
}
// Mirakurun Client
const mirakurunPath = config.mirakurunPath || config.schedulerMirakurunPath || "http+unix://%2Fvar%2Frun%2Fmirakurun.sock/";
if (/(?:\/|\+)unix:/.test(mirakurunPath) === true) {
const standardFormat = /^http\+unix:\/\/([^\/]+)(\/?.*)$/;
const legacyFormat = /^http:\/\/unix:([^:]+):?(.*)$/;
if (standardFormat.test(mirakurunPath) === true) {
mirakurun.socketPath = mirakurunPath.replace(standardFormat, "$1").replace(/%2F/g, "/");
mirakurun.basePath = path.join(mirakurunPath.replace(standardFormat, "$2"), mirakurun.basePath);
} else {
mirakurun.socketPath = mirakurunPath.replace(legacyFormat, "$1");
mirakurun.basePath = path.join(mirakurunPath.replace(legacyFormat, "$2"), mirakurun.basePath);
}
} else {
const urlObject = url.parse(mirakurunPath);
mirakurun.host = urlObject.hostname;
mirakurun.port = urlObject.port;
mirakurun.basePath = path.join(urlObject.pathname, mirakurun.basePath);
}
mirakurun.userAgent = `Chinachu/${pkg.version} (operator)`;
mirakurun.priority = recordingPriority;
console.info(mirakurun);
// sendmail
const transporter = nodemailer.createTransport(sendmail());
// 録画中リストをクリア
fs.writeFileSync(RECORDING_DATA_FILE, '[]');
// 保存先ディレクトリが存在しない場合には作成
if (!fs.existsSync(config.recordedDir)) {
util.log('MKDIR: ' + config.recordedDir);
mkdirp.sync(config.recordedDir);
}
// Tweeter (Experimental)
let tweeter, tweeterUpdater;
if (config.operTweeter && config.operTweeterAuth && config.operTweeterFormat) {
tweeter = new Mtwitter({
consumer_key : config.operTweeterAuth.consumerKey,
consumer_secret : config.operTweeterAuth.consumerSecret,
access_token_key : config.operTweeterAuth.accessToken,
access_token_secret: config.operTweeterAuth.accessTokenSecret
});
tweeterUpdater = (status) => {
tweeter.post(
'/statuses/update',
{ status: status },
(err, item) => {
if (err) {
util.log('[Tweeter] Error: ' + JSON.stringify(err));
} else {
util.log('[Tweeter] Updated: ' + status);
}
}
);
};
}
let clock = Date.now();
let scheduler = null;
let scheduled = 0;
let stChecked = 0;
let stNotified = 0;
// メインループ
setInterval(() => {
clock = Date.now();
for (let i = 0, l = reserves.length; i < l; i++) {
reservesChecker(reserves[i]);
}
}, 1000 * 3);
// 裏ループ
setInterval(() => {
if ((scheduler === null) && (clock - scheduled > schedulerIntervalTime)) {
startScheduler();
scheduled = clock;
}
if (clock - stChecked > 1000 * 20) {
storageChecker();
stChecked = clock;
}
}, 1000 * 6);
// 予約時間チェック
function reservesChecker(program) {
// スキップする
if (program.isSkip) {
return;
}
// 予約時間超過
if (clock > program.end) {
return;
}
// 予約準備時間内
if (program.start - clock < prepTime) {
if (isRecording(program) === false) {
prepRecord(program);
}
}
}
// 録画中か
function isRecording(program) {
for (let i = 0, l = recording.length; i < l; i++) {
if (recording[i].id === program.id) {
return true;
}
}
return false;
}
// 録画したか
function isRecorded(program) {
for (let i = 0, l = recorded.length; i < l; i++) {
if (recorded[i].id === program.id && clock < recorded[i].end) {
return true;
}
}
return false;
}
// 録画中の番組を更新
function recordingUpdater(program) {
for (let i = 0, l = recording.length; i < l; i++) {
if (recording[i].id === program.id) {
for (let k in program) {
if (program.hasOwnProperty(k)) {
recording[i][k] = program[k];
}
}
return;
}
}
}
// スケジューラーを停止
function stopScheduler() {
process.removeListener('SIGINT', stopScheduler);
process.removeListener('SIGQUIT', stopScheduler);
process.removeListener('SIGTERM', stopScheduler);
if (scheduler === null) { return; }
scheduler.kill('SIGQUIT');
util.log('KILL: SIGQUIT -> Scheduler (pid=' + scheduler.pid + ')');
}
// スケジューラーを開始
function startScheduler() {
if (scheduler !== null) { return; }
var output, finalize;
scheduler = child_process.spawn('./chinachu', [ 'update' ]);
util.log('SPAWN: ./chinachu update (pid=' + scheduler.pid + ')');
// ログ用
output = fs.createWriteStream('./log/scheduler', { flags: 'a' });
util.log('STREAM: ./log/scheduler');
finalize = function () {
util.log('EXIT: node app-scheduler.js (pid=' + scheduler.pid + ')');
try {
process.removeListener('SIGINT', stopScheduler);
process.removeListener('SIGQUIT', stopScheduler);
process.removeListener('SIGTERM', stopScheduler);
} catch (e) {}
try { output.end(); } catch (ee) {}
scheduler = null;
};
scheduler.stdout.on('data', function (data) {
try {
output.write(data);
} catch (e) {
util.log('ERROR: Scheduler -> Abort (' + e + ')');
finalize();
}
});
scheduler.once('exit', finalize);
process.once('SIGINT', stopScheduler);
process.once('SIGQUIT', stopScheduler);
process.once('SIGTERM', stopScheduler);
}
// 番組ログ用
function printProgram(program) {
return `#${program.id} ${dateFormat(new Date(program.start), "isoDateTime")} [${program.channel.name}] ${program.title}`
}
// 録画準備
function prepRecord(program) {
if (clock > program.end) {
return;
}
util.log('PREPARE: ' + printProgram(program));
// set priority
mirakurun.priority = program.priority = program.priority || (program.isConflict ? conflictedPriority : recordingPriority);
// get stream
mirakurun.getProgramStream(parseInt(program.id, 36), true)
.then(stream => doRecord(program, stream))
.catch(err => {
if (program._stream) {
// 既に録画開始
return;
}
if (err.req) {
util.log("ERROR: " + printProgram(program), err.req.path, err.statusCode, err.statusMessage);
} else {
util.log("ERROR: " + printProgram(program), err.address, err.code);
}
// リトライ
setTimeout(() => {
recording.splice(recording.indexOf(program), 1);
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
}, 5000);
});
recording.push(program);
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
}
// 録画実行
function doRecord(program, stream) {
util.log('RECORD: ' + printProgram(program));
// dummy
program.tuner = {
name: `Mirakurun (${mirakurun.host ? mirakurun.host : "UnixSocket"})`,
command: "*",
isScrambling: false
};
program.command = `mirakurun type=${program.channel.type} url=${stream.req.path} priority=${program.priority}`;// dummy
program.pid = -1;// dummy
// 保存先パス
const recPath = config.recordedDir + chinachu.formatRecordedName(program, program.recordedFormat || config.recordedFormat);
program.recorded = recPath;
// 保存先ディレクトリ
const recDirPath = recPath.replace(/^(.+)\/.+$/, '$1');
if (!fs.existsSync(recDirPath)) {
util.log('MKDIR: ' + recDirPath);
mkdirp.sync(recDirPath);
}
// 保存ストリーム
const recFile = fs.createWriteStream(recPath, { flags: 'a' });
util.log('STREAM: ' + recPath);
stream.pipe(recFile);
// 録画プロセス終了時処理
stream.once('end', finalize);
// 終了シグナル時処理
process.on('SIGINT', finalize);
process.on('SIGQUIT', finalize);
process.on('SIGTERM', finalize);
// 状態更新
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
// 内部用
Object.defineProperty(program, "_stream", {
enumerable: false,
value: stream
});
// Tweeter (Experimental)
if (tweeter && config.operTweeterFormat.start) {
tweeterUpdater(
config.operTweeterFormat.start
.replace('<id>', program.id)
.replace('<type>', program.channel.type)
.replace('<channel>', ((program.channel.type === 'CS') ? program.channel.sid : program.channel.channel))
.replace('<title>', program.title)
.replace('<fullTitle>', program.fullTitle)
.replace('<channelname>', program.channel.name)
.replace('<date>', dateFormat(new Date(program.start), "mm/dd"))
.replace('<starttime>', dateFormat(new Date(program.start), "h:MM"))
.replace('<endtime>', dateFormat(new Date(program.end), "h:MM"))
);
}
// お片付け
function finalize() {
stream.unpipe();
stream.req.abort();
process.removeListener('SIGINT', finalize);
process.removeListener('SIGQUIT', finalize);
process.removeListener('SIGTERM', finalize);
// 書き込みストリームを閉じる
recFile.end();
// 状態を更新
delete program.pid;
for (let i = 0, l = recorded.length; i < l; i++) {
if (recorded[i].id === program.id) {
if (recorded[i].recorded === program.recorded) {
recorded.splice(i, 1);
} else {
recorded[i].id += '-' + recorded[i].start.toString(36);
}
break;
}
}
recorded.push(program);
recording.splice(recording.indexOf(program), 1);
fs.writeFileSync(RECORDED_DATA_FILE, JSON.stringify(recorded));
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDED_DATA_FILE);
util.log('WRITE: ' + RECORDING_DATA_FILE);
if (program.isManualReserved) {
for (let i = 0, l = reserves.length; i < l; i++) {
if (reserves[i].id === program.id) {
reserves.splice(i, 1);
fs.writeFileSync(RESERVES_DATA_FILE, JSON.stringify(reserves));
util.log('WRITE: ' + RESERVES_DATA_FILE);
break;
}
}
}
// ポストプロセス
if (config.recordedCommand) {
const postProcess = child_process.spawn(config.recordedCommand, [recPath, JSON.stringify(program)]);
util.log('SPAWN: ' + config.recordedCommand + ' (pid=' + postProcess.pid + ')');
}
// Tweeter (Experimental)
if (tweeter && config.operTweeterFormat.end) {
tweeterUpdater(
config.operTweeterFormat.end
.replace('<id>', program.id)
.replace('<type>', program.channel.type)
.replace('<channel>', ((program.channel.type === 'CS') ? program.channel.sid : program.channel.channel))
.replace('<title>', program.title)
.replace('<fullTitle>', program.fullTitle)
.replace('<channelname>', program.channel.name)
.replace('<date>', dateFormat(new Date(program.start), "mm/dd"))
.replace('<starttime>', dateFormat(new Date(program.start), "h:MM"))
.replace('<endtime>', dateFormat(new Date(program.end), "h:MM"))
);
}
finalize = null;
util.log('FIN: ' + printProgram(program));
}
}
// 録画中止
function stopRecording(programId) {
const program = recording.find(program => program.id === programId);
if (program && program._stream) {
program._stream.req.abort();
}
}
// ストレージチェック
function storageChecker() {
disk.check(config.recordedDir, (err, info) => {
if(err) {
return;
}
const freeMB = info.available / 1024 / 1024;
if (freeMB < storageLowSpaceThresholdMB) {
stChecked = 0;// すぐに再チェックするため
util.log(`ALERT: Storage Low Space! (${freeMB} MB < ${storageLowSpaceThresholdMB} MB)`);
// 1. 指定コマンド実行
if (storageLowSpaceCommand) {
const command = child_process.spawn(storageLowSpaceCommand);
util.log('SPAWN: ' + storageLowSpaceCommand + ' (pid=' + command.pid + ')');
}
// 2. アクション
if (storageLowSpaceAction === "stop") {
// 録画停止
recording.forEach(program => stopRecording(program.id));
} else if (storageLowSpaceAction === "remove") {
// 削除
if (recorded.length > 0) {
const program = recorded.shift();
if (fs.existsSync(program.recorded) === true) {
fs.unlinkSync(program.recorded);
}
fs.writeFileSync(RECORDED_DATA_FILE, JSON.stringify(recorded));
util.log('WRITE: ' + RECORDED_DATA_FILE);
}
}
// 3. メール通知
if (storageLowSpaceNotifyTo && clock - stNotified > notifyIntervalTime) {
stNotified = clock;
transporter.sendMail({
from: "Chinachu <chinachu@localhost>",
to: storageLowSpaceNotifyTo,
subject: "[Chinachu] ALERT: Storage Low Space!",
text: `Current Free Space is ${freeMB} MB.\nThreshold is ${storageLowSpaceThresholdMB} MB.`
}, (err, info) => {
if (err) {
console.log(err);
}
});
}
}
});
}
// ファイル更新監視: ./data/reserves.json
chinachu.jsonWatcher(
RESERVES_DATA_FILE,
(err, data, mes) => {
if (err) {
console.error(err);
return;
}
reserves = data;
util.log(mes);
if (recording.length > 0) {
reserves.forEach(recordingUpdater);
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
}
},
{ create: [], now: true }
);
// ファイル更新監視: ./data/recorded.json
chinachu.jsonWatcher(
RECORDED_DATA_FILE,
(err, data, mes) => {
if (err) {
console.error(err);
return;
}
recorded = data;
util.log(mes);
},
{ create: [], now: true }
);
// ファイル更新監視: ./data/recording.json
chinachu.jsonWatcher(
RECORDING_DATA_FILE,
(err, data, mes) => {
if (err) {
console.error(err);
return;
}
// 録画中止処理
data.filter(program => !!program.abort).forEach(program => {
stopRecording(program.id);
});
},
{ create: [], now: false }
);