monitor.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const fs = require('fs');
  2. const path = require('path');
  3. const Queue = require('better-queue');
  4. const chokidar = require('chokidar');
  5. const paths = require('../data/paths.json'); // get our paths
  6. const settings = require('../data/settings.json'); // get the settings
  7. const { log } = require('./utils');
  8. const file = require('./file');
  9. const db = require('./db');
  10. const queue = new Queue(async (input, cb) => {
  11. cb = cb || function(){};
  12. let result = true; //await file.process(input); // process the queue
  13. cb(null, result);
  14. }, settings.queue);
  15. queue.on('task_started', (taskId, result, stats) => {
  16. console.log('task_started',taskId);
  17. });
  18. queue.on('task_progress', (taskId, completed, total) => {
  19. console.log('task_progress',taskId);
  20. });
  21. queue.on('task_finish', (taskId, result, stats) => {
  22. console.log('task_finish',taskId);
  23. // log the activity
  24. // taskId = 1, result: 3, stats = { elapsed: <time taken> }
  25. });
  26. queue.on('task_failed', (taskId, err) => {
  27. console.log('task_failed',taskId);
  28. // get the file from the task
  29. //if (settings.queue.deleteOnFail) file.remove();
  30. });
  31. const cancel = async (taskId) => {
  32. queue.cancel(taskId);
  33. };
  34. const pause = async () => {
  35. queue.pause();
  36. };
  37. const resume = async () => {
  38. queue.resume();
  39. };
  40. const stats = async () => {
  41. return queue.getStats();
  42. };
  43. // initialize watches and db then start the watcher
  44. const watch = () => {
  45. const dirs = Object.keys(paths); // get a list of the paths from the keys
  46. const defaults = settings.defaults; // load tje defaults
  47. const opts = settings.watcher; // setup watcher options
  48. let watches = []; // array of things to watch
  49. opts.ignored = new RegExp(opts.ignored); // convert the ignored into a reqex
  50. // loop the dirs
  51. for (let d in dirs) {
  52. let dir = dirs[d]; // pointer
  53. let options = Object.assign({}, defaults, paths[dir]); // baseline the options
  54. let database = db.connect(dir); // connect to the database
  55. database.defaults({ files: [] }).write(); // init the database
  56. // loop the exts to watch
  57. for (let e in options.exts) {
  58. let ext = options.exts[e]; // alias the ext
  59. watches.push(`${dir}/**/*.${ext}`); // push the watch
  60. }
  61. }
  62. // init our watcher
  63. const watcher = chokidar.watch(watches, opts);
  64. log(`Watching:\n${JSON.stringify(watches,null,2)}\n`,false);
  65. watcher
  66. .on('add', async f => {
  67. queue.push(f); // when a file is added ... push the file onto the queue to be processed
  68. })
  69. .on('change', f => {
  70. log(` -> "${f}" [changed] (${new Date()})\n`,false); // when a file changes ...
  71. })
  72. .on('unlink', async f => {
  73. log(` -> "${f}" [removed] (${new Date()})\n`,false); // when a file is removed ...
  74. //file.cleanup(dirs, f);
  75. })
  76. .on('error', error => {
  77. log(` -> Error ${new Date()}: ${error.message || error}\n`, false);
  78. });
  79. };
  80. module.exports = {
  81. cancel,
  82. pause,
  83. queue,
  84. resume,
  85. stats,
  86. watch
  87. };