From fb82526a427e503fff1f1cf56b091ff0c8a5a96e Mon Sep 17 00:00:00 2001 From: Bailey Miller Date: Fri, 25 Dec 2020 19:14:21 -0500 Subject: [PATCH] feat: add redis (#1390) --- docs/reference/notification.md | 8 +++++- package-lock.json | 42 ++++++++++++++++++++++++++++++++ package.json | 2 ++ src/config.ts | 3 +++ src/notification/notification.ts | 2 ++ src/notification/redis.ts | 40 ++++++++++++++++++++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 src/notification/redis.ts diff --git a/docs/reference/notification.md b/docs/reference/notification.md index 5050064c7d..66dff34f9d 100644 --- a/docs/reference/notification.md +++ b/docs/reference/notification.md @@ -142,6 +142,12 @@ Generate token at https://pushover.net/apps/build. ???+ note `PUSHOVER_EXPIRE` and `PUSHOVER_RETRY` are only used when `PUSHOVER_PRIORITY="2"` +## Redis + +| Environment variable | Description | +|:---:|---| +| `REDIS_URL` | Connection string in format (redis://[username]:[password]@[host][:port]/[database-id]) (redis://localhost:6379/1) | + ## Slack | Environment variable | Description | @@ -190,4 +196,4 @@ Generate all Twitter keys at: https://developer.twitter.com/ | `TWITCH_CLIENT_SECRET`| Twitch client secret | | `TWITCH_ACCESS_TOKEN` | Twitch access token | | `TWITCH_REFRESH_TOKEN` | Twitch refresh token | -| `TWITCH_CHANNEL` | Twitch channel | +| `TWITCH_CHANNEL` | Twitch channel | \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 5b6431638c..63d41e5667 100644 --- a/package-lock.json +++ b/package-lock.json @@ -979,6 +979,14 @@ "resolved": "https://registry.npmjs.org/@types/random-useragent/-/random-useragent-0.3.0.tgz", "integrity": "sha512-K6yHeQCCJJBvHeW1akRxXC8GnADsXmoRA2UdVEYITzD88U8Vk3lX+0c0A27j3CrAiic2I961glcnwScc9vUtWg==" }, + "@types/redis": { + "version": "2.8.28", + "resolved": "https://registry.npmjs.org/@types/redis/-/redis-2.8.28.tgz", + "integrity": "sha512-8l2gr2OQ969ypa7hFOeKqtFoY70XkHxISV0pAwmQ2nm6CSPb1brmTmqJCGGrekCo+pAZyWlNXr+Kvo6L/1wijA==", + "requires": { + "@types/node": "*" + } + }, "@types/request": { "version": "2.48.5", "resolved": "https://registry.npmjs.org/@types/request/-/request-2.48.5.tgz", @@ -2957,6 +2965,11 @@ "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, + "denque": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/denque/-/denque-1.4.1.tgz", + "integrity": "sha512-OfzPuSZKGcgr96rf1oODnfjqBFmr1DVoc/TrItj3Ohe0Ah1C5WX5Baquw/9U9KovnQ88EqmJbD66rKYUQYN1tQ==" + }, "des.js": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/des.js/-/des.js-1.0.1.tgz", @@ -9700,6 +9713,35 @@ "strip-indent": "^3.0.0" } }, + "redis": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/redis/-/redis-3.0.2.tgz", + "integrity": "sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==", + "requires": { + "denque": "^1.4.1", + "redis-commands": "^1.5.0", + "redis-errors": "^1.2.0", + "redis-parser": "^3.0.0" + } + }, + "redis-commands": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/redis-commands/-/redis-commands-1.6.0.tgz", + "integrity": "sha512-2jnZ0IkjZxvguITjFTrGiLyzQZcTvaw8DAaCXxZq/dsHXz7KfMQ3OUJy7Tz9vnRtZRVz6VRCPDvruvU8Ts44wQ==" + }, + "redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha1-62LSrbFeTq9GEMBK/hUpOEJQq60=" + }, + "redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha1-tm2CjNyv5rS4pCin3vTGvKwxyLQ=", + "requires": { + "redis-errors": "^1.0.0" + } + }, "regex-not": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/regex-not/-/regex-not-1.0.2.tgz", diff --git a/package.json b/package.json index 11fef033ca..af6ec97dd4 100644 --- a/package.json +++ b/package.json @@ -29,6 +29,7 @@ "@jef/pushbullet": "^2.4.3", "@slack/web-api": "^5.14.0", "@types/random-useragent": "^0.3.0", + "@types/redis": "^2.8.28", "chalk": "^4.1.0", "cheerio": "^1.0.0-rc.3", "discord.js": "^12.5.1", @@ -48,6 +49,7 @@ "puppeteer-extra-plugin-block-resources": "^2.2.7", "puppeteer-extra-plugin-stealth": "^2.6.5", "pushover-notifications": "^1.2.2", + "redis": "^3.0.2", "top-user-agents": "^1.0.19", "twilio": "^3.54.1", "twitch": "^4.3.6", diff --git a/src/config.ts b/src/config.ts index 82934304a2..aa2672ddf3 100644 --- a/src/config.ts +++ b/src/config.ts @@ -279,6 +279,9 @@ const notifications = { token: envOrString(process.env.PUSHOVER_TOKEN), username: envOrString(process.env.PUSHOVER_USER) }, + redis: { + url: envOrString(process.env.REDIS_URL) + }, slack: { channel: envOrString(process.env.SLACK_CHANNEL), token: envOrString(process.env.SLACK_TOKEN) diff --git a/src/notification/notification.ts b/src/notification/notification.ts index 31de64ab6c..23cc9c3457 100644 --- a/src/notification/notification.ts +++ b/src/notification/notification.ts @@ -14,6 +14,7 @@ import {sendTelegramMessage} from './telegram'; import {sendTweet} from './twitter'; import {sendTwilioMessage} from './twilio'; import {sendTwitchMessage} from './twitch'; +import updateRedis from './redis'; export function sendNotification(link: Link, store: Store) { // Priority @@ -33,4 +34,5 @@ export function sendNotification(link: Link, store: Store) { sendTweet(link, store); sendTwilioMessage(link, store); sendTwitchMessage(link, store); + updateRedis(link, store); } diff --git a/src/notification/redis.ts b/src/notification/redis.ts new file mode 100644 index 0000000000..037da83ecc --- /dev/null +++ b/src/notification/redis.ts @@ -0,0 +1,40 @@ +import {Link, Store} from '../store/model'; +import {config} from '../config'; +import {logger} from '../logger'; +import redis from 'redis'; + +const {url} = config.notifications.redis; + +const client = redis.createClient({ + url +}); + +const updateRedis = (link: Link, store: Store) => { + try { + if (url) { + const key = `${store.name}:${link.brand}:${link.model}` + .split(' ') + .join('-'); + + const value = { + ...link, + labels: store.labels, + links: store.links, + name: store.name, + updatedAt: new Date().toUTCString() + }; + + const redisUpdated = client.set(key, JSON.stringify(value)); + + if (redisUpdated) { + logger.info('✔ redis updated'); + } else { + logger.error(`✖ couldn't update redis for key (${key})`); + } + } + } catch (error: unknown) { + logger.error("✖ couldn't update redis", error); + } +}; + +export default updateRedis;