@@ -44,6 +44,7 @@ import * as ThreadLaunchService from "../orchestration-v2/ThreadLaunchService.ts
4444import * as Metrics from "../observability/Metrics.ts" ;
4545import * as ThreadManagementService from "../orchestration-v2/ThreadManagementService.ts" ;
4646import * as SecretRequests from "../secrets/SecretRequests.ts" ;
47+ import * as ServerSettings from "../serverSettings.ts" ;
4748import * as Scheduler from "../scheduling/Scheduler.ts" ;
4849import { decideIdleTrigger } from "./idleTrigger.ts" ;
4950import { isMissedFixedTimeRun , isSameSchedule , nextScheduledRunAt } from "./Schedule.ts" ;
@@ -398,6 +399,15 @@ export const layer = Layer.effect(
398399 const threadManagement = yield * ThreadManagementService . ThreadManagementService ;
399400 const secretRequests = yield * SecretRequests . SecretRequests ;
400401 const scheduler = yield * Scheduler . Scheduler ;
402+ // Optional, so harnesses that build this service without settings still work.
403+ const serverSettings = yield * Effect . serviceOption ( ServerSettings . ServerSettingsService ) ;
404+ // The environment-wide pause. A manual "run now" is the user asking, and still runs.
405+ const tasksPaused = Option . isNone ( serverSettings )
406+ ? Effect . succeed ( false )
407+ : serverSettings . value . getSettings . pipe (
408+ Effect . map ( ( settings ) => settings . scheduledTasksPaused ) ,
409+ Effect . orElseSucceed ( ( ) => false ) ,
410+ ) ;
401411 const readWebhookOrigin = yield * ScheduledTaskWebhookOrigin ;
402412 // Webhook deliveries for one task dispatch in arrival order rather than
403413 // being dropped while an earlier delivery is still dispatching.
@@ -714,6 +724,12 @@ export const layer = Layer.effect(
714724 trigger : "scheduled" | "manual" | "webhook" | "idle" ,
715725 webhook ?: { readonly deliveryId : string ; readonly prompt : string } ,
716726 ) {
727+ if ( trigger !== "manual" && ( yield * tasksPaused ) ) {
728+ if ( webhook !== undefined ) {
729+ return yield * new WebhookDeliverySkipped ( { reason : "Scheduled tasks are paused." } ) ;
730+ }
731+ return task ;
732+ }
717733 const reserved = yield * Ref . modify ( activeRuns , ( active ) => {
718734 if ( active . has ( task . id ) ) return [ false , active ] as const ;
719735 const next = new Set ( active ) ;
@@ -923,6 +939,7 @@ export const layer = Layer.effect(
923939 } ) ;
924940
925941 const runDueTasks = Effect . fn ( "ScheduledTaskService.runDueTasks" ) ( function * ( ) {
942+ if ( yield * tasksPaused ) return ;
926943 const now = yield * localNow ;
927944 const tasks = yield * listDueTasks ( now ) . pipe (
928945 Effect . mapError ( ( cause ) => taskError ( "Could not list schedule tasks." , { cause } ) ) ,
@@ -1006,6 +1023,7 @@ export const layer = Layer.effect(
10061023 // gone quiet. The thread is read here, on the server, right before the
10071024 // prompt is sent; a prompt that still meets a new run queues behind it.
10081025 const runIdleTasks = Effect . fn ( "ScheduledTaskService.runIdleTasks" ) ( function * ( ) {
1026+ if ( yield * tasksPaused ) return ;
10091027 const rows = yield * sql < ScheduledTaskRow > `
10101028 SELECT * FROM scheduled_tasks
10111029 WHERE enabled = 1 AND next_run_at IS NULL AND thread_id IS NOT NULL
0 commit comments