-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rs
More file actions
172 lines (158 loc) · 6.25 KB
/
config.rs
File metadata and controls
172 lines (158 loc) · 6.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//! Service and poller configuration types.
use derive_builder::Builder;
use es_entity::clock::{Clock, ClockHandle};
use serde::{Deserialize, Serialize};
use std::time::Duration;
#[serde_with::serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
/// Controls how the background poller balances work across processes.
pub struct JobPollerConfig {
#[serde_as(as = "serde_with::DurationSeconds<u64>")]
#[serde(default = "default_job_lost_interval")]
/// How long a job may be in a 'running' state
pub job_lost_interval: Duration,
#[serde(default = "default_max_jobs_per_process")]
/// Maximum number of concurrent jobs this process will execute.
pub max_jobs_per_process: usize,
#[serde(default = "default_min_jobs_per_process")]
/// Minimum number of concurrent jobs to keep running before the poller sleeps.
pub min_jobs_per_process: usize,
#[serde_as(as = "serde_with::DurationSeconds<u64>")]
#[serde(default = "default_shutdown_timeout")]
/// How long to wait for jobs to complete gracefully during shutdown before rescheduling them.
pub shutdown_timeout: Duration,
#[serde_as(as = "serde_with::DurationSeconds<u64>")]
#[serde(default = "default_cancel_timeout")]
/// How long to wait for a cancelled job to finish cooperatively before force-aborting it.
pub cancel_timeout: Duration,
}
impl Default for JobPollerConfig {
fn default() -> Self {
Self {
job_lost_interval: default_job_lost_interval(),
max_jobs_per_process: default_max_jobs_per_process(),
min_jobs_per_process: default_min_jobs_per_process(),
shutdown_timeout: default_shutdown_timeout(),
cancel_timeout: default_cancel_timeout(),
}
}
}
#[derive(Builder, Debug, Clone)]
#[builder(build_fn(skip))]
/// Configuration consumed by [`Jobs::init`](crate::Jobs::init).
/// Build with [`JobSvcConfig::builder`](Self::builder).
///
/// # Examples
///
/// Build a configuration that manages its own Postgres pool from a connection string:
///
/// ```no_run
/// use job::{Jobs, JobSvcConfig};
/// use job::error::JobError;
///
/// # async fn run() -> Result<(), JobError> {
/// let config = JobSvcConfig::builder()
/// .pg_con("postgres://postgres:password@localhost/postgres")
/// .build()
/// .unwrap();
///
/// let mut jobs = Jobs::init(config).await?;
/// jobs.start_poll().await?;
/// # Ok(())
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run()).unwrap();
/// ```
///
/// Reuse an existing `sqlx::PgPool` instead:
///
/// ```no_run
/// use job::{Jobs, JobSvcConfig};
/// use job::error::JobError;
/// use sqlx::postgres::PgPoolOptions;
///
/// # async fn run() -> Result<(), JobError> {
/// let pool = PgPoolOptions::new()
/// .connect_lazy("postgres://postgres:password@localhost/postgres")?;
///
/// let config = JobSvcConfig::builder()
/// .pool(pool)
/// .exec_migrations(false) // migrations already handled elsewhere
/// .build()
/// .unwrap();
///
/// let mut jobs = Jobs::init(config).await?;
/// jobs.start_poll().await?;
/// # Ok(())
/// # }
/// # tokio::runtime::Runtime::new().unwrap().block_on(run()).unwrap();
/// ```
pub struct JobSvcConfig {
#[builder(setter(into, strip_option), default)]
/// Provide a Postgres connection string used to build an internal pool. Mutually exclusive with `pool`. When set, `exec_migrations` defaults to `true` unless overridden.
pub(super) pg_con: Option<String>,
#[builder(setter(into, strip_option), default)]
/// Override the maximum number of connections the internally managed pool may open. Ignored when `pool` is supplied.
pub(super) max_connections: Option<u32>,
#[builder(default)]
/// Set to `true` to have `Jobs::init` run the embedded database migrations during startup. Defaults to `false`, unless `pg_con` is supplied without an explicit value.
pub(super) exec_migrations: bool,
#[builder(setter(into, strip_option), default)]
/// Inject an existing `sqlx::PgPool` instead of letting the job service build one. Mutually exclusive with `pg_con`.
pub(super) pool: Option<sqlx::PgPool>,
#[builder(default)]
/// Override the defaults that control how the background poller distributes work across processes.
pub poller_config: JobPollerConfig,
#[builder(setter(into), default = "Clock::handle()")]
/// Clock handle for time operations. Defaults to the global clock.
/// The global clock is realtime unless an artificial clock was installed.
pub clock: ClockHandle,
}
impl JobSvcConfig {
/// Create a [`JobSvcConfigBuilder`] with defaults for all optional settings.
pub fn builder() -> JobSvcConfigBuilder {
JobSvcConfigBuilder::default()
}
}
impl JobSvcConfigBuilder {
/// Validate and construct a [`JobSvcConfig`], ensuring either `pg_con` or `pool` is set.
pub fn build(&mut self) -> Result<JobSvcConfig, String> {
// Validate configuration
match (self.pg_con.as_ref(), self.pool.as_ref()) {
(None, None) | (Some(None), None) | (None, Some(None)) => {
return Err("One of pg_con or pool must be set".to_string());
}
(Some(_), Some(_)) => return Err("Only one of pg_con or pool must be set".to_string()),
_ => (),
}
// If pg_con is provided and exec_migrations is not explicitly set, default to true
if matches!(self.pg_con.as_ref(), Some(Some(_))) && self.exec_migrations.is_none() {
self.exec_migrations = Some(true);
}
Ok(JobSvcConfig {
pg_con: self.pg_con.clone().flatten(),
max_connections: self.max_connections.flatten(),
exec_migrations: self.exec_migrations.unwrap_or(false),
pool: self.pool.clone().flatten(),
poller_config: self.poller_config.clone().unwrap_or_default(),
clock: self
.clock
.clone()
.unwrap_or_else(|| Clock::handle().clone()),
})
}
}
fn default_job_lost_interval() -> Duration {
Duration::from_secs(60 * 5)
}
fn default_max_jobs_per_process() -> usize {
50
}
fn default_min_jobs_per_process() -> usize {
30
}
fn default_shutdown_timeout() -> Duration {
Duration::from_secs(5)
}
fn default_cancel_timeout() -> Duration {
Duration::from_secs(5)
}