@@ -703,6 +703,7 @@ export class RedisAdapter implements Adapter {
703703 async createSchedule ( config : ScheduleConfig ) : Promise < string > {
704704 const id = config . id ?? randomUUID ( )
705705 const now = Date . now ( )
706+ const scheduleKey = `${ schedulesKey } ::${ id } `
706707
707708 const scheduleData : Record < string , string > = {
708709 id,
@@ -714,18 +715,19 @@ export class RedisAdapter implements Adapter {
714715 created_at : now . toString ( ) ,
715716 }
716717
717- if ( config . cronExpression ) scheduleData . cron_expression = config . cronExpression
718- if ( config . everyMs ) scheduleData . every_ms = config . everyMs . toString ( )
719- if ( config . from ) scheduleData . from_date = config . from . getTime ( ) . toString ( )
720- if ( config . to ) scheduleData . to_date = config . to . getTime ( ) . toString ( )
721- if ( config . limit ) scheduleData . run_limit = config . limit . toString ( )
722-
723- // Store schedule as hash
724- const scheduleKey = `${ schedulesKey } ::${ id } `
725- await this . #connection. hset ( scheduleKey , scheduleData )
726-
727- // Add to index set for listing
728- await this . #connection. sadd ( schedulesIndexKey , id )
718+ if ( config . cronExpression !== undefined ) scheduleData . cron_expression = config . cronExpression
719+ if ( config . everyMs !== undefined ) scheduleData . every_ms = config . everyMs . toString ( )
720+ if ( config . from !== undefined ) scheduleData . from_date = config . from . getTime ( ) . toString ( )
721+ if ( config . to !== undefined ) scheduleData . to_date = config . to . getTime ( ) . toString ( )
722+ if ( config . limit !== undefined ) scheduleData . run_limit = config . limit . toString ( )
723+
724+ // Upsert schedule and clear stale optional fields from previous config.
725+ await this . #connection
726+ . multi ( )
727+ . hdel ( scheduleKey , 'cron_expression' , 'every_ms' , 'from_date' , 'to_date' , 'run_limit' )
728+ . hset ( scheduleKey , scheduleData )
729+ . sadd ( schedulesIndexKey , id )
730+ . exec ( )
729731
730732 return id
731733 }
0 commit comments