2019-03-03 03:18:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2021-12-27 21:36:01 +00:00
|
|
|
const config = require('config');
|
2020-05-20 00:23:45 +00:00
|
|
|
const util = require('util');
|
2022-11-27 03:22:58 +00:00
|
|
|
const unprint = require('unprint');
|
2021-10-25 00:06:24 +00:00
|
|
|
// const log = require('why-is-node-running');
|
2021-10-17 21:56:49 +00:00
|
|
|
const Inspector = require('inspector-api');
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
const dayjs = require('dayjs');
|
2020-05-20 00:23:45 +00:00
|
|
|
|
2019-03-18 03:46:53 +00:00
|
|
|
const argv = require('./argv');
|
2019-05-06 00:01:57 +00:00
|
|
|
const initServer = require('./web/server');
|
2021-03-07 15:54:20 +00:00
|
|
|
const http = require('./utils/http');
|
2019-03-23 21:48:39 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const logger = require('./logger')(__filename);
|
2020-03-14 01:56:28 +00:00
|
|
|
const knex = require('./knex');
|
2020-03-16 03:10:52 +00:00
|
|
|
const fetchUpdates = require('./updates');
|
2020-03-21 01:48:24 +00:00
|
|
|
const { fetchScenes, fetchMovies } = require('./deep');
|
2023-07-25 01:03:41 +00:00
|
|
|
const { storeScenes, storeMovies, associateMovieScenes } = require('./store-releases');
|
|
|
|
const { updateSceneSearch, updateMovieSearch } = require('./update-search');
|
2021-02-05 03:23:13 +00:00
|
|
|
const { scrapeActors, deleteActors, flushActors, flushProfiles, interpolateProfiles } = require('./actors');
|
2020-10-19 00:02:21 +00:00
|
|
|
const { flushEntities } = require('./entities');
|
2021-02-05 03:16:59 +00:00
|
|
|
const { deleteScenes, deleteMovies, flushScenes, flushMovies, flushBatches } = require('./releases');
|
2020-10-24 22:52:40 +00:00
|
|
|
const { flushOrphanedMedia } = require('./media');
|
2020-07-15 01:24:47 +00:00
|
|
|
const getFileEntries = require('./utils/file-entries');
|
2020-03-14 01:56:28 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const inspector = new Inspector();
|
2021-11-20 22:59:15 +00:00
|
|
|
let done = false;
|
2021-10-17 21:56:49 +00:00
|
|
|
|
2022-11-27 03:22:58 +00:00
|
|
|
unprint.options({
|
|
|
|
timeout: 5000,
|
|
|
|
headers: {
|
|
|
|
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-12-31 00:45:03 +00:00
|
|
|
/*
|
2021-03-07 01:09:37 +00:00
|
|
|
function logActive() {
|
|
|
|
setTimeout(() => {
|
2021-12-18 22:44:51 +00:00
|
|
|
log();
|
|
|
|
|
|
|
|
if (!done) {
|
|
|
|
logActive();
|
|
|
|
}
|
2021-08-09 08:31:12 +00:00
|
|
|
}, typeof argv.logActive === 'number' ? argv.logActive : 60000);
|
2021-03-07 01:09:37 +00:00
|
|
|
}
|
2021-12-31 00:45:03 +00:00
|
|
|
*/
|
2021-03-07 01:09:37 +00:00
|
|
|
|
2021-12-31 00:45:03 +00:00
|
|
|
async function snapshotMemory(trigger) {
|
2021-12-20 01:22:10 +00:00
|
|
|
const profile = await inspector.heap.takeSnapshot();
|
2021-12-31 00:45:03 +00:00
|
|
|
const filepath = `traxxx_snapshot_${trigger}M_${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.heapsnapshot`;
|
2021-11-20 22:59:15 +00:00
|
|
|
|
2021-12-27 16:29:23 +00:00
|
|
|
logger.info(`Starting heap snapshot, memory usage: ${process.memoryUsage.rss() / 1000000} MB`);
|
2021-12-20 01:22:10 +00:00
|
|
|
|
|
|
|
await inspector.heap.disable();
|
|
|
|
await fs.writeFile(filepath, JSON.stringify(profile));
|
|
|
|
|
2021-12-27 16:29:23 +00:00
|
|
|
logger.info(`Saved heap snapshot to ${filepath}`);
|
2021-11-20 22:59:15 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 01:28:59 +00:00
|
|
|
async function stopMemorySample(snapshotTriggers) {
|
2021-12-31 00:45:03 +00:00
|
|
|
const usage = process.memoryUsage.rss() / 1000000;
|
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const profile = await inspector.heap.stopSampling();
|
2021-12-20 02:29:14 +00:00
|
|
|
const filepath = `traxxx_sample_${dayjs().format('YYYY-MM-DD_HH-mm-ss')}.heapprofile`;
|
2021-10-17 21:56:49 +00:00
|
|
|
|
|
|
|
await inspector.heap.disable();
|
2021-11-20 22:59:15 +00:00
|
|
|
await fs.writeFile(filepath, JSON.stringify(profile));
|
2021-10-17 21:56:49 +00:00
|
|
|
|
|
|
|
logger.info(`Saved heap sample to ${filepath}`);
|
2021-12-20 01:22:10 +00:00
|
|
|
|
2021-12-20 01:28:59 +00:00
|
|
|
if (usage > snapshotTriggers[0]) {
|
2021-12-31 00:45:03 +00:00
|
|
|
await snapshotMemory(snapshotTriggers[0]);
|
2021-12-20 01:28:59 +00:00
|
|
|
return snapshotTriggers.slice(1);
|
2021-12-20 01:22:10 +00:00
|
|
|
}
|
2021-12-20 01:28:59 +00:00
|
|
|
|
|
|
|
return snapshotTriggers;
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
2021-12-20 01:28:59 +00:00
|
|
|
async function startMemorySample(snapshotTriggers = []) {
|
2021-11-20 22:59:15 +00:00
|
|
|
await inspector.heap.enable();
|
|
|
|
await inspector.heap.startSampling();
|
|
|
|
|
2021-12-20 01:22:10 +00:00
|
|
|
const usage = process.memoryUsage.rss() / 1000000;
|
2021-11-20 22:59:15 +00:00
|
|
|
|
2021-12-20 01:22:10 +00:00
|
|
|
logger.info(`Start heap sampling, memory usage: ${usage} MB`);
|
2021-11-20 22:59:15 +00:00
|
|
|
|
|
|
|
setTimeout(async () => {
|
2021-12-20 01:28:59 +00:00
|
|
|
const newSnapshotTriggers = await stopMemorySample(snapshotTriggers);
|
2021-11-20 22:59:15 +00:00
|
|
|
|
|
|
|
if (!done) {
|
2021-12-20 01:28:59 +00:00
|
|
|
await startMemorySample(newSnapshotTriggers);
|
2021-11-20 22:59:15 +00:00
|
|
|
}
|
2021-12-31 00:45:03 +00:00
|
|
|
}, config.memorySampling.sampleDuration);
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 01:56:28 +00:00
|
|
|
async function init() {
|
2021-10-17 21:56:49 +00:00
|
|
|
try {
|
2021-12-31 00:45:03 +00:00
|
|
|
if (argv.server) {
|
|
|
|
await initServer();
|
|
|
|
return;
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
2020-03-14 01:56:28 +00:00
|
|
|
|
2021-12-20 01:22:10 +00:00
|
|
|
if (argv.sampleMemory) {
|
2021-12-27 21:36:01 +00:00
|
|
|
await startMemorySample(config.memorySampling.snapshotIntervals);
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
2020-03-28 00:40:02 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.logActive) {
|
2021-12-31 00:45:03 +00:00
|
|
|
// logActive();
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
2020-12-30 01:23:43 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.updateSearch) {
|
|
|
|
await Promise.all([
|
|
|
|
updateSceneSearch(),
|
|
|
|
updateMovieSearch(),
|
|
|
|
]);
|
|
|
|
}
|
2020-12-30 02:19:09 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.interpolateProfiles) {
|
|
|
|
await interpolateProfiles(argv.interpolateProfiles.length > 0 ? argv.interpolateProfiles : null);
|
|
|
|
}
|
2020-12-29 23:16:05 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushActors) {
|
|
|
|
await flushActors(argv.flushActors);
|
|
|
|
}
|
2020-10-19 00:02:21 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushProfiles) {
|
|
|
|
await flushProfiles(argv.flushProfiles.length > 0 ? argv.flushProfiles : null);
|
|
|
|
}
|
2020-10-24 22:52:40 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushNetworks || argv.flushChannels) {
|
|
|
|
await flushEntities(argv.flushNetworks, argv.flushChannels);
|
|
|
|
}
|
2020-12-30 03:17:09 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushBatches) {
|
|
|
|
await flushBatches(argv.flushBatches);
|
|
|
|
}
|
2021-02-05 03:16:59 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushScenes) {
|
|
|
|
await flushScenes();
|
|
|
|
}
|
2021-02-05 03:23:13 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushMovies) {
|
|
|
|
await flushMovies();
|
|
|
|
}
|
2020-10-24 22:52:40 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.deleteActors) {
|
|
|
|
await deleteActors(argv.deleteActors);
|
|
|
|
}
|
2020-10-24 22:52:40 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.deleteScenes) {
|
|
|
|
await deleteScenes(argv.deleteScenes);
|
|
|
|
}
|
2020-10-24 22:52:40 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.deleteMovies) {
|
|
|
|
await deleteMovies(argv.deleteMovies);
|
|
|
|
}
|
2021-03-07 15:54:20 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.flushOrphanedMedia) {
|
|
|
|
await flushOrphanedMedia();
|
|
|
|
}
|
2021-03-07 15:54:20 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.request) {
|
2022-04-10 19:51:22 +00:00
|
|
|
const res = await http[argv.requestMethod](argv.request);
|
2020-07-15 01:24:47 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
console.log(res.status, res.body);
|
|
|
|
}
|
2020-03-19 00:54:25 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const actorsFromFile = argv.actorsFile && await getFileEntries(argv.actorsFile);
|
|
|
|
const actorNames = (argv.actors || []).concat(actorsFromFile || []);
|
2020-03-28 00:40:02 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const actors = (argv.actors || argv.actorsUpdate || argv.actorsFile) && await scrapeActors(actorNames);
|
2021-11-20 22:59:15 +00:00
|
|
|
const actorBaseScenes = argv.actors && argv.actorScenes && actors.map((actor) => actor.scenes).flat().filter(Boolean);
|
2020-07-15 01:24:47 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const updateBaseScenes = (argv.latest || argv.upcoming || argv.channels || argv.networks || argv.movies) && await fetchUpdates();
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const scenesFromFile = argv.scenesFile && await getFileEntries(argv.scenesFile);
|
|
|
|
const sceneUrls = (argv.scene || []).concat(scenesFromFile || []);
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
const deepScenes = argv.deep
|
|
|
|
? await fetchScenes([...(sceneUrls), ...(updateBaseScenes || []), ...(actorBaseScenes || [])])
|
|
|
|
: [...(updateBaseScenes || []), ...(actorBaseScenes || [])];
|
2020-08-08 16:10:59 +00:00
|
|
|
|
2021-12-20 02:29:14 +00:00
|
|
|
const storedScenes = argv.save ? await storeScenes(deepScenes) : [];
|
|
|
|
|
2022-02-02 23:04:48 +00:00
|
|
|
const moviesFromFile = argv.moviesFile && await getFileEntries(argv.moviesFile);
|
|
|
|
const movieUrls = (argv.movie || []).concat(moviesFromFile || []);
|
|
|
|
|
2022-02-05 16:14:34 +00:00
|
|
|
const sceneMovies = deepScenes && argv.sceneMovies ? deepScenes.filter((scene) => scene.movie).map((scene) => ({ ...scene.movie, entity: scene.entity })) : [];
|
2022-02-02 23:04:48 +00:00
|
|
|
const deepMovies = argv.sceneMovies || argv.movie || movieUrls ? await fetchMovies([...movieUrls, ...(sceneMovies || []), ...[]]) : sceneMovies;
|
2020-05-20 00:23:45 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
const movieScenes = argv.movieScenes ? deepMovies.map((movie) => movie.scenes?.map((scene) => ({ ...scene, movie, entity: movie.entity }))).flat().filter(Boolean) : [];
|
2021-10-17 21:56:49 +00:00
|
|
|
const deepMovieScenes = argv.deep ? await fetchScenes(movieScenes) : movieScenes;
|
2021-01-26 23:21:58 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.report) {
|
|
|
|
console.log(util.inspect(deepScenes, { depth: Infinity, colors: true }));
|
|
|
|
console.log(util.inspect(deepMovies, { depth: Infinity, colors: true }));
|
|
|
|
}
|
2020-03-16 03:10:52 +00:00
|
|
|
|
2021-10-17 21:56:49 +00:00
|
|
|
if (argv.save) {
|
2022-01-19 23:54:10 +00:00
|
|
|
const storedMovies = await storeMovies(deepMovies, storedScenes[0]?.batchId);
|
|
|
|
const storedMovieScenes = await storeScenes(deepMovieScenes, storedScenes[0]?.batchId);
|
2021-10-17 21:56:49 +00:00
|
|
|
|
2021-12-20 02:29:14 +00:00
|
|
|
await associateMovieScenes(storedMovies, [...storedScenes, ...storedMovieScenes]);
|
2021-10-17 21:56:49 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-05-15 21:28:56 +00:00
|
|
|
console.trace(error);
|
2021-10-17 21:56:49 +00:00
|
|
|
logger.error(error);
|
|
|
|
}
|
2021-11-20 22:59:15 +00:00
|
|
|
|
2022-09-27 18:09:46 +00:00
|
|
|
await http.destroyBypassSessions();
|
2022-12-28 00:34:12 +00:00
|
|
|
await http.destroyBrowserSessions();
|
2022-09-27 18:09:46 +00:00
|
|
|
|
2021-11-20 22:59:15 +00:00
|
|
|
knex.destroy();
|
|
|
|
done = true;
|
2020-03-14 01:56:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-09 04:00:49 +00:00
|
|
|
module.exports = init;
|