forked from Chinachu/Chinachu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-operator.js
564 lines (477 loc) · 16.3 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
/*!
* Chinachu Task Operator Service (chinachu-operator)
*
* Copyright (c) 2012 Yuki KAN and Chinachu Project Contributors
* https://chinachu.moe/
**/
/*jslint node:true, nomen: true, plusplus: true, regexp: true */
'use strict';
var CONFIG_FILE = __dirname + '/config.json';
var RESERVES_DATA_FILE = __dirname + '/data/reserves.json';
var RECORDING_DATA_FILE = __dirname + '/data/recording.json';
var RECORDED_DATA_FILE = __dirname + '/data/recorded.json';
// 標準モジュールのロード
var path = require('path');
var fs = require('fs');
var util = require('util');
var child_process = require('child_process');
// ディレクトリチェック
if (!fs.existsSync('./data/') || !fs.existsSync('./log/') || !fs.existsSync('./web/')) {
console.error('必要なディレクトリが存在しないか、カレントワーキングディレクトリが不正です。');
process.exit(1);
}
// 終了処理
process.on('SIGQUIT', function () {
setTimeout(function () {
process.exit(0);
}, 0);
});
// 例外処理
process.on('uncaughtException', function (err) {
console.error('uncaughtException: ' + err.stack);
});
// 追加モジュールのロード
var dateFormat = require('dateformat');
var mkdirp = require('mkdirp');
var Mtwitter = require('mtwitter');
var chinachu = require('chinachu-common');
//
var reserves = [];
var recorded = [];
var recording = [];
// 設定の読み込み
var config = require(CONFIG_FILE);
// 録画中リストをクリア
fs.writeFileSync(RECORDING_DATA_FILE, '[]');
// 保存先ディレクトリが存在しない場合には作成
if (!fs.existsSync(config.recordedDir)) {
util.log('MKDIR: ' + config.recordedDir);
mkdirp.sync(config.recordedDir);
}
// Tweeter (Experimental)
if (config.operTweeter && config.operTweeterAuth && config.operTweeterFormat) {
var 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
});
var tweeterUpdater = function (status) {
tweeter.post(
'/statuses/update',
{ status: status },
function _onUpdatedTweeter(err, item) {
if (err) {
util.log('[Tweeter] Error: ' + JSON.stringify(err));
} else {
util.log('[Tweeter] Updated: ' + status);
}
}
);
};
}
//
var schedulerProcessTime = config.operSchedulerProcessTime || 1000 * 60 * 20;//20分
var schedulerIntervalTime = config.operSchedulerIntervalTime || 1000 * 60 * 60;//60分
var schedulerSleepStartHour = config.operSchedulerSleepStartHour || 1;
var schedulerSleepEndHour = config.operSchedulerSleepEndHour || 5;
var schedulerEpgRecordTime = config.schedulerEpgRecordTime || 60;
var prepTime = config.operRecPrepTime || 1000 * 60;//60秒
var offsetStart = (typeof config.operRecOffsetStart !== 'undefined' ? config.operRecOffsetStart : 1000 * 5);
var offsetEnd = (typeof config.operRecOffsetEnd !== 'undefined' ? config.operRecOffsetEnd : -(1000 * 8));
var clock = Date.now();
var next = 0;
var scheduler = null;
var scheduled = 0;
// 録画コマンドのシリアライズ
var operRecCmdSpan = config.operRecCmdSpan || 0;
if (operRecCmdSpan < 0) {
operRecCmdSpan = 0;
}
var recCmdLastTime = Date.now();
function execRecCmd(cmd, timeout, msg) {
if (timeout > 0) {
setTimeout(execRecCmd, timeout, cmd, 0, msg);
return;
}
var t = operRecCmdSpan - (Date.now() - recCmdLastTime);
if (t > 0) {
util.log(msg + ': ' + t + 'ms');
setTimeout(execRecCmd, t, cmd, 0, msg);
return;
}
cmd();
recCmdLastTime = Date.now();
}
// 録画中か
function isRecording(program) {
var i, l;
for (i = 0, l = recording.length; i < l; i++) {
if (recording[i].id === program.id) {
return true;
}
}
return false;
}
// 録画したか
function isRecorded(program) {
var i, l;
for (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) {
var i, l, k;
for (i = 0, l = recording.length; i < l; i++) {
if (recording[i].id === program.id) {
for (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(channel) {
if ((scheduler !== null) || (recording.length !== 0)) { return; }
var output, finalize;
if (channel) {
scheduler = child_process.spawn('node', [ 'app-scheduler.js', '-f', '-ch', channel ]);
util.log('SPAWN: node app-scheduler.js -f -ch ' + channel + ' (pid=' + scheduler.pid + ')');
} else {
scheduler = child_process.spawn('node', [ 'app-scheduler.js', '-f' ]);
util.log('SPAWN: node app-scheduler.js -f (pid=' + scheduler.pid + ')');
}
// ログ用
output = fs.createWriteStream('./log/scheduler', { flags: 'a' });
util.log('STREAM: ./log/scheduler');
finalize = function () {
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 doRecord(program) {
var timeout, tuner, recPath, recDirPath, recCmd, recProc, recFile, /*epgInterval, */finalize;
util.log('RECORD: ' + dateFormat(new Date(program.start), 'isoDateTime') + ' [' + program.channel.name + '] ' + program.title);
timeout = program.end - Date.now() + offsetEnd;
if (timeout < 0) {
util.log('FATAL: 時間超過による録画中止');
// 状態を更新
recording.splice(recording.indexOf(program), 1);
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
return;
}
// チューナーを選ぶ
tuner = chinachu.getFreeTunerSync(config.tuners, program.channel.type, false, 2);
// チューナーが見つからない
if (tuner === null) {
util.log('WARNING: ' + program.channel.type + ' 利用可能なチューナーが見つかりません (存在しないかロックされています) (5秒後に再試行)');
setTimeout(function () {
doRecord(program);
}, 5000);
return;
}
// チューナーをロック
try {
chinachu.lockTunerSync(tuner, 2);
} catch (e) {
util.log('WARNING: チューナー(' + tuner.n + ')のロックに失敗しました');
}
util.log('LOCK: ' + tuner.name + ' (n=' + tuner.n + ')');
program.tuner = tuner;
// 保存先パス
recPath = config.recordedDir + chinachu.formatRecordedName(program, program.recordedFormat || config.recordedFormat);
program.recorded = recPath;
// 保存先ディレクトリ
recDirPath = recPath.replace(/^(.+)\/.+$/, '$1');
if (!fs.existsSync(recDirPath)) {
util.log('MKDIR: ' + recDirPath);
mkdirp.sync(recDirPath);
}
// 録画コマンド
recCmd = tuner.command;
recCmd = recCmd.replace('<channel>', program.channel.channel);
recCmd = recCmd.replace('<type>', program.channel.type);
if (program['1seg'] === true) {
recCmd = recCmd.replace(' --b25', '');
recCmd = recCmd.replace('<sid>', '1seg');
} else {
recCmd = recCmd.replace('<sid>', program.channel.sid);
}
program.command = recCmd;
execRecCmd(function () {
// 録画プロセスを生成
recProc = child_process.spawn(recCmd.split(' ')[0], recCmd.replace(/[^ ]+ /, '').split(' '));
chinachu.writeTunerPidSync(tuner, recProc.pid, 2);
util.log('SPAWN: ' + recCmd + ' (pid=' + recProc.pid + ')');
program.pid = recProc.pid;
// 状態保存
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
// 書き込みストリームを作成
recFile = fs.createWriteStream(recPath, { flags: 'a' });
util.log('STREAM: ' + recPath);
// ts出力
recProc.stdout.pipe(recFile);
// ログ出力
recProc.stderr.on('data', function (data) {
util.log('#' + (recCmd.split(' ')[0] + ': ' + data).replace(/\n/g, ' ').trim());
});
// お片付け
finalize = function () {
var i, l, postProcess;
process.removeListener('SIGINT', finalize);
process.removeListener('SIGQUIT', finalize);
process.removeListener('SIGTERM', finalize);
recProc.stdout.removeAllListeners();
// 書き込みストリームを閉じる
recFile.end();
// チューナーのロックを解除
try {
chinachu.unlockTunerSync(tuner);
util.log('UNLOCK: ' + tuner.name + ' (n=' + tuner.n + ')');
} catch (e) {
util.log(e);
}
// EPG処理を終了
//clearInterval(epgInterval);
// 状態を更新
delete program.pid;
for (i = 0, l = recorded.length; i < l; i++) {
if (recorded[i].id === program.id) {
recorded[i].id = recorded[i].id.replace(/^([^\-]+)\-(.+)$/, '$1-_$2');
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 (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) {
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;
};
// 録画プロセス終了時処理
recProc.on('exit', finalize);
// 終了シグナル時処理
process.on('SIGINT', finalize);
process.on('SIGQUIT', finalize);
process.on('SIGTERM', finalize);
// 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"))
);
}
}, 0, 'RECWAIT: ' + tuner.name);
}
// 録画準備
function prepRecord(program) {
util.log('PREPARE: ' + dateFormat(new Date(program.start), 'isoDateTime') + ' [' + program.channel.name + '] ' + program.title);
program.isSigTerm = false;
recording.push(program);
var timeout = program.start - clock - offsetStart;
if (timeout < 0) { timeout = 3000; }
setTimeout(function () {
doRecord(program);
}, timeout);
fs.writeFileSync(RECORDING_DATA_FILE, JSON.stringify(recording));
util.log('WRITE: ' + RECORDING_DATA_FILE);
if (scheduler !== null) {
stopScheduler();
}
// Tweeter (Experimental)
if ((timeout !== 0) && tweeter && config.operTweeterFormat.prepare) {
tweeterUpdater(
config.operTweeterFormat.prepare
.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 reservesChecker(program, i) {
// スキップ または 競合
if (program.isSkip || program.isConflict) { return undefined; }
// 予約時間超過
if (clock > program.end) {
next = 0;
return;
}
// 予約準備時間内
if (program.start - clock <= prepTime) {
if (isRecording(program) === false && isRecorded(program) === false) {
prepRecord(program);
}
} else if (scheduler === null && program.start - clock < prepTime + ((schedulerEpgRecordTime + 30) * 1000)) {
if (program.start - clock > prepTime + (schedulerEpgRecordTime * 1000)) {
// 録画前EPG確認
util.log('CHECK: ' + dateFormat(new Date(program.start), 'isoDateTime') + ' [' + program.channel.name + '] ' + program.title);
startScheduler(program.channel.channel);
}
}
// 次の開始時間
if (next === 0) {
next = program.start;
}
}
// 録画中チェック
function recordingChecker(program, i) {
var timeout = program.end - clock + offsetEnd;
// 録画時間内はreturn
if (timeout >= 0) { return; }
// 録画開始していない時はreturn
if (!program.pid) { return; }
execRecCmd(function () {
if (program.isSigTerm || ((typeof program.pid) === 'undefined')) { // WAITが入った際に多重にSIGTERM発行されないようにする
return;
}
program.isSigTerm = true;
util.log('FINISH: ' + dateFormat(new Date(program.start), 'isoDateTime') + ' [' + program.channel.name + '] ' + program.title);
process.kill(program.pid, 'SIGTERM');
}, 0, 'KILLWAIT: ' + program.pid);
}
// ファイル更新監視: ./data/reserves.json
chinachu.jsonWatcher(
RESERVES_DATA_FILE,
function _onUpdated(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,
function _onUpdated(err, data, mes) {
if (err) {
console.error(err);
return;
}
recorded = data;
util.log(mes);
},
{ create: [], now: true }
);
// main
function main() {
try {
clock = Date.now();
if (reserves.length !== 0) {
reserves.forEach(reservesChecker);
} else {
next = 0;
}
recording.forEach(recordingChecker);
if ((scheduler === null) && (clock - scheduled > schedulerIntervalTime) && ((next === 0) || (next - clock > schedulerProcessTime))) {
if (
(
schedulerSleepStartHour < schedulerSleepEndHour && (
schedulerSleepStartHour > new Date().getHours() ||
schedulerSleepEndHour <= new Date().getHours()
)
) || (
schedulerSleepStartHour > schedulerSleepEndHour && (
schedulerSleepStartHour > new Date().getHours() &&
schedulerSleepEndHour <= new Date().getHours()
)
)
) {
startScheduler();
scheduled = clock;
}
}
} catch (e) {
console.error('ERROR: ' + e.stack);
}
}
setInterval(main, 1000);