mirror of
https://github.com/Tzahi12345/YoutubeDL-Material.git
synced 2026-04-11 01:31:29 +03:00
Added scheduler for tasks
This commit is contained in:
@@ -3,34 +3,53 @@ const db_api = require('./db');
|
||||
|
||||
const fs = require('fs-extra');
|
||||
const logger = require('./logger');
|
||||
const scheduler = require('node-schedule');
|
||||
|
||||
const TASKS = {
|
||||
backup_local_db: {
|
||||
run: utils.backupLocalDB,
|
||||
title: 'Backup Local DB',
|
||||
job: null
|
||||
},
|
||||
missing_files_check: {
|
||||
run: checkForMissingFiles,
|
||||
confirm: deleteMissingFiles,
|
||||
title: 'Missing files check'
|
||||
title: 'Missing files check',
|
||||
job: null
|
||||
},
|
||||
missing_db_records: {
|
||||
run: db_api.importUnregisteredFiles,
|
||||
title: 'Import missing DB records'
|
||||
title: 'Import missing DB records',
|
||||
job: null
|
||||
},
|
||||
duplicate_files_check: {
|
||||
run: checkForDuplicateFiles,
|
||||
confirm: removeDuplicates,
|
||||
title: 'Find duplicate files in DB'
|
||||
title: 'Find duplicate files in DB',
|
||||
job: null
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleJob(task_key, schedule) {
|
||||
return scheduler.scheduleJob(schedule, async () => {
|
||||
const task_state = await db_api.getRecord('tasks', {key: task_key});
|
||||
if (task_state['running'] || task_state['confirming']) {
|
||||
logger.verbose(`Skipping running task ${task_state['key']} as it is already in progress.`);
|
||||
return;
|
||||
}
|
||||
|
||||
// we're just "running" the task, any confirmation should be user-initiated
|
||||
exports.executeRun(task_key);
|
||||
});
|
||||
}
|
||||
|
||||
exports.initialize = async () => {
|
||||
const tasks_keys = Object.keys(TASKS);
|
||||
for (let i = 0; i < tasks_keys.length; i++) {
|
||||
const task_key = tasks_keys[i];
|
||||
const task_in_db = await db_api.getRecord('tasks', {key: task_key});
|
||||
if (!task_in_db) {
|
||||
// insert task into table if missing
|
||||
await db_api.insertRecordIntoTable('tasks', {
|
||||
key: task_key,
|
||||
last_ran: null,
|
||||
@@ -38,8 +57,17 @@ exports.initialize = async () => {
|
||||
running: false,
|
||||
confirming: false,
|
||||
data: null,
|
||||
error: null
|
||||
error: null,
|
||||
schedule: null
|
||||
});
|
||||
} else {
|
||||
// reset task if necessary
|
||||
await db_api.updateRecord('tasks', {key: task_key}, {running: false, confirming: false});
|
||||
|
||||
// schedule task and save job
|
||||
if (task_in_db['schedule']) {
|
||||
TASKS[task_key]['job'] = scheduleJob(task_key, task_in_db['schedule']);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,6 +101,16 @@ exports.executeConfirm = async (task_key) => {
|
||||
await db_api.updateRecord('tasks', {key: task_key}, {confirming: false, last_confirmed: Date.now()/1000});
|
||||
}
|
||||
|
||||
exports.updateTaskSchedule = async (task_key, schedule) => {
|
||||
await db_api.updateRecord('tasks', {key: task_key}, {schedule: schedule});
|
||||
if (TASKS[task_key]['job']) {
|
||||
TASKS[task_key]['job'].cancel();
|
||||
}
|
||||
if (schedule) {
|
||||
TASKS[task_key]['job'] = scheduleJob(task_key, schedule);
|
||||
}
|
||||
}
|
||||
|
||||
// missing files check
|
||||
|
||||
async function checkForMissingFiles() {
|
||||
@@ -109,4 +147,6 @@ async function removeDuplicates(data) {
|
||||
for (let i = 0; i < data['uids'].length; i++) {
|
||||
await db_api.removeRecord('files', {uid: data['uids'][i]});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.TASKS = TASKS;
|
||||
Reference in New Issue
Block a user