iptv/scripts/commands/cluster/load.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

2022-02-11 19:07:16 +01:00
const { db, logger, timer, checker, store, file, parser } = require('../../core')
2021-12-12 05:10:03 +01:00
const { program } = require('commander')
const options = program
.requiredOption('-c, --cluster-id <cluster-id>', 'The ID of cluster to load', parser.parseNumber)
.option('-t, --timeout <timeout>', 'Set timeout for each request', parser.parseNumber, 60000)
.option('-d, --delay <delay>', 'Set delay for each request', parser.parseNumber, 0)
2022-06-08 19:45:03 +02:00
.option('--debug', 'Enable debug mode', false)
2021-12-12 05:10:03 +01:00
.parse(process.argv)
.opts()
const config = {
timeout: options.timeout,
delay: options.delay,
debug: options.debug
}
2022-02-11 23:58:11 +01:00
const LOGS_DIR = process.env.LOGS_DIR || 'scripts/logs/cluster/load'
2021-12-12 05:10:03 +01:00
async function main() {
2022-02-05 01:19:31 +01:00
logger.info('starting...')
logger.info(`timeout: ${options.timeout}ms`)
logger.info(`delay: ${options.delay}ms`)
2021-12-12 05:10:03 +01:00
timer.start()
2022-02-07 04:18:31 +01:00
const clusterLog = `${LOGS_DIR}/cluster_${options.clusterId}.log`
2022-02-05 01:19:31 +01:00
logger.info(`loading cluster: ${options.clusterId}`)
logger.info(`creating '${clusterLog}'...`)
2021-12-12 05:10:03 +01:00
await file.create(clusterLog)
2022-02-07 04:18:31 +01:00
await db.streams.load()
const items = await db.streams.find({ cluster_id: options.clusterId })
2021-12-12 05:10:03 +01:00
const total = items.length
2022-02-05 01:19:31 +01:00
logger.info(`found ${total} links`)
2021-12-12 05:10:03 +01:00
2022-02-05 01:19:31 +01:00
logger.info('checking...')
2021-12-12 05:10:03 +01:00
const results = {}
for (const [i, item] of items.entries()) {
const message = `[${i + 1}/${total}] ${item.filepath}: ${item.url}`
2022-02-13 02:44:52 +01:00
const request = {
_id: item._id,
url: item.url,
http: {
referrer: item.http_referrer,
'user-agent': item.user_agent
}
}
const result = await checker.check(request, config)
2021-12-12 05:10:03 +01:00
if (!result.error) {
logger.info(message)
} else {
2022-06-08 19:45:03 +02:00
logger.info(`${message} (${result.error.message})`)
2021-12-12 05:10:03 +01:00
}
2022-02-13 02:15:37 +01:00
const output = {
_id: result._id,
error: result.error,
streams: result.streams,
requests: result.requests
}
await file.append(clusterLog, JSON.stringify(output) + '\n')
2021-12-12 05:10:03 +01:00
}
2022-02-05 01:19:31 +01:00
logger.info(`done in ${timer.format('HH[h] mm[m] ss[s]')}`)
2021-12-12 05:10:03 +01:00
}
main()