haste_health/commands/worker.rs
1use std::sync::Arc;
2
3use clap::Subcommand;
4use figment::{
5 Figment,
6 providers::{Env, Format as _, Toml},
7};
8use haste_fhir_model::r4::generated::terminology::IssueType;
9use haste_fhir_operation_error::OperationOutcomeError;
10use haste_worker::{search_indexing, traits::Worker as _};
11
12/// Run background workers (search indexing, WAL processing).
13#[derive(Subcommand, Debug)]
14pub(crate) enum WorkerCommands {
15 /// Run the search-indexing worker. Default when no subcommand is given.
16 Worker,
17 /// Run the Postgres WAL (write-ahead log) worker. Not yet implemented.
18 WalWorker,
19}
20
21/// Runs the `worker` command group. `command` is `None` when the bare `worker`
22/// invocation is used, which behaves the same as `worker worker`.
23pub(crate) async fn run(command: &Option<WorkerCommands>) -> Result<(), OperationOutcomeError> {
24 match command {
25 None | Some(WorkerCommands::Worker) => {
26 let config: Arc<search_indexing::WorkerEnvironment> = Arc::new(
27 Figment::new()
28 .merge(Toml::file("haste.toml"))
29 .merge(Env::prefixed("HASTE_"))
30 .extract()
31 .map_err(|e| {
32 OperationOutcomeError::error(IssueType::exception(), e.to_string())
33 })?,
34 );
35
36 let indexing_worker = search_indexing::IndexingWorker::new(config).await?;
37
38 let handler = indexing_worker.run().await?;
39
40 handler.await.map_err(|e| {
41 OperationOutcomeError::fatal(
42 haste_fhir_model::r4::generated::terminology::IssueType::exception(),
43 format!("Worker task failed: {:?}", e),
44 )
45 })?;
46
47 Ok(())
48 }
49 Some(WorkerCommands::WalWorker) => todo!(),
50 }
51}
52
53// async fn create_wal_worker() -> Result<(), Box<dyn std::error::Error>> {
54// let config = get_config::<WALWorkerEnvironmentVariables>("environment".into());
55
56// let connection_url = config
57// .get(WALWorkerEnvironmentVariables::DatabaseURL)
58// .expect(&format!(
59// "'{}' variable not set",
60// String::from(WALWorkerEnvironmentVariables::DatabaseURL)
61// ));
62
63// let slot_name = config
64// .get(WALWorkerEnvironmentVariables::PGSlotName)
65// .expect(&format!(
66// "'{}' variable not set",
67// String::from(WALWorkerEnvironmentVariables::PGSlotName)
68// ));
69// let publication_name = config
70// .get(WALWorkerEnvironmentVariables::PGPublicationName)
71// .expect(&format!(
72// "'{}' variable not set",
73// String::from(WALWorkerEnvironmentVariables::PGPublicationName)
74// ));
75
76// wal_worker(slot_name, publication_name, &connection_url).await;
77
78// Ok(())
79// }