phlow_runtime/settings/
envs.rs

1use std::env;
2
3use phlow_sdk::tracing::debug;
4
5pub struct Envs {
6    /**
7     * Number of package consumers
8     *
9     * This is the number of threads that will be used to process packages.
10     * Environment variable: PHLOW_PACKAGE_CONSUMERS_COUNT
11     * Default: 10
12     */
13    pub package_consumer_count: i32,
14    /**
15     * Minimum allocated memory in MB
16     *
17     * This is the minimum amount of memory that will be allocated to the process.
18     * Environment variable: PHLOW_MIN_ALLOCATED_MEMORY_MB
19     * Default: 10
20     */
21    #[cfg(target_env = "gnu")]
22    pub min_allocated_memory: usize,
23    /**
24     * Enable garbage collection
25     *
26     * This will enable or disable garbage collection.
27     * Environment variable: PHLOW_GARBAGE_COLLECTION_ENABLED
28     * Default: true
29     */
30    #[cfg(target_env = "gnu")]
31    pub garbage_collection: bool,
32    /**
33     * Garbage collection interval in seconds
34     *
35     * This is the interval at which garbage collection will be performed.
36     * Environment variable: PHLOW_GARBAGE_COLLECTION_INTERVAL_SECONDS
37     * Default: 60
38     */
39    #[cfg(target_env = "gnu")]
40    pub garbage_collection_interval: u64,
41
42    /**
43     * Default package repository URL
44     *
45     * This is the URL of the default package repository that will be used to fetch packages.
46     * Environment variable: PHLOW_DEFAULT_PACKAGE_REPOSITORY_URL
47     * Default: https://packages.phlow.dev/packages
48     */
49    pub default_package_repository_url: String,
50    /**
51     * Default phlow file main
52     *
53     * This is the default main file that will be used to run the phlow file.
54     * Environment variable: PHLOW_MAIN
55     * Default: None
56     */
57    pub main: String,
58}
59
60impl Envs {
61    pub fn load() -> Self {
62        let package_consumer_count = env::var("PHLOW_PACKAGE_CONSUMERS_COUNT")
63            .ok()
64            .and_then(|v| v.parse::<usize>().ok())
65            .unwrap_or(10) as i32;
66        #[cfg(target_env = "gnu")]
67        let min_allocated_memory = env::var("PHLOW_MIN_ALLOCATED_MEMORY_MB")
68            .ok()
69            .and_then(|v| v.parse::<usize>().ok())
70            .unwrap_or(10);
71
72        #[cfg(target_env = "gnu")]
73        let garbage_collection = env::var("PHLOW_GARBAGE_COLLECTION_ENABLED")
74            .ok()
75            .and_then(|v| v.parse::<bool>().ok())
76            .unwrap_or(true);
77        #[cfg(target_env = "gnu")]
78        let garbage_collection_interval = env::var("PHLOW_GARBAGE_COLLECTION_INTERVAL_SECONDS")
79            .ok()
80            .and_then(|v| v.parse::<u64>().ok())
81            .unwrap_or(60);
82
83        let default_package_repository_url = match env::var("PHLOW_DEFAULT_PACKAGE_REPOSITORY_URL")
84        {
85            Ok(repo) => repo,
86            Err(_) => "https://packages.phlow.dev/packages".to_string(),
87        };
88
89        let main = env::var("PHLOW_MAIN").unwrap_or(".".to_string());
90
91        debug!("PHLOW_PACKAGE_CONSUMERS_COUNT = {}", package_consumer_count);
92        #[cfg(target_env = "gnu")]
93        debug!("PHLOW_MIN_ALLOCATED_MEMORY_MB = {}", min_allocated_memory);
94        #[cfg(target_env = "gnu")]
95        debug!("PHLOW_GARBAGE_COLLECTION_ENABLED = {}", garbage_collection);
96        #[cfg(target_env = "gnu")]
97        debug!(
98            "PHLOW_GARBAGE_COLLECTION_INTERVAL_SECONDS = {}",
99            garbage_collection_interval
100        );
101        debug!(
102            "PHLOW_DEFAULT_PACKAGE_REPOSITORY_URL = {}",
103            default_package_repository_url
104        );
105
106        Self {
107            package_consumer_count,
108            #[cfg(target_env = "gnu")]
109            min_allocated_memory,
110            #[cfg(target_env = "gnu")]
111            garbage_collection,
112            #[cfg(target_env = "gnu")]
113            garbage_collection_interval,
114            default_package_repository_url,
115            main,
116        }
117    }
118}