notifications.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const crypto = require('crypto');
  3. const redis = require('redis');
  4. const logger = require('./logger');
  5. let pub = null;
  6. let sub = null;
  7. const subscriptions = [];
  8. const lib = {
  9. /**
  10. * Initializes the notifications module
  11. *
  12. * @param {String} url - the url of the redis server
  13. * @param {String} password - the password of the redis server
  14. * @param {Function} cb - gets called once we're done initializing
  15. */
  16. init: (url, password, cb) => {
  17. pub = redis.createClient({ url, password });
  18. sub = redis.createClient({ url, password });
  19. sub.on('error', (err) => {
  20. console.error(err);
  21. process.exit();
  22. });
  23. sub.on('pmessage', (pattern, channel, expiredKey) => {
  24. logger.stationIssue(`PMESSAGE - Pattern: ${pattern}; Channel: ${channel}; ExpiredKey: ${expiredKey}`);
  25. subscriptions.forEach((sub) => {
  26. if (sub.name !== expiredKey) return;
  27. sub.cb();
  28. });
  29. });
  30. sub.psubscribe('__keyevent@0__:expired');
  31. cb();
  32. },
  33. /**
  34. * Schedules a notification to be dispatched in a specific amount of milliseconds,
  35. * notifications are unique by name, and the first one is always kept, as in
  36. * attempting to schedule a notification that already exists won't do anything
  37. *
  38. * @param {String} name - the name of the notification we want to schedule
  39. * @param {Integer} time - how long in milliseconds until the notification should be fired
  40. * @param {Function} cb - gets called when the notification has been scheduled
  41. */
  42. schedule: (name, time, cb, station) => {
  43. if (!cb) cb = ()=>{};
  44. time = Math.round(time);
  45. logger.stationIssue(`SCHEDULE - Time: ${time}; Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}; StationId: ${station._id}; StationName: ${station.name}`);
  46. pub.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', time, 'NX', cb);
  47. },
  48. /**
  49. * Subscribes a callback function to be called when a notification gets called
  50. *
  51. * @param {String} name - the name of the notification we want to subscribe to
  52. * @param {Function} cb - gets called when the subscribed notification gets called
  53. * @param {Boolean} unique - only subscribe if another subscription with the same name doesn't already exist
  54. * @return {Object} - the subscription object
  55. */
  56. subscribe: (name, cb, unique = false, station) => {
  57. if (unique && subscriptions.find((subscription) => subscription.originalName == name)) return;
  58. logger.stationIssue(`SUBSCRIBE - Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}, StationId: ${station._id}; StationName: ${station.name}`);
  59. let subscription = { originalName: name, name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
  60. subscriptions.push(subscription);
  61. return subscription;
  62. },
  63. /**
  64. * Remove a notification subscription
  65. *
  66. * @param {Object} subscription - the subscription object returned by {@link subscribe}
  67. */
  68. remove: (subscription) => {
  69. let index = subscriptions.indexOf(subscription);
  70. if (index) subscriptions.splice(index, 1);
  71. },
  72. unschedule: (name) => {
  73. logger.stationIssue(`UNSCHEDULE - Name: ${name}; Key: ${crypto.createHash('md5').update(`_notification:${name}_`).digest('hex')}`);
  74. pub.del(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'));
  75. },
  76. };
  77. module.exports = lib;