Created
June 18, 2025 11:07
-
-
Save xamgore/09e6ccc52f953f8bbe083cefe26834d0 to your computer and use it in GitHub Desktop.
run_worker_in_own_thread_and_runtime.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn run_worker_in_own_thread(state: StateForWorkers) { | |
// Build a single-threaded Tokio runtime | |
let rt = tokio::runtime::Builder::new_current_thread() | |
.enable_all() | |
.build() | |
.expect("Failed to build runtime"); | |
std::thread::spawn(move || { | |
// Create a LocalSet for running !Send futures | |
let local = tokio::task::LocalSet::new(); | |
// Spawn the !Send future on the local task set | |
local.spawn_local(async move { | |
// Initialize the worker | |
let worker = graphile_worker::WorkerOptions::default() | |
.concurrency(5) | |
.schema(WORKER_DB_SCHEMA) | |
.pg_pool(state.db.get_postgres_connection_pool().clone()) | |
.add_extension(state) | |
.init() | |
.await; | |
if let Ok(worker) = worker { | |
while let Err(err) = worker.run().await { | |
eprintln!("{:?}", err); | |
tokio::time::sleep(std::time::Duration::from_secs(1)).await; | |
} | |
} | |
}); | |
rt.block_on(local); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment