| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
use std::sync::atomic::{AtomicBool, Ordering}; |
| 20 |
|
| 21 |
use sqlx::PgPool; |
| 22 |
use tokio::sync::watch; |
| 23 |
use tokio::task::JoinHandle; |
| 24 |
|
| 25 |
use crate::constants; |
| 26 |
use crate::db::{self, Slug}; |
| 27 |
|
| 28 |
static PUBLISHED: AtomicBool = AtomicBool::new(false); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
const REFRESH_INTERVAL_SECS: u64 = 60; |
| 34 |
|
| 35 |
|
| 36 |
pub fn is_published() -> bool { |
| 37 |
PUBLISHED.load(Ordering::Relaxed) |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
#[tracing::instrument(skip_all)] |
| 45 |
pub async fn refresh(pool: &PgPool) { |
| 46 |
let slug = Slug::from_trusted(constants::CHANGELOG_PROJECT_SLUG.to_owned()); |
| 47 |
match db::projects::get_public_project_by_slug(pool, &slug).await { |
| 48 |
Ok(project) => PUBLISHED.store(project.is_some(), Ordering::Relaxed), |
| 49 |
Err(e) => { |
| 50 |
tracing::warn!(error = ?e, "changelog visibility refresh failed, keeping last value"); |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
pub fn spawn_refresher(pool: PgPool, mut shutdown_rx: watch::Receiver<()>) -> JoinHandle<()> { |
| 58 |
tokio::spawn(async move { |
| 59 |
let mut interval = |
| 60 |
tokio::time::interval(std::time::Duration::from_secs(REFRESH_INTERVAL_SECS)); |
| 61 |
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); |
| 62 |
|
| 63 |
loop { |
| 64 |
tokio::select! { |
| 65 |
_ = interval.tick() => refresh(&pool).await, |
| 66 |
_ = shutdown_rx.changed() => break, |
| 67 |
} |
| 68 |
} |
| 69 |
}) |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
#[doc(hidden)] |
| 74 |
pub fn set_published_for_test(published: bool) { |
| 75 |
PUBLISHED.store(published, Ordering::Relaxed); |
| 76 |
} |
| 77 |
|