1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 'use strict';
- const crypto = require('crypto');
- const redis = require('redis');
- let pub = null;
- let sub = null;
- const subscriptions = [];
- const lib = {
-
- init: (url, cb) => {
- pub = redis.createClient({ url: url });
- sub = redis.createClient({ url: url });
- sub.on('error', (err) => console.error);
- sub.on('pmessage', (pattern, channel, expiredKey) => {
- subscriptions.forEach((sub) => {
- if (sub.name !== expiredKey) return;
- sub.cb();
- });
- });
- sub.psubscribe('__keyevent@0__:expired');
- cb();
- },
-
- schedule: (name, time, cb) => {
- console.log(time);
- pub.set(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), '', 'PX', time, 'NX', cb);
- },
-
- subscribe: (name, cb, unique = false) => {
- if (unique && subscriptions.find((subscription) => subscription.originalName == name)) return;
- let subscription = { originalName: name, name: crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'), cb };
- subscriptions.push(subscription);
- return subscription;
- },
-
- remove: (subscription) => {
- let index = subscriptions.indexOf(subscription);
- if (index) subscriptions.splice(index, 1);
- },
- unschedule: (name) => {
- pub.del(crypto.createHash('md5').update(`_notification:${name}_`).digest('hex'));
- },
- };
- module.exports = lib;
|