|
| 1 | +use async_nats::jetstream; |
| 2 | + |
| 3 | +use crate::{ |
| 4 | + config::JobConfig, domain::RegisteredJob, error::CronError, nats_impls::NatsConfigStore, |
| 5 | + traits::ConfigStore, |
| 6 | +}; |
| 7 | + |
| 8 | +/// Client for registering and managing CRON job configs. |
| 9 | +/// |
| 10 | +/// Multiple processes can use `CronClient` simultaneously — changes are picked up |
| 11 | +/// by all running `Scheduler` instances in real time via KV watch. |
| 12 | +/// |
| 13 | +/// # Example |
| 14 | +/// |
| 15 | +/// ```rust,no_run |
| 16 | +/// use trogon_cron::{CronClient, JobConfig, Schedule, Action}; |
| 17 | +/// |
| 18 | +/// # async fn example() -> Result<(), trogon_cron::CronError> { |
| 19 | +/// let nats = async_nats::connect("nats://localhost:4222").await.unwrap(); |
| 20 | +/// let client = CronClient::new(nats).await?; |
| 21 | +/// |
| 22 | +/// client.register_job(&JobConfig { |
| 23 | +/// id: "health".to_string(), |
| 24 | +/// schedule: Schedule::Interval { interval_sec: 30 }, |
| 25 | +/// action: Action::Publish { subject: "cron.health".to_string() }, |
| 26 | +/// enabled: true, |
| 27 | +/// payload: None, |
| 28 | +/// retry: None, |
| 29 | +/// }).await?; |
| 30 | +/// # Ok(()) |
| 31 | +/// # } |
| 32 | +/// ``` |
| 33 | +pub struct CronClient<C = NatsConfigStore> { |
| 34 | + store: C, |
| 35 | +} |
| 36 | + |
| 37 | +impl CronClient<NatsConfigStore> { |
| 38 | + /// Connect the client and ensure the config KV bucket exists. |
| 39 | + pub async fn new(nats: async_nats::Client) -> Result<Self, CronError> { |
| 40 | + let js = jetstream::new(nats); |
| 41 | + let store = NatsConfigStore::new(js).await?; |
| 42 | + Ok(Self { store }) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<C: ConfigStore> CronClient<C> { |
| 47 | + /// Create a client backed by any `ConfigStore` implementation. |
| 48 | + pub fn with_store(store: C) -> Self { |
| 49 | + Self { store } |
| 50 | + } |
| 51 | + |
| 52 | + /// Register or update a job. Existing jobs with the same `id` are overwritten. |
| 53 | + /// |
| 54 | + /// Structural config constraints are validated here so callers get an |
| 55 | + /// immediate error rather than a silent scheduler-side skip. Filesystem |
| 56 | + /// checks for spawned binaries are intentionally omitted because the client |
| 57 | + /// may run on a different host than the scheduler. |
| 58 | + pub async fn register_job(&self, config: &JobConfig) -> Result<(), CronError> { |
| 59 | + let _ = RegisteredJob::try_from(config)?; |
| 60 | + self.store.put_job(config).await?; |
| 61 | + tracing::info!(job_id = %config.id, "Job registered"); |
| 62 | + Ok(()) |
| 63 | + } |
| 64 | + |
| 65 | + /// Remove a job by id. No-op if the job doesn't exist. |
| 66 | + pub async fn remove_job(&self, id: &str) -> Result<(), CronError> { |
| 67 | + self.store.delete_job(id).await?; |
| 68 | + tracing::info!(job_id = %id, "Job removed"); |
| 69 | + Ok(()) |
| 70 | + } |
| 71 | + |
| 72 | + /// Enable or disable a job without removing it. |
| 73 | + pub async fn set_enabled(&self, id: &str, enabled: bool) -> Result<(), CronError> { |
| 74 | + let mut config = self |
| 75 | + .get_job(id) |
| 76 | + .await? |
| 77 | + .ok_or_else(|| CronError::Kv(format!("job '{}' not found", id)))?; |
| 78 | + config.enabled = enabled; |
| 79 | + self.register_job(&config).await |
| 80 | + } |
| 81 | + |
| 82 | + /// Get a single job config by id. Returns `None` if not found or deleted. |
| 83 | + pub async fn get_job(&self, id: &str) -> Result<Option<JobConfig>, CronError> { |
| 84 | + self.store.get_job(id).await |
| 85 | + } |
| 86 | + |
| 87 | + /// List all currently active job configs. |
| 88 | + pub async fn list_jobs(&self) -> Result<Vec<JobConfig>, CronError> { |
| 89 | + self.store.list_jobs().await |
| 90 | + } |
| 91 | +} |
0 commit comments