Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
189 lines
5.9 KiB
Swift
189 lines
5.9 KiB
Swift
import Foundation
|
|
import Observation
|
|
import OpenClawKit
|
|
import OpenClawProtocol
|
|
import OSLog
|
|
|
|
@MainActor
|
|
@Observable
|
|
final class CronJobsStore {
|
|
static let shared = CronJobsStore()
|
|
|
|
var jobs: [CronJob] = []
|
|
var selectedJobId: String?
|
|
var runEntries: [CronRunLogEntry] = []
|
|
|
|
var schedulerEnabled: Bool?
|
|
var schedulerStorePath: String?
|
|
var schedulerNextWakeAtMs: Int?
|
|
|
|
var isLoadingJobs = false
|
|
var isLoadingRuns = false
|
|
var lastError: String?
|
|
var statusMessage: String?
|
|
|
|
private let logger = Logger(subsystem: "ai.openclaw", category: "cron.ui")
|
|
private var refreshTask: Task<Void, Never>?
|
|
private var runsTask: Task<Void, Never>?
|
|
private var eventTask: Task<Void, Never>?
|
|
private var pollTask: Task<Void, Never>?
|
|
|
|
private let interval: TimeInterval = 30
|
|
private let isPreview: Bool
|
|
|
|
init(isPreview: Bool = ProcessInfo.processInfo.isPreview) {
|
|
self.isPreview = isPreview
|
|
}
|
|
|
|
func start() {
|
|
guard !self.isPreview else { return }
|
|
guard self.eventTask == nil else { return }
|
|
GatewayPushSubscription.restartTask(task: &self.eventTask) { [weak self] push in
|
|
self?.handle(push: push)
|
|
}
|
|
self.pollTask = Task.detached { [weak self] in
|
|
guard let self else { return }
|
|
await self.refreshJobs()
|
|
while !Task.isCancelled {
|
|
try? await Task.sleep(nanoseconds: UInt64(self.interval * 1_000_000_000))
|
|
await self.refreshJobs()
|
|
}
|
|
}
|
|
}
|
|
|
|
func stop() {
|
|
self.refreshTask?.cancel()
|
|
self.refreshTask = nil
|
|
self.runsTask?.cancel()
|
|
self.runsTask = nil
|
|
self.eventTask?.cancel()
|
|
self.eventTask = nil
|
|
self.pollTask?.cancel()
|
|
self.pollTask = nil
|
|
}
|
|
|
|
func refreshJobs() async {
|
|
guard !self.isLoadingJobs else { return }
|
|
self.isLoadingJobs = true
|
|
self.lastError = nil
|
|
self.statusMessage = nil
|
|
defer { self.isLoadingJobs = false }
|
|
|
|
do {
|
|
if let status = try? await GatewayConnection.shared.cronStatus() {
|
|
self.schedulerEnabled = status.enabled
|
|
self.schedulerStorePath = status.sqlitePath ?? status.storePath
|
|
self.schedulerNextWakeAtMs = status.nextWakeAtMs
|
|
}
|
|
self.jobs = try await GatewayConnection.shared.cronList(includeDisabled: true)
|
|
if self.jobs.isEmpty {
|
|
self.statusMessage = "No cron jobs yet."
|
|
}
|
|
} catch {
|
|
self.logger.error("cron.list failed \(error.localizedDescription, privacy: .public)")
|
|
self.lastError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func refreshRuns(jobId: String, limit: Int = 200) async {
|
|
guard !self.isLoadingRuns else { return }
|
|
self.isLoadingRuns = true
|
|
defer { self.isLoadingRuns = false }
|
|
|
|
do {
|
|
self.runEntries = try await GatewayConnection.shared.cronRuns(jobId: jobId, limit: limit)
|
|
} catch {
|
|
self.logger.error("cron.runs failed \(error.localizedDescription, privacy: .public)")
|
|
self.lastError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func runJob(id: String, force: Bool = true) async {
|
|
do {
|
|
try await GatewayConnection.shared.cronRun(jobId: id, force: force)
|
|
} catch {
|
|
self.lastError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func removeJob(id: String) async {
|
|
do {
|
|
try await GatewayConnection.shared.cronRemove(jobId: id)
|
|
await self.refreshJobs()
|
|
if self.selectedJobId == id {
|
|
self.selectedJobId = nil
|
|
self.runEntries = []
|
|
}
|
|
} catch {
|
|
self.lastError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func setJobEnabled(id: String, enabled: Bool) async {
|
|
do {
|
|
try await GatewayConnection.shared.cronUpdate(
|
|
jobId: id,
|
|
patch: ["enabled": AnyCodable(enabled)])
|
|
await self.refreshJobs()
|
|
} catch {
|
|
self.lastError = error.localizedDescription
|
|
}
|
|
}
|
|
|
|
func upsertJob(
|
|
id: String?,
|
|
payload: [String: AnyCodable]) async throws
|
|
{
|
|
if let id {
|
|
try await GatewayConnection.shared.cronUpdate(jobId: id, patch: payload)
|
|
} else {
|
|
try await GatewayConnection.shared.cronAdd(payload: payload)
|
|
}
|
|
await self.refreshJobs()
|
|
}
|
|
|
|
// MARK: - Gateway events
|
|
|
|
private func handle(push: GatewayPush) {
|
|
switch push {
|
|
case let .event(evt) where evt.event == "cron":
|
|
guard let payload = evt.payload else { return }
|
|
if let cronEvt = try? GatewayPayloadDecoding.decode(payload, as: CronEvent.self) {
|
|
self.handle(cronEvent: cronEvt)
|
|
}
|
|
case .seqGap:
|
|
self.scheduleRefresh()
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
|
|
private func handle(cronEvent evt: CronEvent) {
|
|
// Keep UI in sync with the gateway scheduler.
|
|
self.scheduleRefresh(delayMs: 250)
|
|
if evt.action == "finished", let selected = self.selectedJobId, selected == evt.jobId {
|
|
self.scheduleRunsRefresh(jobId: selected, delayMs: 200)
|
|
}
|
|
}
|
|
|
|
private func scheduleRefresh(delayMs: Int = 250) {
|
|
self.refreshTask?.cancel()
|
|
self.refreshTask = Task { [weak self] in
|
|
guard let self else { return }
|
|
try? await Task.sleep(nanoseconds: UInt64(delayMs) * 1_000_000)
|
|
await self.refreshJobs()
|
|
}
|
|
}
|
|
|
|
private func scheduleRunsRefresh(jobId: String, delayMs: Int = 200) {
|
|
self.runsTask?.cancel()
|
|
self.runsTask = Task { [weak self] in
|
|
guard let self else { return }
|
|
try? await Task.sleep(nanoseconds: UInt64(delayMs) * 1_000_000)
|
|
await self.refreshRuns(jobId: jobId)
|
|
}
|
|
}
|
|
|
|
// MARK: - (no additional RPC helpers)
|
|
}
|