2018-01-20 20:23:27 +01:00
|
|
|
const fs = require('fs');
|
2021-03-19 14:01:45 +01:00
|
|
|
|
|
|
|
|
//if db folder does not exist, ensure to create it before loading anything else
|
|
|
|
|
if (!fs.existsSync('./db')) {
|
|
|
|
|
fs.mkdirSync('./db');
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 20:23:27 +01:00
|
|
|
const path = './lib/provider';
|
2021-01-21 16:09:23 +01:00
|
|
|
const provider = fs.readdirSync(path).filter((file) => file.endsWith('.js'));
|
2018-01-25 16:38:39 +01:00
|
|
|
const config = require('./conf/config.json');
|
2021-01-21 16:09:23 +01:00
|
|
|
|
2021-06-28 08:52:09 +02:00
|
|
|
const similarityCache = require('./lib/services/similarity-check/similarityCache');
|
2021-01-21 16:09:23 +01:00
|
|
|
const { setLastJobExecution } = require('./lib/services/storage/listingsStorage');
|
2021-06-28 08:52:09 +02:00
|
|
|
const jobStorage = require('./lib/services/storage/jobStorage');
|
2020-02-26 09:05:20 +01:00
|
|
|
const FredyRuntime = require('./lib/FredyRuntime');
|
2018-01-20 20:23:27 +01:00
|
|
|
|
2021-05-30 09:37:45 +02:00
|
|
|
const { duringWorkingHoursOrNotSet } = require('./lib/utils');
|
|
|
|
|
|
2020-02-26 09:05:20 +01:00
|
|
|
//starting the api service
|
|
|
|
|
require('./lib/api/api');
|
|
|
|
|
|
2021-01-21 16:09:23 +01:00
|
|
|
//assuming interval is always in minutes
|
|
|
|
|
const INTERVAL = config.interval * 60 * 1000;
|
|
|
|
|
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
console.log(`Started Fredy successfully. Ui can be accessed via http://localhost:${config.port}`);
|
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
|
setInterval(
|
|
|
|
|
(function exec() {
|
2021-05-30 09:37:45 +02:00
|
|
|
const isDuringWorkingHoursOrNotSet = duringWorkingHoursOrNotSet(config, Date.now());
|
|
|
|
|
|
|
|
|
|
if (isDuringWorkingHoursOrNotSet) {
|
|
|
|
|
config.lastRun = Date.now();
|
2022-12-11 20:07:18 +01:00
|
|
|
const fetchedProvider = provider
|
|
|
|
|
.filter((provider) => provider.endsWith('.js'))
|
|
|
|
|
.map((pro) => require(`${path}/${pro}`));
|
|
|
|
|
|
2021-05-30 09:37:45 +02:00
|
|
|
jobStorage
|
|
|
|
|
.getJobs()
|
|
|
|
|
.filter((job) => job.enabled)
|
|
|
|
|
.forEach((job) => {
|
2022-12-11 20:07:18 +01:00
|
|
|
job.provider
|
|
|
|
|
.filter((p) => fetchedProvider.find((fp) => fp.metaInformation.id === p.id) != null)
|
|
|
|
|
.forEach(async (prov) => {
|
|
|
|
|
const pro = fetchedProvider.find((fp) => fp.metaInformation.id === prov.id);
|
|
|
|
|
pro.init(prov, job.blacklist);
|
|
|
|
|
await new FredyRuntime(pro.config, job.notificationAdapter, prov.id, job.id, similarityCache).execute();
|
2021-05-30 09:37:45 +02:00
|
|
|
setLastJobExecution(job.id);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
console.debug('Working hours set. Skipping as outside of working hours.');
|
|
|
|
|
/* eslint-enable no-console */
|
|
|
|
|
}
|
2021-01-21 16:09:23 +01:00
|
|
|
return exec;
|
|
|
|
|
})(),
|
|
|
|
|
INTERVAL
|
|
|
|
|
);
|