runpod_sdk/serverless/
types.rs

1//! Types for the endpoint runner
2
3use serde::{Deserialize, Serialize};
4#[cfg(feature = "strum")]
5use strum::{Display, EnumString};
6
7/// Job status indicating the current state of a serverless job.
8///
9/// Jobs progress through various states from submission to completion or failure.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11#[cfg_attr(feature = "strum", derive(Display, EnumString))]
12#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
13#[cfg_attr(feature = "strum", strum(serialize_all = "SCREAMING_SNAKE_CASE"))]
14pub enum JobStatus {
15    /// Job is waiting in the queue to be processed
16    InQueue,
17    /// Job is currently being processed by a worker
18    InProgress,
19    /// Job has completed successfully
20    Completed,
21    /// Job failed due to an error
22    Failed,
23    /// Job exceeded the execution timeout
24    TimedOut,
25    /// Job was cancelled by the user
26    Cancelled,
27}
28
29impl JobStatus {
30    /// Returns true if the job is in a final state (completed, failed, timed out, or cancelled)
31    pub fn is_final(&self) -> bool {
32        matches!(
33            self,
34            JobStatus::Completed | JobStatus::Failed | JobStatus::TimedOut | JobStatus::Cancelled
35        )
36    }
37
38    /// Returns true if the job completed successfully
39    pub fn is_completed(&self) -> bool {
40        matches!(self, JobStatus::Completed)
41    }
42}
43
44/// Response from the status endpoint
45#[derive(Debug, Clone, Serialize, Deserialize)]
46#[serde(rename_all = "camelCase")]
47pub struct JobStatusResponse {
48    /// Current status of the job
49    pub status: JobStatus,
50    /// Job output (only present when completed)
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub output: Option<serde_json::Value>,
53}
54
55/// Response from the stream endpoint
56#[derive(Debug, Clone, Serialize, Deserialize)]
57pub struct StreamResponse {
58    /// Current status of the job
59    pub status: JobStatus,
60    /// Stream chunks
61    #[serde(default)]
62    pub stream: Vec<StreamChunk>,
63}
64
65/// A single chunk from a streaming response
66#[derive(Debug, Clone, Serialize, Deserialize)]
67pub struct StreamChunk {
68    /// The output data for this chunk
69    pub output: serde_json::Value,
70}
71
72/// Job output that may be returned from a completed job
73pub type JobOutput = serde_json::Value;
74
75/// Health information for an endpoint
76#[derive(Debug, Clone, Serialize, Deserialize)]
77pub struct EndpointHealth {
78    /// Job statistics
79    pub jobs: JobStats,
80    /// Worker statistics
81    pub workers: WorkerStats,
82}
83
84/// Statistics about jobs
85#[derive(Debug, Clone, Serialize, Deserialize)]
86#[serde(rename_all = "camelCase")]
87pub struct JobStats {
88    /// Number of completed jobs
89    pub completed: u32,
90    /// Number of failed jobs
91    pub failed: u32,
92    /// Number of jobs currently in progress
93    pub in_progress: u32,
94    /// Number of jobs waiting in queue
95    pub in_queue: u32,
96    /// Number of retried jobs
97    pub retried: u32,
98}
99
100/// Statistics about workers
101#[derive(Debug, Clone, Serialize, Deserialize)]
102pub struct WorkerStats {
103    /// Number of idle workers
104    pub idle: u32,
105    /// Number of workers initializing
106    pub initializing: u32,
107    /// Number of ready workers
108    pub ready: u32,
109    /// Number of workers currently running jobs
110    pub running: u32,
111    /// Number of throttled workers
112    pub throttled: u32,
113}
114
115/// Request payload for running a job
116#[derive(Debug, Clone, Serialize, Deserialize)]
117pub struct RunRequest {
118    /// The input data for the job
119    pub input: serde_json::Value,
120}
121
122/// Response from submitting a job
123#[derive(Debug, Clone, Serialize, Deserialize)]
124pub struct RunResponse {
125    /// The unique identifier for the submitted job
126    pub id: String,
127    /// Initial status (for runsync endpoint)
128    #[serde(skip_serializing_if = "Option::is_none")]
129    pub status: Option<JobStatus>,
130    /// Output (for runsync endpoint when completed immediately)
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub output: Option<serde_json::Value>,
133}