| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- const fs = require('fs');
- const path = require('path');
- const Queue = require('better-queue');
- const chokidar = require('chokidar');
- const paths = require('../data/paths.json'); // get our paths
- const settings = require('../data/settings.json'); // get the settings
- const { log } = require('./utils');
- const file = require('./file');
- const db = require('./db');
- const queue = new Queue(async (input, cb) => {
- cb = cb || function(){};
- let result = true; //await file.process(input); // process the queue
- cb(null, result);
- }, settings.queue);
- queue.on('task_started', (taskId, result, stats) => {
- console.log('task_started',taskId);
- });
- queue.on('task_progress', (taskId, completed, total) => {
- console.log('task_progress',taskId);
- });
- queue.on('task_finish', (taskId, result, stats) => {
- console.log('task_finish',taskId);
- // log the activity
- // taskId = 1, result: 3, stats = { elapsed: <time taken> }
- });
- queue.on('task_failed', (taskId, err) => {
- console.log('task_failed',taskId);
- // get the file from the task
- //if (settings.queue.deleteOnFail) file.remove();
- });
- const cancel = async (taskId) => {
- queue.cancel(taskId);
- };
- const pause = async () => {
- queue.pause();
- };
- const resume = async () => {
- queue.resume();
- };
- const stats = async () => {
- return queue.getStats();
- };
- // initialize watches and db then start the watcher
- const watch = () => {
- const dirs = Object.keys(paths); // get a list of the paths from the keys
- const defaults = settings.defaults; // load tje defaults
- const opts = settings.watcher; // setup watcher options
- let watches = []; // array of things to watch
- opts.ignored = new RegExp(opts.ignored); // convert the ignored into a reqex
- // loop the dirs
- for (let d in dirs) {
- let dir = dirs[d]; // pointer
- let options = Object.assign({}, defaults, paths[dir]); // baseline the options
- let database = db.connect(dir); // connect to the database
- database.defaults({ files: [] }).write(); // init the database
- // loop the exts to watch
- for (let e in options.exts) {
- let ext = options.exts[e]; // alias the ext
- watches.push(`${dir}/**/*.${ext}`); // push the watch
- }
- }
- // init our watcher
- const watcher = chokidar.watch(watches, opts);
- log(`Watching:\n${JSON.stringify(watches,null,2)}\n`,false);
- watcher
- .on('add', async f => {
- queue.push(f); // when a file is added ... push the file onto the queue to be processed
- })
- .on('change', f => {
- log(` -> "${f}" [changed] (${new Date()})\n`,false); // when a file changes ...
- })
- .on('unlink', async f => {
- log(` -> "${f}" [removed] (${new Date()})\n`,false); // when a file is removed ...
- //file.cleanup(dirs, f);
- })
- .on('error', error => {
- log(` -> Error ${new Date()}: ${error.message || error}\n`, false);
- });
- };
- module.exports = {
- cancel,
- pause,
- queue,
- resume,
- stats,
- watch
- };
|