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.
5 * Copyright (C) 2025  Nick Stephenson
6 * Copyright (C) 2025  Ethan Dixon
7 * Copyright (C) 2025  MedicalMasses L.L.C.
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 */
23use rumtk_core::core::RUMResult;
24use rumtk_core::id::id_to_uuid;
25use rumtk_core::strings::RUMString;
26use rumtk_core::threading::threading_manager::{Task, TaskID, TaskManager};
27use rumtk_core::types::RUMBuffer;
28
29pub type JobID = TaskID;
30pub type JobBuffer = RUMBuffer;
31
32#[derive(Default, Debug, Clone)]
33pub enum JobResultType {
34    File(JobBuffer),
35    JSON(RUMString),
36    TEXT(RUMString),
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() -> &'static mut JobManager {
60    let mut manager = unsafe { TASK_MANAGER.as_mut().unwrap() };
61    manager
62}
63
64#[macro_export]
65macro_rules! rumtk_web_init_job_manager {
66    ( $workers:expr ) => {{
67        use $crate::jobs::init_job_manager;
68        init_job_manager($workers)
69    }};
70}
71
72#[macro_export]
73macro_rules! rumtk_web_get_job_manager {
74    (  ) => {{
75        use $crate::jobs::get_manager;
76        get_manager()
77    }};
78}
79
80#[macro_export]
81macro_rules! rumtk_web_generate_job_id {
82    ( $id:expr ) => {{
83        use $crate::jobs::job_str_id_to_id;
84        job_str_id_to_id($id)
85    }};
86}