notifications.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. pub.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', time, 'NX', cb);
  41. },
  42. /**
  43. * Subscribes a callback function to be called when a notification gets called
  44. *
  45. * @param {String} name - the name of the notification we want to subscribe to
  46. * @param {Function} cb - gets called when the subscribed notification gets called
  47. * @param {Boolean} unique - only subscribe if another subscription with the same name doesn't already exist
  48. * @return {Object} - the subscription object
  49. */
  50. subscribe: (name, cb, unique = false) => {
  51. if (unique && subscriptions.find((subscription) => subscription.originalName == name)) return;
  52. let subscription = { originalName: name, name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
  53. subscriptions.push(subscription);
  54. return subscription;
  55. },
  56. /**
  57. * Remove a notification subscription
  58. *
  59. * @param {Object} subscription - the subscription object returned by {@link subscribe}
  60. */
  61. remove: (subscription) => {
  62. let index = subscriptions.indexOf(subscription);
  63. if (index) subscriptions.splice(index, 1);
  64. },
  65. unschedule: (name) => {
  66. pub.del(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'));
  67. },
  68. };
  69. module.exports = lib;