@@ -5,7 +5,7 @@ import { OpenAPIHono, type RouteHandler } from '@hono/zod-openapi';
55import type { Context } from 'hono' ;
66import type { UserContext } from '../auth/identity' ;
77import type { IAgentStore } from '../db/agentStore' ;
8- import { syncScheduledRun , type IScheduleStore , type ScheduleRecord } from '../db/scheduleStore' ;
8+ import type { IScheduleStore , ScheduleRecord } from '../db/scheduleStore' ;
99import type { WithTransaction } from '../db/transaction' ;
1010import {
1111 createScheduleRoute ,
@@ -96,19 +96,18 @@ export function createSchedulesRouter<TTransaction>(deps: SchedulesRouterDeps<TT
9696 }
9797
9898 const record = await deps . withTransaction ( async transaction => {
99- const created = await deps . scheduleStore . createSchedule (
99+ const { schedule } = await deps . scheduleStore . createScheduleAndRun (
100100 {
101101 tenant_id : TENANT_ID ,
102102 agent_id : body . agent_id ,
103103 name : body . name ,
104104 manifest : body . manifest ,
105105 created_by : user . userRef ,
106+ runFrom : new Date ( ) ,
106107 } ,
107108 transaction ,
108109 ) ;
109- // A schedule created paused arms nothing; the first PUT that activates it arms.
110- await syncScheduledRun ( deps . scheduleStore , created , new Date ( ) , transaction ) ;
111- return created ;
110+ return schedule ;
112111 } ) ;
113112
114113 return c . json ( { data : toWireSchedule ( record ) } , 201 ) ;
@@ -139,15 +138,17 @@ export function createSchedulesRouter<TTransaction>(deps: SchedulesRouterDeps<TT
139138 validateManifest ( body . manifest ) ;
140139
141140 const record = await deps . withTransaction ( async transaction => {
142- const updated = await deps . scheduleStore . updateSchedule (
143- { tenant_id : TENANT_ID , id : scheduleId , name : body . name , manifest : body . manifest } ,
141+ const result = await deps . scheduleStore . updateScheduleAndRun (
142+ {
143+ tenant_id : TENANT_ID ,
144+ id : scheduleId ,
145+ name : body . name ,
146+ manifest : body . manifest ,
147+ runFrom : new Date ( ) ,
148+ } ,
144149 transaction ,
145150 ) ;
146- if ( updated === undefined ) {
147- return undefined ;
148- }
149- await syncScheduledRun ( deps . scheduleStore , updated , new Date ( ) , transaction ) ;
150- return updated ;
151+ return result ?. schedule ;
151152 } ) ;
152153
153154 if ( record === undefined ) {
0 commit comments