|
1 | 1 | import type { ActorDriver, KvKey, KvValue } from "actor-core/driver-helpers"; |
2 | 2 | import type Redis from "ioredis"; |
3 | 3 | import { KEYS } from "./keys"; |
4 | | -import { AnyActorInstance } from "actor-core/driver-helpers"; |
| 4 | +import type { AnyActorInstance } from "actor-core/driver-helpers"; |
5 | 5 |
|
6 | 6 | export interface ActorDriverContext { |
7 | 7 | redis: Redis; |
8 | 8 | } |
9 | 9 |
|
10 | 10 | export class RedisActorDriver implements ActorDriver { |
11 | 11 | #redis: Redis; |
| 12 | + #subscriptionRedis: Redis | null = null; |
| 13 | + #alarmCallbacks = new Map<string, { actor: AnyActorInstance; timestamp: number }>(); |
12 | 14 |
|
13 | 15 | constructor(redis: Redis) { |
14 | 16 | this.#redis = redis; |
| 17 | + |
| 18 | + // Create a separate connection for subscriptions since a subscribed connection |
| 19 | + // cannot be used for other commands |
| 20 | + this.#subscriptionRedis = redis.duplicate(); |
| 21 | + |
| 22 | + // Subscribe to expired events |
| 23 | + this.#subscriptionRedis.config('SET', 'notify-keyspace-events', 'Ex'); |
| 24 | + this.#subscriptionRedis.subscribe('__keyevent@0__:expired'); |
| 25 | + |
| 26 | + // Handle expired events |
| 27 | + this.#subscriptionRedis.on('message', async (_channel, key) => { |
| 28 | + // Extract actor ID from the key |
| 29 | + const match = key.match(/^actor:(.+):alarm$/); |
| 30 | + if (!match) return; |
| 31 | + |
| 32 | + const actorId = match[1]; |
| 33 | + const callback = this.#alarmCallbacks.get(actorId); |
| 34 | + if (callback) { |
| 35 | + // Verify this is still the current alarm before triggering |
| 36 | + const currentAlarm = await this.getAlarm(callback.actor); |
| 37 | + if (currentAlarm === callback.timestamp) { |
| 38 | + await callback.actor.onAlarm(); |
| 39 | + this.#alarmCallbacks.delete(actorId); |
| 40 | + } |
| 41 | + } |
| 42 | + }); |
15 | 43 | } |
16 | 44 |
|
17 | 45 | getContext(_actorId: string): ActorDriverContext { |
@@ -52,8 +80,40 @@ export class RedisActorDriver implements ActorDriver { |
52 | 80 | await this.#redis.del(key.map((k) => this.#serializeKey(actorId, k))); |
53 | 81 | } |
54 | 82 |
|
55 | | - async setAlarm(_actor: AnyActorInstance, _timestamp: number): Promise<void> { |
56 | | - throw new Error("Alarms are not yet implemented for this driver."); |
| 83 | + async setAlarm(actor: AnyActorInstance, timestamp: number): Promise<void> { |
| 84 | + const key = KEYS.ACTOR.alarm(actor.id); |
| 85 | + |
| 86 | + // Delete any existing alarm first |
| 87 | + await this.deleteAlarm(actor); |
| 88 | + |
| 89 | + const delay = timestamp - Date.now(); |
| 90 | + if (delay <= 0) { |
| 91 | + // If timestamp is in the past, trigger immediately |
| 92 | + await actor.onAlarm(); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + // Store both the actor instance and timestamp for callback verification |
| 97 | + this.#alarmCallbacks.set(actor.id, { actor, timestamp }); |
| 98 | + |
| 99 | + // Set the key with expiration |
| 100 | + await this.#redis.set(key, timestamp.toString(), 'PX', delay); |
| 101 | + } |
| 102 | + |
| 103 | + async getAlarm(actor: AnyActorInstance): Promise<number | null> { |
| 104 | + const key = KEYS.ACTOR.alarm(actor.id); |
| 105 | + |
| 106 | + // Get the timestamp value |
| 107 | + const value = await this.#redis.get(key); |
| 108 | + if (!value) return null; |
| 109 | + |
| 110 | + return Number.parseInt(value, 10); |
| 111 | + } |
| 112 | + |
| 113 | + async deleteAlarm(actor: AnyActorInstance): Promise<void> { |
| 114 | + const key = KEYS.ACTOR.alarm(actor.id); |
| 115 | + await this.#redis.del(key); |
| 116 | + this.#alarmCallbacks.delete(actor.id); |
57 | 117 | } |
58 | 118 |
|
59 | 119 | #serializeKey(actorId: string, key: KvKey): string { |
|
0 commit comments