|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! Job state event notifications for subscribers. |
| 19 | +//! |
| 20 | +//! This module provides a broadcast-based notification system for job state changes. |
| 21 | +//! Consumers can subscribe to receive notifications when jobs change state, avoiding |
| 22 | +//! the need to poll for status updates. |
| 23 | +
|
| 24 | +use std::fmt; |
| 25 | + |
| 26 | +/// Represents the current state of a job in the scheduler. |
| 27 | +/// |
| 28 | +/// This enum mirrors the possible states from the protobuf `job_status::Status` |
| 29 | +/// but is designed to be lightweight for broadcasting. |
| 30 | +#[derive(Debug, Clone, PartialEq, Eq)] |
| 31 | +pub enum JobState { |
| 32 | + /// Job is queued and waiting to be scheduled. |
| 33 | + Queued, |
| 34 | + /// Job is currently running. |
| 35 | + Running, |
| 36 | + /// Job has completed successfully. |
| 37 | + Completed, |
| 38 | + /// Job has failed with an error message. |
| 39 | + Failed(String), |
| 40 | + /// Job was cancelled. |
| 41 | + Cancelled, |
| 42 | +} |
| 43 | + |
| 44 | +impl fmt::Display for JobState { |
| 45 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 46 | + match self { |
| 47 | + JobState::Queued => write!(f, "Queued"), |
| 48 | + JobState::Running => write!(f, "Running"), |
| 49 | + JobState::Completed => write!(f, "Completed"), |
| 50 | + JobState::Failed(msg) => write!(f, "Failed: {}", msg), |
| 51 | + JobState::Cancelled => write!(f, "Cancelled"), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Event emitted when a job's state changes. |
| 57 | +/// |
| 58 | +/// This struct is designed to be cloned and sent through a broadcast channel |
| 59 | +/// to notify subscribers about job state changes. |
| 60 | +#[derive(Debug, Clone)] |
| 61 | +pub struct JobStateEvent { |
| 62 | + /// The unique identifier of the job. |
| 63 | + pub job_id: String, |
| 64 | + /// The new state of the job. |
| 65 | + pub state: JobState, |
| 66 | +} |
| 67 | + |
| 68 | +impl JobStateEvent { |
| 69 | + /// Creates a new job state event. |
| 70 | + pub fn new(job_id: impl Into<String>, state: JobState) -> Self { |
| 71 | + Self { |
| 72 | + job_id: job_id.into(), |
| 73 | + state, |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + /// Creates a queued event for the given job. |
| 78 | + pub fn queued(job_id: impl Into<String>) -> Self { |
| 79 | + Self::new(job_id, JobState::Queued) |
| 80 | + } |
| 81 | + |
| 82 | + /// Creates a running event for the given job. |
| 83 | + pub fn running(job_id: impl Into<String>) -> Self { |
| 84 | + Self::new(job_id, JobState::Running) |
| 85 | + } |
| 86 | + |
| 87 | + /// Creates a completed event for the given job. |
| 88 | + pub fn completed(job_id: impl Into<String>) -> Self { |
| 89 | + Self::new(job_id, JobState::Completed) |
| 90 | + } |
| 91 | + |
| 92 | + /// Creates a failed event for the given job. |
| 93 | + pub fn failed(job_id: impl Into<String>, error: impl Into<String>) -> Self { |
| 94 | + Self::new(job_id, JobState::Failed(error.into())) |
| 95 | + } |
| 96 | + |
| 97 | + /// Creates a cancelled event for the given job. |
| 98 | + pub fn cancelled(job_id: impl Into<String>) -> Self { |
| 99 | + Self::new(job_id, JobState::Cancelled) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +impl fmt::Display for JobStateEvent { |
| 104 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 105 | + write!( |
| 106 | + f, |
| 107 | + "JobStateEvent[job_id={}, state={}]", |
| 108 | + self.job_id, self.state |
| 109 | + ) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +#[cfg(test)] |
| 114 | +mod tests { |
| 115 | + use super::*; |
| 116 | + |
| 117 | + #[test] |
| 118 | + fn test_job_state_event_creation() { |
| 119 | + let event = JobStateEvent::queued("job-123"); |
| 120 | + assert_eq!(event.job_id, "job-123"); |
| 121 | + assert_eq!(event.state, JobState::Queued); |
| 122 | + |
| 123 | + let event = JobStateEvent::running("job-123"); |
| 124 | + assert_eq!(event.state, JobState::Running); |
| 125 | + |
| 126 | + let event = JobStateEvent::completed("job-123"); |
| 127 | + assert_eq!(event.state, JobState::Completed); |
| 128 | + |
| 129 | + let event = JobStateEvent::failed("job-123", "Something went wrong"); |
| 130 | + assert_eq!( |
| 131 | + event.state, |
| 132 | + JobState::Failed("Something went wrong".to_string()) |
| 133 | + ); |
| 134 | + |
| 135 | + let event = JobStateEvent::cancelled("job-123"); |
| 136 | + assert_eq!(event.state, JobState::Cancelled); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn test_job_state_display() { |
| 141 | + assert_eq!(JobState::Queued.to_string(), "Queued"); |
| 142 | + assert_eq!(JobState::Running.to_string(), "Running"); |
| 143 | + assert_eq!(JobState::Completed.to_string(), "Completed"); |
| 144 | + assert_eq!( |
| 145 | + JobState::Failed("error".to_string()).to_string(), |
| 146 | + "Failed: error" |
| 147 | + ); |
| 148 | + assert_eq!(JobState::Cancelled.to_string(), "Cancelled"); |
| 149 | + } |
| 150 | +} |
0 commit comments