forked from film42/sidekiq-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.rs
236 lines (199 loc) · 7.15 KB
/
processor.rs
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
use super::Result;
use crate::{
periodic::PeriodicJob, Chain, Counter, Job, RedisPool, Scheduled, ServerMiddleware,
StatsPublisher, UnitOfWork, Worker, WorkerRef,
};
use std::collections::BTreeMap;
use std::sync::Arc;
use tracing::{error, info};
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum WorkFetcher {
NoWorkFound,
Done,
}
#[derive(Clone)]
pub struct Processor {
redis: RedisPool,
queues: Vec<String>,
human_readable_queues: Vec<String>,
periodic_jobs: Vec<PeriodicJob>,
workers: BTreeMap<String, Arc<WorkerRef>>,
chain: Chain,
busy_jobs: Counter,
}
impl Processor {
#[must_use]
pub fn new(redis: RedisPool, queues: Vec<String>) -> Self {
let busy_jobs = Counter::new(0);
Self {
chain: Chain::new_with_stats(busy_jobs.clone()),
workers: BTreeMap::new(),
periodic_jobs: vec![],
busy_jobs,
redis,
queues: queues
.iter()
.map(|queue| format!("queue:{queue}"))
.collect(),
human_readable_queues: queues,
}
}
async fn fetch(&mut self) -> Result<Option<UnitOfWork>> {
let response: Option<(String, String)> = self
.redis
.get()
.await?
.brpop(self.queues.clone(), 2)
.await?;
if let Some((queue, job_raw)) = response {
let job: Job = serde_json::from_str(&job_raw)?;
return Ok(Some(UnitOfWork { queue, job }));
}
Ok(None)
}
pub async fn process_one(&mut self) -> Result<()> {
loop {
if let WorkFetcher::NoWorkFound = self.process_one_tick_once().await? {
continue;
}
return Ok(());
}
}
pub async fn process_one_tick_once(&mut self) -> Result<WorkFetcher> {
let work = self.fetch().await?;
if work.is_none() {
return Ok(WorkFetcher::NoWorkFound);
}
let mut work = work.expect("polled and found some work");
let started = std::time::Instant::now();
info!({
"status" = "start",
"class" = &work.job.class,
"queue" = &work.job.queue,
"jid" = &work.job.jid
}, "sidekiq");
if let Some(worker) = self.workers.get_mut(&work.job.class) {
self.chain
.call(&work.job, worker.clone(), self.redis.clone())
.await?;
} else {
error!({
"staus" = "fail",
"class" = &work.job.class,
"queue" = &work.job.queue,
"jid" = &work.job.jid
},"!!! Worker not found !!!");
work.reenqueue(&self.redis).await?;
}
// TODO: Make this only say "done" when the job is successful.
// We might need to change the ChainIter to return the final job and
// detect any retries?
info!({
"elapsed" = format!("{:?}", started.elapsed()),
"status" = "done",
"class" = &work.job.class,
"queue" = &work.job.queue,
"jid" = &work.job.jid}, "sidekiq");
Ok(WorkFetcher::Done)
}
pub fn register<
Args: Sync + Send + for<'de> serde::Deserialize<'de> + 'static,
W: Worker<Args> + 'static,
>(
&mut self,
worker: W,
) {
self.workers
.insert(W::class_name(), Arc::new(WorkerRef::wrap(Arc::new(worker))));
}
pub(crate) async fn register_periodic(&mut self, periodic_job: PeriodicJob) -> Result<()> {
self.periodic_jobs.push(periodic_job.clone());
let mut conn = self.redis.get().await?;
periodic_job.insert(&mut conn).await?;
info!({
"args" = &periodic_job.args,
"class" = &periodic_job.class,
"queue" = &periodic_job.queue,
"name" = &periodic_job.name,
"cron" = &periodic_job.cron,
},"Inserting periodic job");
Ok(())
}
/// Takes self to consume the processor. This is for life-cycle management, not
/// memory safety because you can clone processor pretty easily.
pub async fn run(self) {
let cpu_count = num_cpus::get();
// Start worker routines.
for _ in 0..cpu_count {
tokio::spawn({
let mut processor = self.clone();
async move {
loop {
if let Err(err) = processor.process_one().await {
error!("Error leaked out the bottom: {:?}", err);
}
}
}
});
}
// Start sidekiq-web metrics publisher.
tokio::spawn({
let redis = self.redis.clone();
let queues = self.human_readable_queues.clone();
let busy_jobs = self.busy_jobs.clone();
async move {
let hostname = if let Some(host) = gethostname::gethostname().to_str() {
host.to_string()
} else {
"UNKNOWN_HOSTNAME".to_string()
};
let stats_publisher = StatsPublisher::new(hostname, queues, busy_jobs);
loop {
// TODO: Use process count to meet a 5 second avg.
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
if let Err(err) = stats_publisher.publish_stats(redis.clone()).await {
error!("Error publishing processor stats: {:?}", err);
}
}
}
});
// Start retry and scheduled routines.
tokio::spawn({
let redis = self.redis.clone();
async move {
let sched = Scheduled::new(redis);
let sorted_sets = vec!["retry".to_string(), "schedule".to_string()];
loop {
// TODO: Use process count to meet a 5 second avg.
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
if let Err(err) = sched.enqueue_jobs(chrono::Utc::now(), &sorted_sets).await {
error!("Error in scheduled poller routine: {:?}", err);
}
}
}
});
// Watch for periodic jobs and enqueue jobs.
tokio::spawn({
let redis = self.redis.clone();
async move {
let sched = Scheduled::new(redis);
loop {
// TODO: Use process count to meet a 30 second avg.
tokio::time::sleep(std::time::Duration::from_secs(30)).await;
if let Err(err) = sched.enqueue_periodic_jobs(chrono::Utc::now()).await {
error!("Error in periodic job poller routine: {:?}", err);
}
}
}
});
loop {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
pub async fn using<M>(&mut self, middleware: M)
where
M: ServerMiddleware + Send + Sync + 'static,
{
self.chain.using(Box::new(middleware)).await;
}
}