Skip to content

Commit f325d05

Browse files
committed
update code
1 parent 368939f commit f325d05

File tree

16 files changed

+196
-242
lines changed

16 files changed

+196
-242
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
**/.vscode/
55
**/.venv/**
66
**/dist/**
7+
logs/**
78

89
**.wasm
910

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ parking_lot = "0.12"
5151
arrow-array = "52"
5252
arrow-ipc = "52"
5353
arrow-schema = "52"
54+
proctitle = "0.1"
5455

5556
[features]
5657
default = ["incremental-cache", "python"]

Makefile

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,26 @@ PACKAGE_DIR := packages
2121
PYTHON_WASM_PATH := python/functionstream-runtime/target/functionstream-python-runtime.wasm
2222
PYTHON_WASM_NAME := functionstream-python-runtime.wasm
2323

24-
.PHONY: help clean clean-dist build build-full build-lite package package-full package-lite package-all test install docker-build docker-up fmt fmt-check
24+
.PHONY: help clean clean-dist build build-full build-lite package package-full package-lite package-all test install
2525

2626
help:
2727
@echo "Function Stream Build System"
2828
@echo ""
2929
@echo "Available targets:"
30-
@echo " fmt - Format code with cargo fmt"
31-
@echo " fmt-check - Check code format without modifying"
3230
@echo " build - Build full version (debug)"
3331
@echo " build-full - Build full release version"
3432
@echo " build-lite - Build lite release version (no Python)"
3533
@echo " package-full - Build and package full version (.zip and .tar.gz)"
3634
@echo " package-lite - Build and package lite version (.zip and .tar.gz)"
3735
@echo " package-all - Build and package both versions"
38-
@echo " docker-build - Run package-full then docker-compose build"
39-
@echo " docker-up - Run package-full then docker-compose up -d"
40-
@echo " test - Run tests"
36+
@echo " test - Run tests"
4137
@echo " clean - Clean all build artifacts (cargo + dist)"
4238
@echo " clean-dist - Clean distribution directory only"
4339
@echo ""
4440
@echo "Version: $(VERSION)"
4541
@echo "Architecture: $(ARCH)"
4642
@echo "OS: $(OS)"
4743

48-
fmt:
49-
@echo "Formatting code..."
50-
cargo fmt --all
51-
52-
fmt-check:
53-
@echo "Checking code format..."
54-
cargo fmt --all -- --check
55-
5644
clean:
5745
@echo "Cleaning build artifacts..."
5846
cargo clean
@@ -232,14 +220,6 @@ package-all: clean-dist package-full package-lite
232220
@echo "All packages created:"
233221
@ls -lh $(DIST_BASE)/$(PACKAGE_DIR)/
234222

235-
docker-build: package-full
236-
@echo "Building Docker image (using dist/packages/function-stream-$(VERSION).zip)..."
237-
docker-compose build
238-
239-
docker-up: package-full
240-
@echo "Building and starting containers..."
241-
docker-compose up -d --build
242-
243223
test:
244224
cargo test
245225

src/config/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ pub mod wasm_config;
2222
pub use global_config::GlobalConfig;
2323
pub use loader::load_global_config;
2424
pub use log_config::LogConfig;
25+
#[allow(unused_imports)]
2526
pub use paths::{
26-
find_config_file, find_or_create_conf_dir, find_or_create_data_dir, find_or_create_logs_dir,
27+
ENV_CONF, ENV_HOME, find_config_file, get_app_log_path, get_conf_dir, get_data_dir,
28+
get_log_path, get_logs_dir, get_project_root, get_python_cache_dir, get_python_cwasm_path,
29+
get_python_wasm_path, get_state_dir, get_state_dir_for_base, get_task_dir, get_wasm_cache_dir,
30+
resolve_path,
2731
};
2832
pub use python_config::PythonConfig;

src/config/paths.rs

Lines changed: 105 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,94 +10,136 @@
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
1212

13+
use std::env;
1314
use std::fs;
14-
use std::path::{Path, PathBuf};
15+
use std::path::PathBuf;
16+
use std::sync::OnceLock;
1517

16-
fn get_exe_dir() -> Option<PathBuf> {
17-
std::env::current_exe().ok()?.parent().map(PathBuf::from)
18+
pub const ENV_HOME: &str = "FUNCTION_STREAM_HOME";
19+
pub const ENV_CONF: &str = "FUNCTION_STREAM_CONF";
20+
21+
static PROJECT_ROOT: OnceLock<PathBuf> = OnceLock::new();
22+
23+
pub fn get_project_root() -> &'static PathBuf {
24+
PROJECT_ROOT
25+
.get_or_init(|| resolve_project_root().expect("CRITICAL: Failed to resolve project root"))
1826
}
1927

20-
fn search_paths<F>(mut check: F) -> Option<PathBuf>
21-
where
22-
F: FnMut(&Path) -> bool,
23-
{
24-
let cwd = PathBuf::from(".");
25-
if check(cwd.as_path()) {
26-
return Some(cwd);
28+
fn resolve_project_root() -> std::io::Result<PathBuf> {
29+
if let Ok(home) = env::var(ENV_HOME) {
30+
let path = PathBuf::from(&home);
31+
return path.canonicalize().or(Ok(path));
2732
}
2833

29-
if let Some(exe_dir) = get_exe_dir() {
30-
if check(exe_dir.as_path()) {
31-
return Some(exe_dir);
32-
}
34+
if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
35+
return Ok(PathBuf::from(manifest_dir));
36+
}
3337

34-
if let Some(parent) = exe_dir.parent() {
35-
if check(parent) {
36-
return Some(parent.to_path_buf());
37-
}
38+
if let Ok(exe_path) = env::current_exe() {
39+
let mut path = exe_path;
40+
path.pop();
41+
if path.file_name().map_or(false, |n| n == "bin") {
42+
path.pop();
3843
}
44+
return Ok(path);
3945
}
4046

41-
None
47+
env::current_dir()
4248
}
4349

44-
pub fn find_config_file(config_name: &str) -> Option<PathBuf> {
45-
let config_path = PathBuf::from(config_name);
46-
if config_path.exists() {
47-
return Some(config_path);
50+
pub fn resolve_path(input_path: &str) -> PathBuf {
51+
let path = PathBuf::from(input_path);
52+
if path.is_absolute() {
53+
path
54+
} else {
55+
get_project_root().join(path)
4856
}
57+
}
4958

50-
search_paths(|base| base.join(config_name).exists())
51-
.or_else(|| {
52-
get_exe_dir().and_then(|exe_dir| {
53-
exe_dir.parent().and_then(|parent| {
54-
let path = parent.join("conf").join(config_name);
55-
path.exists().then_some(path)
56-
})
57-
})
58-
})
59-
.or_else(|| {
60-
std::env::var("FUNCTION_STREAM_CONFIG")
61-
.ok()
62-
.map(PathBuf::from)
63-
.filter(|p| p.exists())
64-
})
65-
}
66-
67-
fn find_or_create_dir(dir_name: &str) -> std::io::Result<PathBuf> {
68-
let cwd_dir = PathBuf::from(dir_name);
69-
if cwd_dir.exists() {
70-
return Ok(cwd_dir);
71-
}
59+
fn to_absolute_path(input_path: &str) -> PathBuf {
60+
resolve_path(input_path)
61+
}
7262

73-
if let Some(exe_dir) = get_exe_dir() {
74-
if let Some(parent) = exe_dir.parent() {
75-
let dir = parent.join(dir_name);
76-
if dir.exists() {
77-
return Ok(dir);
63+
pub fn find_config_file(config_name: &str) -> Option<PathBuf> {
64+
if let Ok(conf_env) = env::var(ENV_CONF) {
65+
let path = to_absolute_path(&conf_env);
66+
if path.is_file() {
67+
return Some(path);
68+
}
69+
if path.is_dir() {
70+
let full = path.join(config_name);
71+
if full.exists() {
72+
return Some(full);
7873
}
79-
fs::create_dir_all(&dir)?;
80-
return Ok(dir);
8174
}
75+
}
76+
77+
let search_paths = vec![
78+
get_conf_dir().join(config_name),
79+
get_project_root().join(config_name),
80+
];
8281

83-
let dir = exe_dir.join(dir_name);
84-
if dir.exists() {
85-
return Ok(dir);
82+
for path in search_paths {
83+
if path.exists() {
84+
return Some(path.canonicalize().unwrap_or(path));
8685
}
8786
}
8887

89-
fs::create_dir_all(&cwd_dir)?;
90-
Ok(cwd_dir)
88+
None
89+
}
90+
91+
fn get_or_create_sub_dir(name: &str) -> PathBuf {
92+
let dir = get_project_root().join(name);
93+
if !dir.exists() {
94+
let _ = fs::create_dir_all(&dir);
95+
}
96+
dir
97+
}
98+
99+
pub fn get_data_dir() -> PathBuf {
100+
get_or_create_sub_dir("data")
101+
}
102+
103+
pub fn get_logs_dir() -> PathBuf {
104+
get_or_create_sub_dir("logs")
105+
}
106+
107+
pub fn get_conf_dir() -> PathBuf {
108+
get_or_create_sub_dir("conf")
109+
}
110+
111+
pub fn get_task_dir() -> PathBuf {
112+
get_or_create_sub_dir("data/task")
113+
}
114+
115+
pub fn get_state_dir() -> PathBuf {
116+
get_or_create_sub_dir("data/state")
117+
}
118+
119+
pub fn get_state_dir_for_base(base: &str) -> PathBuf {
120+
resolve_path(base).join("state")
121+
}
122+
123+
pub fn get_app_log_path() -> PathBuf {
124+
get_logs_dir().join("app.log")
125+
}
126+
127+
pub fn get_log_path(relative: &str) -> PathBuf {
128+
get_logs_dir().join(relative)
129+
}
130+
131+
pub fn get_wasm_cache_dir() -> PathBuf {
132+
get_or_create_sub_dir("data/cache/wasm-incremental")
91133
}
92134

93-
pub fn find_or_create_data_dir() -> std::io::Result<PathBuf> {
94-
find_or_create_dir("data")
135+
pub fn get_python_cache_dir() -> PathBuf {
136+
get_or_create_sub_dir("data/cache/python-runner")
95137
}
96138

97-
pub fn find_or_create_conf_dir() -> std::io::Result<PathBuf> {
98-
find_or_create_dir("conf")
139+
pub fn get_python_wasm_path() -> PathBuf {
140+
get_python_cache_dir().join("functionstream-python-runtime.wasm")
99141
}
100142

101-
pub fn find_or_create_logs_dir() -> std::io::Result<PathBuf> {
102-
find_or_create_dir("logs")
143+
pub fn get_python_cwasm_path() -> PathBuf {
144+
get_python_cache_dir().join("functionstream-python-runtime.cwasm")
103145
}

src/config/python_config.rs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,19 @@
1313
use serde::{Deserialize, Serialize};
1414
use std::path::PathBuf;
1515

16-
/// Default directory for Python runtime cache (wasm and cwasm files)
17-
pub const DEFAULT_PYTHON_CACHE_DIR: &str = "data/cache/python-runner";
18-
19-
/// Default Python WASM filename
2016
pub const DEFAULT_PYTHON_WASM_FILENAME: &str = "functionstream-python-runtime.wasm";
21-
22-
/// Default Python compiled WASM cache filename
2317
pub const DEFAULT_PYTHON_CWASM_FILENAME: &str = "functionstream-python-runtime.cwasm";
2418

2519
fn default_python_wasm_path() -> String {
26-
format!(
27-
"{}/{}",
28-
DEFAULT_PYTHON_CACHE_DIR, DEFAULT_PYTHON_WASM_FILENAME
29-
)
20+
super::paths::get_python_wasm_path()
21+
.to_string_lossy()
22+
.to_string()
3023
}
3124

3225
fn default_python_cache_dir() -> String {
33-
DEFAULT_PYTHON_CACHE_DIR.to_string()
26+
super::paths::get_python_cache_dir()
27+
.to_string_lossy()
28+
.to_string()
3429
}
3530

3631
fn default_true() -> bool {
@@ -39,19 +34,12 @@ fn default_true() -> bool {
3934

4035
#[derive(Debug, Clone, Serialize, Deserialize)]
4136
pub struct PythonConfig {
42-
/// Path to the Python WASM file
43-
/// Default: data/cache/python-runner/functionstream-python-runtime.wasm
4437
#[serde(default = "default_python_wasm_path")]
4538
pub wasm_path: String,
4639

47-
/// Cache directory for precompiled components
48-
/// Default: data/cache/python-runner
4940
#[serde(default = "default_python_cache_dir")]
5041
pub cache_dir: String,
5142

52-
/// Enable component caching
53-
/// If true, precompiled components will be cached to speed up subsequent loads
54-
/// Default: true
5543
#[serde(default = "default_true")]
5644
pub enable_cache: bool,
5745
}
@@ -67,18 +55,15 @@ impl Default for PythonConfig {
6755
}
6856

6957
impl PythonConfig {
70-
/// Get the WASM file path as PathBuf
7158
pub fn wasm_path_buf(&self) -> PathBuf {
72-
PathBuf::from(&self.wasm_path)
59+
super::paths::resolve_path(&self.wasm_path)
7360
}
7461

75-
/// Get the cache directory as PathBuf
7662
pub fn cache_dir_buf(&self) -> PathBuf {
77-
PathBuf::from(&self.cache_dir)
63+
super::paths::resolve_path(&self.cache_dir)
7864
}
7965

80-
/// Get the precompiled component cache file path (cwasm)
8166
pub fn cwasm_cache_path(&self) -> PathBuf {
82-
self.cache_dir_buf().join(DEFAULT_PYTHON_CWASM_FILENAME)
67+
super::paths::resolve_path(&self.cache_dir).join(DEFAULT_PYTHON_CWASM_FILENAME)
8368
}
8469
}

src/config/wasm_config.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
use serde::{Deserialize, Serialize};
1414

1515
fn default_wasm_cache_dir() -> String {
16-
".cache/wasm-incremental".to_string()
16+
crate::config::paths::get_wasm_cache_dir()
17+
.to_string_lossy()
18+
.to_string()
1719
}
1820

1921
fn default_true() -> bool {

0 commit comments

Comments
 (0)