rci/
common.rs

1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3use std::time::{SystemTime, UNIX_EPOCH};
4
5const TKI: Duration = Duration::from_secs(5);
6
7#[derive(Deserialize, Serialize)]
8pub struct OkTrue {
9    ok: bool,
10}
11
12impl Default for OkTrue {
13    fn default() -> Self {
14        Self { ok: true }
15    }
16}
17
18pub(crate) fn now() -> i64 {
19    i64::try_from(
20        SystemTime::now()
21            .duration_since(UNIX_EPOCH)
22            .unwrap()
23            .as_secs(),
24    )
25    .unwrap()
26}
27
28#[inline]
29pub fn tki() -> Duration {
30    TKI
31}
32
33#[derive(Serialize, Deserialize)]
34pub struct TaskId {
35    pub id: i64,
36}
37
38#[cfg(feature = "ci")]
39pub(crate) mod internal {
40    use once_cell::sync::{Lazy, OnceCell};
41    use simple_pool::ResourcePool;
42    use std::path::{Path, PathBuf};
43    use std::time::Duration;
44
45    pub(crate) static VAR_DIR: OnceCell<PathBuf> = OnceCell::new();
46    pub(crate) static WORK_DIR: OnceCell<PathBuf> = OnceCell::new();
47    pub(crate) static LOG_DIR: OnceCell<PathBuf> = OnceCell::new();
48    pub(crate) static JOB_DIR: OnceCell<PathBuf> = OnceCell::new();
49    pub(crate) static SHELL: OnceCell<String> = OnceCell::new();
50
51    pub(crate) static COMMON_COMMANDS: OnceCell<String> = OnceCell::new();
52
53    pub(crate) static TASK_POOL: Lazy<ResourcePool<()>> = Lazy::new(ResourcePool::new);
54
55    const TIMEOUT_FAIL_NOTIFY: Duration = Duration::from_secs(30);
56    const DEFAULT_SHELL: &str = "/bin/sh";
57
58    pub(crate) fn init_task_pool(size: usize) {
59        for _ in 0..size {
60            TASK_POOL.append(());
61        }
62    }
63
64    #[inline]
65    pub(crate) fn var_dir() -> &'static Path {
66        VAR_DIR.get().unwrap()
67    }
68
69    #[inline]
70    pub(crate) fn work_dir() -> &'static Path {
71        WORK_DIR.get().unwrap()
72    }
73
74    #[inline]
75    pub(crate) fn log_dir() -> &'static Path {
76        LOG_DIR.get().unwrap()
77    }
78
79    #[inline]
80    pub(crate) fn job_dir() -> &'static Path {
81        JOB_DIR.get().unwrap()
82    }
83
84    #[inline]
85    pub(crate) fn timeout_fail_notify() -> Duration {
86        TIMEOUT_FAIL_NOTIFY
87    }
88
89    #[inline]
90    pub(crate) fn task_pool() -> &'static ResourcePool<()> {
91        &TASK_POOL
92    }
93
94    pub fn shell_path() -> &'static str {
95        SHELL.get().map_or(DEFAULT_SHELL, String::as_str)
96    }
97
98    #[inline]
99    pub fn common_commands() -> Option<&'static String> {
100        COMMON_COMMANDS.get()
101    }
102}