|
1 | 1 | use crate::guc::{init_guc, NUM_BGW_PROC}; |
2 | 2 | use crate::init::VECTORIZE_QUEUE; |
3 | 3 | use crate::util::{get_pg_conn, ready}; |
4 | | -use anyhow::Result; |
5 | 4 | use pgrx::bgworkers::*; |
6 | 5 | use pgrx::*; |
7 | | -use std::time::{Duration, Instant}; |
| 6 | +use std::time::Duration; |
8 | 7 |
|
9 | 8 | use crate::workers::run_worker; |
10 | 9 |
|
@@ -45,30 +44,30 @@ pub extern "C" fn background_worker_main(_arg: pg_sys::Datum) { |
45 | 44 |
|
46 | 45 | log!("Starting BG Workers {}", BackgroundWorker::get_name(),); |
47 | 46 |
|
48 | | - while BackgroundWorker::wait_latch(Some(Duration::from_millis(10))) { |
49 | | - runtime.block_on(async { |
50 | | - while !ready(&conn).await { |
51 | | - log!("pg-vectorize: waiting for CREATE EXTENSION vectorize"); |
52 | | - tokio::time::sleep(Duration::from_secs(5)).await; |
53 | | - } |
54 | | - }); |
| 47 | + let mut ext_ready: bool = false; |
| 48 | + let mut wait_duration: Duration = Duration::from_secs(6); |
| 49 | + while BackgroundWorker::wait_latch(Some(wait_duration)) { |
| 50 | + if !ext_ready { |
| 51 | + warning!("pg-vectorize: waiting for CREATE EXTENSION vectorize CASCADE;"); |
| 52 | + runtime.block_on(async { |
| 53 | + ext_ready = ready(&conn).await; |
| 54 | + }); |
| 55 | + // return to wait_latch if extension is not ready |
| 56 | + continue; |
| 57 | + } |
55 | 58 |
|
56 | | - let _worker_ran: Result<()> = runtime.block_on(async { |
57 | | - // continue to poll without pauses |
58 | | - let start = Instant::now(); |
59 | | - let duration = Duration::from_secs(6); |
60 | | - // return control to wait_latch() after `duration` has elapsed |
61 | | - while start.elapsed() < duration { |
62 | | - match run_worker(queue.clone(), &conn, VECTORIZE_QUEUE).await { |
63 | | - // sleep for 2 seconds when no messages in the queue |
64 | | - Ok(None) => tokio::time::sleep(Duration::from_secs(2)).await, |
65 | | - // sleep for 6 seconds when there is an error reading messages |
66 | | - Err(_) => tokio::time::sleep(Duration::from_secs(6)).await, |
67 | | - // continue to poll where there was a message consumed |
68 | | - Ok(Some(_)) => continue, |
69 | | - } |
70 | | - } |
71 | | - Ok(()) |
| 59 | + wait_duration = runtime.block_on(async { |
| 60 | + let wait_dur = match run_worker(queue.clone(), &conn, VECTORIZE_QUEUE).await { |
| 61 | + // no messages in queue, so wait 2 seconds |
| 62 | + Ok(None) => 2000, |
| 63 | + // wait 10 seconds between polls when there is a failure |
| 64 | + Err(_) => 10000, |
| 65 | + // when there was a successfully processed message from queue, |
| 66 | + // only wait 10ms before checking for more messages |
| 67 | + // this allows postgres to kill or restart the bgw in between messages |
| 68 | + Ok(Some(_)) => 10, |
| 69 | + }; |
| 70 | + Duration::from_millis(wait_dur) |
72 | 71 | }); |
73 | 72 | } |
74 | 73 | log!("pg-vectorize: shutting down"); |
|
0 commit comments