2025-12-11 10:40:55 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2025 by Christian Kellner.
|
|
|
|
|
* Licensed under Apache-2.0 with Commons Clause and Attribution/Naming Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-03-13 13:42:43 +01:00
|
|
|
import fs from 'fs';
|
2025-12-09 13:56:46 +01:00
|
|
|
import { checkIfConfigIsAccessible, getProviders, refreshConfig } from './lib/utils.js';
|
2023-03-13 13:42:43 +01:00
|
|
|
import * as similarityCache from './lib/services/similarity-check/similarityCache.js';
|
|
|
|
|
import * as jobStorage from './lib/services/storage/jobStorage.js';
|
2025-10-12 16:43:56 +02:00
|
|
|
import FredyPipeline from './lib/FredyPipeline.js';
|
2023-03-13 13:42:43 +01:00
|
|
|
import { duringWorkingHoursOrNotSet } from './lib/utils.js';
|
2025-09-18 17:28:30 +02:00
|
|
|
import { runMigrations } from './lib/services/storage/migrations/migrate.js';
|
2025-09-18 15:38:23 +02:00
|
|
|
import { ensureDemoUserExists, ensureAdminUserExists } from './lib/services/storage/userStorage.js';
|
2025-09-22 09:57:50 +02:00
|
|
|
import { cleanupDemoAtMidnight } from './lib/services/crons/demoCleanup-cron.js';
|
|
|
|
|
import { initTrackerCron } from './lib/services/crons/tracker-cron.js';
|
2025-09-13 18:57:56 +02:00
|
|
|
import logger from './lib/services/logger.js';
|
2025-09-18 20:48:25 +02:00
|
|
|
import { bus } from './lib/services/events/event-bus.js';
|
2025-09-22 09:57:50 +02:00
|
|
|
import { initActiveCheckerCron } from './lib/services/crons/listing-alive-cron.js';
|
2025-12-09 13:56:46 +01:00
|
|
|
import { getSettings } from './lib/services/storage/settingsStorage.js';
|
2025-12-17 15:48:56 +01:00
|
|
|
import SqliteConnection, { computeDbPath } from './lib/services/storage/SqliteConnection.js';
|
2025-12-09 13:56:46 +01:00
|
|
|
|
|
|
|
|
//in the config, we store the path of the sqlite file, thus we must check if it is available
|
|
|
|
|
const isConfigAccessible = await checkIfConfigIsAccessible();
|
|
|
|
|
await SqliteConnection.init();
|
2025-09-22 09:57:50 +02:00
|
|
|
|
|
|
|
|
// Load configuration before any other startup steps
|
|
|
|
|
await refreshConfig();
|
2025-09-18 15:38:23 +02:00
|
|
|
|
2025-10-03 17:23:46 +02:00
|
|
|
if (!isConfigAccessible) {
|
|
|
|
|
logger.error('Configuration exists, but is not accessible. Please check the file permission');
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-09 13:56:46 +01:00
|
|
|
// Run DB migrations once at startup and block until finished
|
|
|
|
|
await runMigrations();
|
|
|
|
|
|
|
|
|
|
const settings = await getSettings();
|
|
|
|
|
|
2025-09-18 15:38:23 +02:00
|
|
|
// Ensure sqlite directory exists before loading anything else (based on config.sqlitepath)
|
2025-12-17 15:48:56 +01:00
|
|
|
const { dir: sqliteDir } = await computeDbPath();
|
|
|
|
|
if (!fs.existsSync(sqliteDir)) {
|
|
|
|
|
fs.mkdirSync(sqliteDir, { recursive: true });
|
2021-03-19 14:01:45 +01:00
|
|
|
}
|
2025-09-18 15:38:23 +02:00
|
|
|
|
2025-09-22 09:57:50 +02:00
|
|
|
// Load provider modules once at startup
|
|
|
|
|
const providers = await getProviders();
|
|
|
|
|
|
2025-10-29 09:36:05 +01:00
|
|
|
similarityCache.initSimilarityCache();
|
2025-10-29 10:35:07 +01:00
|
|
|
similarityCache.startSimilarityCacheReloader();
|
2025-10-29 09:36:05 +01:00
|
|
|
|
2021-01-21 16:09:23 +01:00
|
|
|
//assuming interval is always in minutes
|
2025-12-09 13:56:46 +01:00
|
|
|
const INTERVAL = settings.interval * 60 * 1000;
|
2025-09-18 15:38:23 +02:00
|
|
|
|
|
|
|
|
// Initialize API only after migrations completed
|
|
|
|
|
await import('./lib/api/api.js');
|
|
|
|
|
|
2025-12-09 13:56:46 +01:00
|
|
|
if (settings.demoMode) {
|
2025-09-13 18:57:56 +02:00
|
|
|
logger.info('Running in demo mode');
|
2025-07-23 08:47:26 +02:00
|
|
|
cleanupDemoAtMidnight();
|
2024-11-22 09:11:10 +01:00
|
|
|
}
|
2025-09-18 15:38:23 +02:00
|
|
|
|
2025-12-09 13:56:46 +01:00
|
|
|
logger.info(`Started Fredy successfully. Ui can be accessed via http://localhost:${settings.port}`);
|
2025-09-18 15:38:23 +02:00
|
|
|
|
|
|
|
|
ensureAdminUserExists();
|
|
|
|
|
ensureDemoUserExists();
|
2025-09-13 17:06:18 +02:00
|
|
|
await initTrackerCron();
|
2025-09-22 09:57:50 +02:00
|
|
|
//do not wait for this to finish, let it run in the background
|
|
|
|
|
initActiveCheckerCron();
|
2024-11-22 09:11:10 +01:00
|
|
|
|
2025-09-18 20:48:25 +02:00
|
|
|
bus.on('jobs:runAll', () => {
|
|
|
|
|
logger.debug('Running Fredy Job manually');
|
|
|
|
|
execute();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const execute = () => {
|
2025-12-09 13:56:46 +01:00
|
|
|
const isDuringWorkingHoursOrNotSet = duringWorkingHoursOrNotSet(settings, Date.now());
|
|
|
|
|
if (!settings.demoMode) {
|
2025-09-18 20:48:25 +02:00
|
|
|
if (isDuringWorkingHoursOrNotSet) {
|
2025-12-09 13:56:46 +01:00
|
|
|
settings.lastRun = Date.now();
|
2025-09-18 20:48:25 +02:00
|
|
|
jobStorage
|
|
|
|
|
.getJobs()
|
|
|
|
|
.filter((job) => job.enabled)
|
|
|
|
|
.forEach((job) => {
|
|
|
|
|
job.provider
|
2025-09-22 09:57:50 +02:00
|
|
|
.filter((p) => providers.find((loaded) => loaded.metaInformation.id === p.id) != null)
|
2025-09-18 20:48:25 +02:00
|
|
|
.forEach(async (prov) => {
|
2025-12-12 22:21:49 +01:00
|
|
|
try {
|
|
|
|
|
const matchedProvider = providers.find((loaded) => loaded.metaInformation.id === prov.id);
|
|
|
|
|
matchedProvider.init(prov, job.blacklist);
|
|
|
|
|
await new FredyPipeline(
|
|
|
|
|
matchedProvider.config,
|
|
|
|
|
job.notificationAdapter,
|
|
|
|
|
prov.id,
|
|
|
|
|
job.id,
|
|
|
|
|
similarityCache,
|
|
|
|
|
).execute();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(error);
|
|
|
|
|
}
|
2025-09-18 20:48:25 +02:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
logger.debug('Working hours set. Skipping as outside of working hours.');
|
2025-07-23 08:47:26 +02:00
|
|
|
}
|
2025-09-18 20:48:25 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
setInterval(execute, INTERVAL);
|
|
|
|
|
//start once at startup
|
|
|
|
|
execute();
|