Processing (Manticore) sync queue.
This commit is contained in:
35
src/sync.js
35
src/sync.js
@@ -1,4 +1,6 @@
|
||||
import config from 'config';
|
||||
import { format } from 'date-fns';
|
||||
import { CronJob } from 'cron';
|
||||
|
||||
import initLogger from './logger.js';
|
||||
import { knexOwner as knex } from './knex.js';
|
||||
@@ -438,3 +440,36 @@ export async function syncActors(actorIds) {
|
||||
|
||||
await syncManticoreActors(actorIds);
|
||||
}
|
||||
|
||||
function getQueueItemIds(tasks, domain) {
|
||||
return Array.from(new Set(tasks.filter((task) => task.domain === domain).flatMap((task) => task.item_ids)));
|
||||
}
|
||||
|
||||
export async function syncQueue() {
|
||||
const tasks = await knex('sync');
|
||||
|
||||
const sceneIds = getQueueItemIds(tasks, 'scene');
|
||||
const movieIds = getQueueItemIds(tasks, 'movie');
|
||||
const actorIds = getQueueItemIds(tasks, 'actor');
|
||||
|
||||
await Promise.all([
|
||||
syncScenes(sceneIds),
|
||||
syncMovies(movieIds),
|
||||
syncActors(actorIds),
|
||||
]);
|
||||
|
||||
await knex('sync')
|
||||
.whereIn('id', tasks.map((task) => task.id))
|
||||
.delete();
|
||||
|
||||
logger[process.tasks > 0 ? 'info' : 'verbose'](`Processed ${tasks.length} sync items`);
|
||||
}
|
||||
|
||||
CronJob.from({
|
||||
cronTime: config.sync.crontab,
|
||||
async onTick() {
|
||||
syncQueue();
|
||||
},
|
||||
start: config.sync.enabled,
|
||||
runOnInit: true,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user