Skip to content

Commit ed0490b

Browse files
committed
redis.ts: deprecate and migrate remaining uses
1 parent 369965a commit ed0490b

3 files changed

Lines changed: 21 additions & 15 deletions

File tree

bot-monitor/AlertsDb.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import {Cassandra} from "../cassandra";
21
import {MwnDate} from "mwn";
32
import {getKey, Rule} from "./Rule";
43
import {bot, toolsdb} from "../botbase";
5-
import * as fs from "fs/promises";
6-
import {getRedisInstance, Redis} from "../redis";
74
import {ResultSetHeader} from "mysql2";
85
import {CustomError} from "../utils";
96

@@ -78,6 +75,11 @@ class MariadbAlertsDb implements AlertsDb {
7875

7976
/*
8077
UNMAINTAINED IMPLEMENTATIONS:
78+
79+
import {Cassandra} from "../cassandra";
80+
import * as fs from "fs/promises";
81+
import {redis} from "../redis-io";
82+
8183
class CassandraAlertsDb implements AlertsDb {
8284
cs: Cassandra = new Cassandra();
8385
@@ -128,16 +130,14 @@ class FileSystemAlertsDb implements AlertsDb {
128130
}
129131
130132
class RedisAlertsDb implements AlertsDb {
131-
redis: Redis;
132133
async connect() {
133-
this.redis = getRedisInstance();
134134
}
135135
136136
async getLastEmailedTime(rule: Rule) {
137-
return new bot.Date(await this.redis.hget('botmonitor-last-emailed', getKey(rule)));
137+
return new bot.Date(await redis.hget('botmonitor-last-emailed', getKey(rule)));
138138
}
139139
async saveLastEmailedTime(rule: Rule) {
140-
await this.redis.hset('botmonitor-last-emailed', getKey(rule), new bot.Date().toISOString);
140+
await redis.hset('botmonitor-last-emailed', getKey(rule), new bot.Date().toISOString);
141141
}
142142
async getPausedTillTime(name: string, webKey: string) { return new bot.Date(0); }
143143
async setPausedTillTime(bot: string, webKey: string, pauseTill: MwnDate) { return -1; }

bot-monitor/ChecksDb.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {RawRule, subtractFromNow} from './index'
55
import * as sqlite from "sqlite";
66
import * as sqlite3 from "sqlite3";
77
import * as hash from 'object-hash';
8-
import {getRedisInstance} from "../redis";
8+
import {redis} from "../redis-io";
99

1010
class SqliteDb {
1111
db: sqlite.Database;
@@ -37,18 +37,16 @@ interface ChecksDb {
3737
*
3838
*/
3939
class RedisChecksDb implements ChecksDb {
40-
redis = getRedisInstance();
41-
4240
async connect() {
43-
await this.redis.ping();
41+
await redis.ping();
4442
}
4543

4644
getKey(rule: RawRule) {
4745
return `botmonitor-${rule.bot}: ${rule.task}`;
4846
}
4947

5048
async update(rule: RawRule, ts: string) {
51-
await this.redis.hset(this.getKey(rule) + '-cached',
49+
await redis.hset(this.getKey(rule) + '-cached',
5250
'rulehash', hash.sha1(rule),
5351
'ts', ts);
5452
}
@@ -57,7 +55,7 @@ class RedisChecksDb implements ChecksDb {
5755
if (argv.nocache) {
5856
return false;
5957
}
60-
let cached = await this.redis.hgetall(this.getKey(rule) + '-cached');
58+
let cached = await redis.hgetall(this.getKey(rule) + '-cached');
6159
return cached.rulehash === hash.sha1(rule) &&
6260
new bot.Date(cached.ts).isAfter(subtractFromNow(rule.duration));
6361
}
@@ -66,7 +64,7 @@ class RedisChecksDb implements ChecksDb {
6664
if (argv.nocache) {
6765
return;
6866
}
69-
let lastseen = await this.redis.hgetall(this.getKey(rule) + '-seen');
67+
let lastseen = await redis.hgetall(this.getKey(rule) + '-seen');
7068
if (lastseen && lastseen.rulehash === hash.sha1(rule)) {
7169
return lastseen;
7270
} // else return undefined
@@ -80,7 +78,7 @@ class RedisChecksDb implements ChecksDb {
8078
if (notSeen) {
8179
props.notseen = '1';
8280
}
83-
await this.redis.hset(this.getKey(rule) + '-seen', ...Object.entries(props).flat());
81+
await redis.hset(this.getKey(rule) + '-seen', ...Object.entries(props).flat());
8482
}
8583

8684
}

redis.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import * as asyncRedis from "async-redis";
33
import { onToolforge, readFile } from "./utils";
44
import { log } from "./botbase";
55

6+
/**
7+
* @deprecated
8+
* Use redis-io.ts instead
9+
*/
10+
611
// Source: https://github.com/moaxaca/async-redis (MIT)
712
// for some reason the Promisified type that we need isn't exported from there
813
// so we copy-paste that type definition here
@@ -28,6 +33,8 @@ let instance: Redis;
2833
* let item = await redis.get('key');
2934
* await redis.set('key', 'value');
3035
* ...
36+
*
37+
* @deprecated Use redis-io.ts instead
3138
*/
3239
export function createRedisClient(config: redis.ClientOpts = {}): Redis {
3340
return asyncRedis.createClient(getRedisConfig(config));
@@ -50,6 +57,7 @@ export function getRedisConfig(config: redis.ClientOpts = {}): redis.ClientOpts
5057
* For typical usage with the default options.
5158
* Note: this triggers a network request even though it doesn't take a callback or return a
5259
* promise.
60+
* @deprecated Use redis-io.ts instead
5361
*/
5462
export function getRedisInstance(): Redis {
5563
if (!instance) {

0 commit comments

Comments
 (0)