You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

  1. var nThen = require("nthen");
  2. var Store = require("../lib/storage/file");
  3. var config = require("../lib/load-config");
  4. var store;
  5. var Log;
  6. nThen(function (w) {
  7. // load the store which will be used for iterating over channels
  8. // and performing operations like archival and deletion
  9. Store.create(config, w(function (err, _) {
  10. if (err) {
  11. w.abort();
  12. throw err;
  13. }
  14. store = _;
  15. }));
  16. // load the logging module so that you have a record of which
  17. // files were archived or deleted at what time
  18. var Logger = require("../lib/log");
  19. Logger.create(config, w(function (_) {
  20. Log = _;
  21. }));
  22. }).nThen(function (w) {
  23. // count the number of files which have been restored in this run
  24. var conflicts = 0;
  25. var handler = function (err, item, cb) {
  26. if (err) {
  27. Log.error('DIAGNOSE_ARCHIVE_CONFLICTS_ITERATION', err);
  28. return void cb();
  29. }
  30. // check if such a file exists on the server
  31. store.isChannelAvailable(item.channel, function (err, available) {
  32. // weird edge case?
  33. if (err) { return void cb(); }
  34. // the channel doesn't exist in the database
  35. if (!available) { return void cb(); }
  36. // the channel is available
  37. // that means it's a duplicate of something in the archive
  38. conflicts++;
  39. Log.info('DIAGNOSE_ARCHIVE_CONFLICT_DETECTED', item.channel);
  40. cb();
  41. });
  42. };
  43. // if you hit an error, log it
  44. // otherwise, when there are no more channels to process
  45. // log some stats about how many were removed
  46. var done = function (err) {
  47. if (err) {
  48. return Log.error('DIAGNOSE_ARCHIVE_CONFLICTS_FINAL_ERROR', err);
  49. }
  50. Log.info('DIAGNOSE_ARCHIVE_CONFLICTS_COUNT', conflicts);
  51. };
  52. store.listArchivedChannels(handler, w(done));
  53. }).nThen(function () {
  54. // the store will keep this script running if you don't shut it down
  55. store.shutdown();
  56. Log.shutdown();
  57. });