notifications.js 2.5 KB

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