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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use crate::error::Result;
use crate::{Job, Queue};
use chrono::{DateTime, Duration as ChronoDuration, Utc};
use crossbeam_channel::{bounded, unbounded, Receiver, Sender};
use postgres::{
    params::{ConnectParams, IntoConnectParams},
    transaction::Transaction,
    Connection, TlsMode,
};
use std::thread;
use std::time::Duration;

const RETRY_DELAY_MS: u32 = 50;
const MAX_DELAY_MS: u32 = 1_209_600_000; // 2 weeks

/// Contains the job and the transaction for use from within a handler
pub struct JobContext<'a> {
    /// Information about and status of the job that is being run
    pub job: &'a Job,

    /// Each job is run within a transaction. Use `tx` to make database calls
    /// within the job to ensure that those calls are rolled-back or committed
    /// in accordance with the success or failure of the job.
    pub tx: &'a Transaction<'a>,
}

/// Implement to run jobs from a `Queue` in a `WorkerPool`
pub trait Handler {
    type Error: std::error::Error;

    fn handle(&self, context: JobContext) -> std::result::Result<(), Self::Error>;
}

/// Configuration options for creating a `WorkerPool`. See `WorkerPool`
/// documentation for examples
#[derive(Clone)]
pub struct WorkerPoolConfig<H> {
    queue: Queue,
    num_workers: usize,
    worker_poll_interval: Duration,
    connect_params: ConnectParams,
    handler: H,
}

/// A group of workers that process jobs from a `Queue`
///
/// # Examples
///
/// ```no_run
/// use dbq::*;
///
/// #[derive(Clone)]
/// struct NoopHandler {}
///
/// impl Handler for NoopHandler {
///     type Error = std::io::Error;
///
///     fn handle(&self, context: JobContext) -> std::result::Result<(), Self::Error> {
///         Ok(())
///     }
/// }
///
/// let queue = Queue::new(SchemaConfig::default(), "de_lancie_q".to_string());
/// let conn_params = "postgres://user@localhost";
/// let mut config = WorkerPoolConfig::new(queue, conn_params, NoopHandler {}).unwrap();
/// // By default the `WorkerPool` is configured to have as many workers as
/// // logical cores on the machine, but it can be configured to be any number
/// config.set_num_workers(2);
///
/// let pool = WorkerPool::start(config);
/// pool.join();
/// ```
pub struct WorkerPool<H> {
    config: WorkerPoolConfig<H>,
    shutdown_chans: Vec<Sender<()>>,
    shutdown_ack_chan: Receiver<usize>,
}

struct Worker<H> {
    worker_id: usize,
    config: WorkerPoolConfig<H>,
    shutdown_chan: Receiver<()>,
    shutdown_ack_chan: Sender<usize>,
}

struct Sentinel<'a, H: Handler + Send + Clone + 'static> {
    worker: &'a Worker<H>,
    active: bool,
    running_job_id: Option<u64>,
}

impl<H> WorkerPoolConfig<H> {
    /// Createa a new `WorkerPoolConfig` that consumes work from the provided
    /// queue and runs it on the provided `Handler`. By default, the number of
    /// workers in the `WorkerPool` is configured to be the number of logical
    /// cores
    #[inline]
    pub fn new<C: IntoConnectParams>(
        queue: Queue,
        db_connect_params: C,
        handler: H,
    ) -> Result<WorkerPoolConfig<H>> {
        let cp = db_connect_params.into_connect_params()?;
        Ok(WorkerPoolConfig {
            queue,
            num_workers: num_cpus::get(),
            worker_poll_interval: Self::default_duration(),
            connect_params: cp,
            handler,
        })
    }

    /// Set the interval between polls for jobs to run.
    #[inline]
    pub fn set_worker_poll_interval(&mut self, interval: Duration) {
        self.worker_poll_interval = interval;
    }

    /// Set the configured number of workers in the `WorkerPool`
    #[inline]
    pub fn set_num_workers(&mut self, size: usize) {
        self.num_workers = size;
    }

    fn default_duration() -> Duration {
        Duration::new(0, 200_000_000) // 200 millis
    }
}

impl<H: Handler + Send + Clone + 'static> WorkerPool<H> {
    /// Create a new `WorkerPool` and start the workers in the pool
    pub fn start(config: WorkerPoolConfig<H>) -> WorkerPool<H> {
        let size = config.num_workers;
        info!("starting worker pool with {} workers", size);
        let (sa_send, sa_recv) = unbounded();
        let mut shutdown_chans = Vec::with_capacity(size);
        for idx in 0..size {
            let (s_send, s_recv) = bounded(1);
            Worker::new(idx + 1, config.clone(), s_recv, sa_send.clone()).spawn();
            shutdown_chans.push(s_send);
        }
        WorkerPool {
            config,
            shutdown_chans,
            shutdown_ack_chan: sa_recv,
        }
    }

    /// Access the `WorkerPoolConfig` for this `WorkerPool`
    pub fn config(&self) -> &WorkerPoolConfig<H> {
        &self.config
    }

    /// Shut down the pool waiting for all currently running jobs to finish.
    /// This function may block for as long as a job takes to run to
    /// completion.
    pub fn join(self) {
        for w in self.shutdown_chans.iter() {
            // ignore errors sending messages
            let _ = w.send(());
        }
        for _ in 0..(self.shutdown_chans.len()) {
            // ignore receive errors
            let _ = self.shutdown_ack_chan.recv();
        }
    }
}

impl<H: Handler + Send + Clone + 'static> Worker<H> {
    fn new(
        worker_id: usize,
        config: WorkerPoolConfig<H>,
        shutdown_chan: Receiver<()>,
        shutdown_ack_chan: Sender<usize>,
    ) -> Worker<H> {
        Worker {
            worker_id,
            config,
            shutdown_chan,
            shutdown_ack_chan,
        }
    }

    fn spawn(self) {
        let worker_id = self.worker_id;
        let _ = thread::spawn(move || {
            self.run();
        });
        debug!("spawned worker {}", worker_id);
    }

    fn run(&self) {
        // Spawns a new worker thread when the thread terminates unless
        // the sentinel is canceled
        let mut sentinel = Sentinel::new(&self);

        let mut conn = match new_conn(self.config.connect_params.clone(), self.worker_id)
        {
            Some(conn) => conn,
            None => return,
        };
        let mut job_last_loop = true;

        loop {
            select! {
                recv(self.shutdown_chan) -> _ => {
                    debug!("worker {} received shutdown signal", self.worker_id);
                    let res = self.shutdown_ack_chan.send(self.worker_id);
                    if let Err(err) = res {
                        warn!("error acking shutdown request. {}", err);
                    }
                    sentinel.cancel();
                    return;
                }
                default => {
                    // Immediately try to reserve a job if one was sucessfully
                    // reserved on the last iteration. If not, delay.
                    if !job_last_loop {
                        thread::sleep(self.config.worker_poll_interval);
                    }
                    if conn.is_desynchronized() {
                        conn = match new_conn(self.config.connect_params.clone(), self.worker_id)
                        {
                            Some(conn) => conn,
                            None => return,
                        };
                    }
                    let res = self.run_job(&conn, &mut sentinel);
                    match res {
                        Ok(found_job) => {
                            job_last_loop = found_job;
                        }
                        Err(err) => {
                            error!("error in worker {}. {}", self.worker_id, err);
                            job_last_loop = false;
                        }
                    }
                }
            }
        }
    }

    fn run_job(&self, conn: &Connection, sentinel: &mut Sentinel<H>) -> Result<bool> {
        let tx = conn.transaction()?;
        let job = self.config.queue.reserve(&tx)?;
        let found_job = job.is_some();

        if let Some(job) = job {
            debug!("worker {} starting job", self.worker_id);
            sentinel.set_running_job(job.id);

            let sub_tx = tx.savepoint("job")?;
            let job_ctx = JobContext {
                job: &job,
                tx: &sub_tx,
            };
            let job_result = self.config.handler.handle(job_ctx);

            match job_result {
                Ok(()) => {
                    debug!("worker {} completed job", self.worker_id);
                    sub_tx.commit()?;
                    self.config.queue.complete(&job, &tx)
                }
                Err(err) => {
                    sub_tx.finish()?;
                    if &job.error_count + 1 < job.max_attempts {
                        warn!(
                            "worker {} error in job {} with error count {}. retrying. {}",
                            self.worker_id,
                            job.id,
                            job.error_count + 1,
                            &err
                        );
                        let next_run_at = next_run_fib(job.error_count, Utc::now());
                        debug!("next run at {}", next_run_at);
                        self.config.queue.mark_error(
                            &job,
                            &format!("{}", err),
                            next_run_at,
                            &tx,
                        )
                    } else {
                        error!(
                            "worker {} job {} failed after {} errors. {}",
                            self.worker_id,
                            job.id,
                            job.error_count + 1,
                            &err
                        );
                        self.config.queue.fail(&job, &format!("{}", err), &tx)
                    }
                }
            }?;
            tx.commit()?;
            sentinel.clear_running_job();
        }
        Ok(found_job)
    }
}

impl<H: Clone> Clone for Worker<H> {
    fn clone(&self) -> Self {
        Worker {
            worker_id: self.worker_id,
            config: self.config.clone(),
            shutdown_chan: self.shutdown_chan.clone(),
            shutdown_ack_chan: self.shutdown_ack_chan.clone(),
        }
    }
}

impl<'a, H: Handler + Send + Clone + 'static> Sentinel<'a, H> {
    fn new(worker: &'a Worker<H>) -> Sentinel<'a, H> {
        Sentinel {
            worker,
            active: true,
            running_job_id: None,
        }
    }

    fn set_running_job(&mut self, job_id: u64) {
        self.running_job_id = Some(job_id);
    }

    fn clear_running_job(&mut self) {
        self.running_job_id = None;
    }

    /// The sentinel will no longer run on drop
    fn cancel(mut self) {
        self.active = false;
    }
}

impl<'a, H: Handler + Send + Clone + 'static> Drop for Sentinel<'a, H> {
    fn drop(&mut self) {
        if self.active {
            debug!("running sentinel for worker {}", self.worker.worker_id);
            if thread::panicking() {
                error!("panic in worker {}", self.worker.worker_id);
                if let Some(rj_id) = self.running_job_id {
                    error!(
                        "job id {} may have cause a panic and be in an incomplete state",
                        rj_id
                    );
                }
            }
            debug!(
                "sentinel for worker {} restarting worker",
                self.worker.worker_id
            );
            self.worker.clone().spawn();
        }
    }
}

fn new_conn(connect_params: ConnectParams, worker_id: usize) -> Option<Connection> {
    match Connection::connect(connect_params, TlsMode::None) {
        Ok(conn) => Some(conn),
        Err(err) => {
            error!(
                "worker {} unable to connect to the database. {}",
                worker_id, err
            );
            // Delay before worker is restarted by the sentinel
            thread::sleep(Duration::new(1, 0));
            None
        }
    }
}

fn next_run_fib(error_count: u32, from: DateTime<Utc>) -> DateTime<Utc> {
    let delay_factor = fib(error_count + 1);
    let delay_ms = RETRY_DELAY_MS * delay_factor;
    if delay_ms <= MAX_DELAY_MS {
        let delay = ChronoDuration::milliseconds(i64::from(delay_ms));
        let next_run_at = from.checked_add_signed(delay);
        if let Some(nr) = next_run_at {
            return nr;
        }
    }
    from.checked_add_signed(ChronoDuration::milliseconds(i64::from(MAX_DELAY_MS)))
        .unwrap()
}

fn fib(n: u32) -> u32 {
    if n == 0 || n == 1 {
        return n;
    }
    let mut sum = 0;
    let mut last = 0;
    let mut curr = 1;
    for _i in 1..n {
        sum = last + curr;
        last = curr;
        curr = sum;
    }
    sum
}