@@ -123,10 +123,20 @@ const RETRY_JOB_SCRIPT = `
123123 return 1
124124`
125125
126+ /**
127+ * Create a new Redis adapter factory.
128+ * Accepts either a Redis instance or Redis options.
129+ *
130+ * When passing options, the adapter will create and manage
131+ * the connection lifecycle (closing it on destroy).
132+ *
133+ * When passing a Redis instance, the caller is responsible for
134+ * managing the connection lifecycle.
135+ */
126136export function redis ( config ?: RedisConfig ) {
127137 return ( ) => {
128138 if ( config instanceof Redis ) {
129- return new RedisAdapter ( config )
139+ return new RedisAdapter ( config , false )
130140 }
131141
132142 const options : RedisOptions = {
@@ -138,24 +148,28 @@ export function redis(config?: RedisConfig) {
138148 }
139149
140150 const connection = new Redis ( options )
141- return new RedisAdapter ( connection )
151+ return new RedisAdapter ( connection , true )
142152 }
143153}
144154
145155export class RedisAdapter implements Adapter {
146156 readonly #connection: Redis
157+ readonly #ownsConnection: boolean
147158 #workerId: string = ''
148159
149- constructor ( connection : Redis ) {
160+ constructor ( connection : Redis , ownsConnection : boolean = false ) {
150161 this . #connection = connection
162+ this . #ownsConnection = ownsConnection
151163 }
152164
153165 setWorkerId ( workerId : string ) : void {
154166 this . #workerId = workerId
155167 }
156168
157169 async destroy ( ) : Promise < void > {
158- await this . #connection. quit ( )
170+ if ( this . #ownsConnection) {
171+ await this . #connection. quit ( )
172+ }
159173 }
160174
161175 pop ( ) : Promise < AcquiredJob | null > {
0 commit comments