@@ -2,7 +2,7 @@ import { Redis } from "ioredis";
22import { Closable } from "../Closable" ;
33import { decodeRedisKey , encodeRedisKey } from "../encodeRedisKey" ;
44import { Job } from "../Job" ;
5- import { Producer } from "../producer/producer " ;
5+ import minimatch from "minimatch " ;
66
77/**
88 * Like String.split, but has a maximum number of delimiters it picks up.
@@ -34,19 +34,21 @@ function splitEvent(message: string, maxParts: number, delimiter = ":") {
3434}
3535
3636export type SubscriptionOptions = { queue ?: string ; id ?: string } ;
37- export type OnActivity = ( event : OnActivityEvent ) => Promise < void > | void ;
37+ export type OnActivity < ScheduleType extends string > = (
38+ event : OnActivityEvent < ScheduleType >
39+ ) => Promise < void > | void ;
3840
39- export type OnActivityEvent =
40- | ScheduledEvent
41+ export type OnActivityEvent < ScheduleType extends string > =
42+ | ScheduledEvent < ScheduleType >
4143 | DeletedEvent
4244 | RequestedEvent
4345 | InvokedEvent
4446 | RescheduledEvent
4547 | AcknowledgedEvent ;
4648
47- interface ScheduledEvent {
49+ interface ScheduledEvent < ScheduleType extends string > {
4850 type : "scheduled" ;
49- job : Job ;
51+ job : Job < ScheduleType > ;
5052}
5153
5254interface InvokedEvent {
@@ -81,35 +83,34 @@ interface AcknowledgedEvent {
8183}
8284
8385export class Activity < ScheduleType extends string > implements Closable {
84- private redis ;
85- private producer ;
86-
86+ private readonly pattern : string ;
8787 constructor (
88- redisFactory : ( ) => Redis ,
89- private readonly onEvent : OnActivity ,
88+ private readonly redis : Redis ,
89+ private readonly onEvent : OnActivity < ScheduleType > ,
9090 options : SubscriptionOptions = { }
9191 ) {
92- this . redis = redisFactory ( ) ;
93- this . producer = new Producer < ScheduleType > ( redisFactory , null as any ) ;
94-
9592 this . redis . on ( "pmessage" , ( _pattern , channel , message ) =>
9693 this . handleMessage ( channel , message )
9794 ) ;
9895
99- if ( options . queue ) {
100- options . queue = encodeRedisKey ( options . queue ) ;
101- }
96+ const queue = options . queue ? encodeRedisKey ( options . queue ) : "*" ;
97+ const id = options . id ? encodeRedisKey ( options . id ) : "*" ;
98+ this . pattern = ` ${ queue } : ${ id } ` ;
10299
103- if ( options . id ) {
104- options . id = encodeRedisKey ( options . id ) ;
105- }
100+ this . redis . psubscribe ( this . pattern ) ;
101+ }
106102
107- this . redis . psubscribe ( `${ options . queue ?? "*" } :${ options . id ?? "*" } ` ) ;
103+ private matchesPattern ( channel : string ) {
104+ return minimatch ( channel , this . pattern ) ;
108105 }
109106
110107 private async handleMessage ( channel : string , message : string ) {
108+ if ( ! this . matchesPattern ( channel ) ) {
109+ return ;
110+ }
111+
111112 const [ _type , ...args ] = splitEvent ( message , 9 ) ;
112- const type = _type as OnActivityEvent [ "type" ] ;
113+ const type = _type as OnActivityEvent < ScheduleType > [ "type" ] ;
113114
114115 const channelParts = channel . split ( ":" ) . map ( decodeRedisKey ) ;
115116 if ( channelParts . length !== 2 ) {
@@ -141,7 +142,7 @@ export class Activity<ScheduleType extends string> implements Closable {
141142 retry : JSON . parse ( retryJson ) ,
142143 schedule : schedule_type
143144 ? {
144- type : schedule_type ,
145+ type : schedule_type as ScheduleType ,
145146 meta : schedule_meta ,
146147 times : max_times ? Number ( max_times ) : undefined ,
147148 }
@@ -166,7 +167,6 @@ export class Activity<ScheduleType extends string> implements Closable {
166167 }
167168
168169 async close ( ) {
169- await this . redis . quit ( ) ;
170- await this . producer . close ( ) ;
170+ await this . redis . punsubscribe ( this . pattern ) ;
171171 }
172172}
0 commit comments