2025-12-11 10:40:55 +01:00
|
|
|
/*
|
2026-01-12 15:00:36 +01:00
|
|
|
* Copyright (c) 2026 by Christian Kellner.
|
2025-12-11 10:40:55 +01:00
|
|
|
* 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';
|
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 { 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-22 09:57:50 +02:00
|
|
|
import { initActiveCheckerCron } from './lib/services/crons/listing-alive-cron.js';
|
2026-01-12 15:00:36 +01:00
|
|
|
import { initGeocodingCron } from './lib/services/crons/geocoding-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-18 19:16:28 +01:00
|
|
|
import { initJobExecutionService } from './lib/services/jobs/jobExecutionService.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-12-18 19:16:28 +01:00
|
|
|
// Ensure the 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');
|
2024-11-22 09:11:10 +01:00
|
|
|
}
|
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();
|
2026-01-12 15:00:36 +01:00
|
|
|
initGeocodingCron();
|
2024-11-22 09:11:10 +01:00
|
|
|
|
2025-12-18 19:16:28 +01:00
|
|
|
logger.info(`Started Fredy successfully. Ui can be accessed via http://localhost:${settings.port}`);
|
2025-09-18 20:48:25 +02:00
|
|
|
|
2025-12-18 19:16:28 +01:00
|
|
|
// Initialize the lean Job Execution Service (schedules and bus listeners)
|
|
|
|
|
initJobExecutionService({ providers, settings, intervalMs: INTERVAL });
|