Skip to main content

rumtk_web/utils/
jobs.rs

1/*
2 * rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 * This toolkit aims to be reliable, simple, performant, and standards compliant.
4 * Copyright (C) 2025  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 * Copyright (C) 2025  Ethan Dixon
6 * Copyright (C) 2025  MedicalMasses L.L.C. <contact@medicalmasses.com>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 */
21use rumtk_core::core::RUMResult;
22use rumtk_core::id::id_to_uuid;
23use rumtk_core::strings::rumtk_format;
24use rumtk_core::strings::RUMString;
25use rumtk_core::threading::threading_manager::{Task, TaskID, TaskManager};
26use rumtk_core::types::RUMBuffer;
27
28pub type JobID = TaskID;
29pub type JobBuffer = RUMBuffer;
30
31#[derive(Default, Debug, Clone)]
32pub enum JobResultType<T = RUMString> {
33    File(JobBuffer),
34    JSON(RUMString),
35    TEXT(RUMString),
36    RustType(T),
37    #[default]
38    NONE,
39}
40
41pub type JobResult = RUMResult<JobResultType>;
42pub type Job = Task<JobResult>;
43type JobManager = TaskManager<JobResult>;
44
45static mut TASK_MANAGER: Option<JobManager> = None;
46
47pub fn job_str_id_to_id(id: &str) -> JobID {
48    id_to_uuid(id)
49}
50
51pub fn init_job_manager(workers: &usize) -> RUMResult<()> {
52    let manager = TaskManager::<JobResult>::new(workers)?;
53    unsafe {
54        TASK_MANAGER = Some(manager);
55    }
56    Ok(())
57}
58
59pub fn get_manager() -> RUMResult<&'static mut JobManager> {
60    unsafe {
61        match TASK_MANAGER.as_mut() {
62            Some(m) => Ok(m),
63            None => return Err(rumtk_format!("TaskManager is not initialized")),
64        }
65    }
66}
67
68#[macro_export]
69macro_rules! rumtk_web_init_job_manager {
70    ( $workers:expr ) => {{
71        use $crate::jobs::init_job_manager;
72        init_job_manager($workers)
73    }};
74}
75
76#[macro_export]
77macro_rules! rumtk_web_get_job_manager {
78    (  ) => {{
79        use $crate::jobs::get_manager;
80        get_manager()
81    }};
82}
83
84#[macro_export]
85macro_rules! rumtk_web_generate_job_id {
86    ( $id:expr ) => {{
87        use $crate::jobs::job_str_id_to_id;
88        job_str_id_to_id($id)
89    }};
90}