Skip to content

Commit cd71276

Browse files
authored
feat: release for v0.1.17. (#11)
Main features includes: - Only create log file before process being really running. (merge request !136) - Remove pid file after stop agent for sysvinit upgrade. (merge request !137) - Remove todo because of v0.1.15 has been released. (merge request !138) - Use x86_64 as default Windows target - Add sleep before ws thread panic reconnect.
1 parent 3fde1b4 commit cd71276

15 files changed

Lines changed: 76 additions & 59 deletions

File tree

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [0.1.17] - 2021-12-10
6+
7+
### Changed
8+
9+
- Only create log file before process being really running.
10+
- Fix bug of sysvinit upgrade.
11+
- Use x86_64 as default Windows target.
12+
513
## [0.1.16] - 2021-12-09
614

715
### Changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tat_agent"
3-
version = "0.1.16"
3+
version = "0.1.17"
44
edition = "2018"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

install/build.bat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
SET RUSTFLAGS=-C target-feature=+crt-static
2-
rustup target add i686-pc-windows-msvc
3-
cargo build --release --target i686-pc-windows-msvc
4-
copy /Y target\i686-pc-windows-msvc\release\tat_agent.exe install\tat_agent.exe
2+
rustup target add x86_64-pc-windows-msvc
3+
cargo build --release --target x86_64-pc-windows-msvc
4+
copy /Y target\x86_64-pc-windows-msvc\release\tat_agent.exe install\tat_agent.exe

install/install.sh

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,9 @@ install() {
9191
elif has_sysvinit; then
9292
cp -f tat_agent_service /etc/init.d/
9393
chmod 755 /etc/init.d/tat_agent_service
94-
# TODO: uncomment following code after 0.1.15 is released.
95-
# if test "${need_restart}" = true; then
96-
/etc/init.d/tat_agent_service restart
97-
# fi
94+
if test "${need_restart}" = true; then
95+
/etc/init.d/tat_agent_service restart
96+
fi
9897
which chkconfig > /dev/null 2>&1
9998
if [ $? -eq 0 ]; then
10099
echo "use chkconfig to manage service"
@@ -110,17 +109,16 @@ install() {
110109
fi
111110
fi
112111
else
113-
# TODO: uncomment following code after 0.1.15 is released.
114-
# if test "${need_restart}" = true; then
115-
echo "no proper daemon manager found, tat_agent can not auto start"
116-
PID=$(cat ${PID_FILE})
117-
kill ${PID} > /dev/null 2>&1
118-
sleep 0.1 || sleep 1
119-
rm -f ${PID_FILE}
120-
cd ${SERVICE_DIR}
121-
./${TAT_AGENT}
122-
echo "tat_agent started"
123-
# fi
112+
if test "${need_restart}" = true; then
113+
echo "no proper daemon manager found, tat_agent can not auto start"
114+
PID=$(cat ${PID_FILE})
115+
kill ${PID} > /dev/null 2>&1
116+
sleep 0.1 || sleep 1
117+
rm -f ${PID_FILE}
118+
cd ${SERVICE_DIR}
119+
./${TAT_AGENT}
120+
echo "tat_agent started"
121+
fi
124122
fi
125123
}
126124

install/tat_agent_service

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ stop() {
4343
echo "tat_agent not found, nothing to stop"
4444
RET=2
4545
fi
46+
rm -f ${PID_FILE}
4647
else
4748
echo "pid file of tat_agent not exist"
4849
RET=3

src/common/consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ cfg_if::cfg_if! {
3131
} else if #[cfg(windows)] {
3232
pub const TASK_STORE_PATH: &str = "C:\\Program Files\\qcloud\\tat_agent\\tmp\\commands\\";
3333
pub const TASK_LOG_PATH: &str = "C:\\Program Files\\qcloud\\tat_agent\\tmp\\logs\\";
34-
pub const SELF_UPDATE_PATH: &str = "C:\\Program Files\\qcloud\\tat_agent\\tmp\\self_update";
34+
pub const SELF_UPDATE_PATH: &str = "C:\\Program Files\\qcloud\\tat_agent\\tmp\\self_update\\";
3535
pub const SELF_UPDATE_SCRIPT: &str = "self_update.bat";
3636
}
3737
}

src/executor/powershell_command.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ use crate::start_failed_err_info;
44
use async_trait::async_trait;
55
use codepage_strings::Coding;
66
use core::mem;
7-
use log::{debug, error, info};
7+
use log::{debug, error, info, warn};
88
use rand::distributions::Alphanumeric;
99
use rand::{thread_rng, Rng};
1010
use std::ffi::OsStr;
1111
use std::fmt;
1212
use std::fmt::{Debug, Formatter};
13-
use std::fs::{File, OpenOptions};
13+
use std::fs::{remove_file, File, OpenOptions};
1414
use std::io::Write;
1515
use std::os::windows::ffi::OsStrExt;
1616
use std::path::Path;
@@ -104,8 +104,6 @@ impl PowerShellCommand {
104104
impl MyCommand for PowerShellCommand {
105105
/* TODO:
106106
1. support set username
107-
2. support kill process when cancelled or timout.
108-
3. support set process group.
109107
*/
110108
async fn run(&mut self) -> Result<(), String> {
111109
info!("=>PowerShellCommand::run()");
@@ -115,15 +113,18 @@ impl MyCommand for PowerShellCommand {
115113
// work dir check
116114
self.work_dir_check()?;
117115

118-
let log_file = self.open_log_file()?;
119-
120116
// create pipe
121117
let (our_pipe, their_pipe) = anon_pipe(true)?;
122118

123119
// start child
124120
let mut cmd = self.prepare_cmd(their_pipe)?;
121+
let log_file = self.open_log_file()?;
125122
let mut child = cmd.spawn().map_err(|e| {
126123
*self.base.err_info.lock().unwrap() = e.to_string();
124+
// remove log_file when process run failed.
125+
if let Err(e) = remove_file(self.base.log_file_path.as_str()) {
126+
warn!("remove log file failed: {:?}", e)
127+
}
127128
format!(
128129
"PowerShellCommand {}, working_directory:{}, start fail: {}",
129130
self.base.cmd_path, self.base.work_dir, e

src/executor/proc.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ use crate::common::asserts::GracefulUnwrap;
1111
use crate::common::consts::{
1212
FINISH_RESULT_FAILED, FINISH_RESULT_START_FAILED, FINISH_RESULT_SUCCESS,
1313
FINISH_RESULT_TERMINATED, FINISH_RESULT_TIMEOUT, METADATA_API, OUTPUT_BYTE_LIMIT_EACH_REPORT,
14-
TASK_LOG_PATH, TASK_STORE_PATH,
14+
TASK_STORE_PATH,
1515
};
1616
use crate::cos::ObjectAPI;
1717
use crate::cos::COS;
1818
use crate::http::MetadataAPIAdapter;
1919
use crate::ontime::timer::Timer;
2020
use crate::start_failed_err_info;
2121
use async_trait::async_trait;
22-
use log::{debug, error, info};
22+
use log::{debug, error, info, warn};
2323
use std::fmt;
24-
use std::fs::{File, OpenOptions};
24+
use std::fs::{create_dir_all, File, OpenOptions};
25+
use std::path::Path;
2526
use std::sync::atomic::Ordering::SeqCst;
2627
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
2728
use std::sync::{Arc, Mutex};
@@ -468,17 +469,32 @@ impl BaseCommand {
468469
start_failed_err_info!(ERR_SCRIPT_FILE_STORE_FAILED, TASK_STORE_PATH);
469470
return Err(ret);
470471
}
471-
if self.log_file_path.is_empty() {
472-
let ret = format!("start fail because log file store failed.");
473-
*self.err_info.lock().unwrap() =
474-
start_failed_err_info!(ERR_LOG_FILE_STORE_FAILED, TASK_LOG_PATH);
475-
return Err(ret);
476-
}
477472
Ok(())
478473
}
479474

480475
fn open_log_file(&self) -> Result<File, String> {
476+
let parent = Path::new(self.log_file_path.as_str()).parent();
477+
match parent {
478+
Some(parent) => {
479+
if let Err(e) = create_dir_all(parent) {
480+
return Err(format!(
481+
"fail to open task log file {}: create parent dir fail {}: {:?}",
482+
self.log_file_path,
483+
parent.display(),
484+
e
485+
));
486+
}
487+
}
488+
None => {
489+
warn!(
490+
"parent dir not found, skip: {}",
491+
self.log_file_path.as_str()
492+
)
493+
}
494+
}
495+
481496
let res = OpenOptions::new()
497+
.create(true)
482498
.write(true)
483499
.open(self.log_file_path.clone());
484500
match res {
@@ -596,10 +612,10 @@ impl BaseCommand {
596612
}
597613
#[cfg(test)]
598614
mod tests {
615+
use std::fs;
599616
use std::fs::File;
600617
use std::io::Write;
601618
use std::time::{Duration, Instant, SystemTime};
602-
use std::fs;
603619

604620
use log::info;
605621
use rand::distributions::Alphanumeric;
@@ -683,7 +699,6 @@ mod tests {
683699
init_log();
684700
// it doesn't matter even if ./a.sh not exist
685701
let log_path = format!("./{}.log", gen_rand_str());
686-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
687702
let ret = new(
688703
CMD_PATH,
689704
&username(),
@@ -700,7 +715,7 @@ mod tests {
700715
let ret = cmd.run().await;
701716
assert!(ret.is_ok());
702717
info!("cmd running, pid:{}", cmd.pid());
703-
tokio::time::delay_for(Duration::from_secs(4)).await;
718+
tokio::time::delay_for(Duration::from_secs(10)).await;
704719
// now it's NOT a defunct process, cmd will be auto-waited
705720
assert!(!is_process_exist(cmd.pid()).await);
706721
//thread::sleep(Duration::new(10, 0));
@@ -824,7 +839,6 @@ mod tests {
824839
}
825840
}
826841
let log_path = format!("./{}.log", gen_rand_str());
827-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
828842
let ret = new(
829843
filename.as_str(),
830844
&username(),
@@ -879,7 +893,6 @@ mod tests {
879893
}
880894
}
881895
let log_path = format!("./{}.log", gen_rand_str());
882-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
883896
let ret = new(
884897
filename.as_str(),
885898
&username(),
@@ -965,7 +978,6 @@ mod tests {
965978
}
966979
}
967980
let log_path = format!("./{}.log", gen_rand_str());
968-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
969981
let ret = new(
970982
filename.as_str(),
971983
&username(),
@@ -1020,9 +1032,8 @@ mod tests {
10201032
.duration_since(SystemTime::UNIX_EPOCH)
10211033
.unwrap()
10221034
.as_secs();
1023-
info!("script {} start_time:{}",filename, start_time);
1035+
info!("script {} start_time:{}", filename, start_time);
10241036
let log_path = format!("./{}.log", gen_rand_str());
1025-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
10261037
let ret = new(
10271038
filename.as_str(),
10281039
&username(),
@@ -1076,7 +1087,6 @@ mod tests {
10761087
let filename = format!("./.{}.sh", gen_rand_str());
10771088
create_file("echo 'hello world'\nsleep 10 &\ndate", filename.as_str());
10781089
let log_path = format!("./{}.log", gen_rand_str());
1079-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
10801090
let ret = new(
10811091
filename.as_str(),
10821092
&username(),
@@ -1123,7 +1133,6 @@ mod tests {
11231133
);
11241134

11251135
let log_path = format!("./{}.log", gen_rand_str());
1126-
File::create(log_path.as_str()).unwrap_or_exit("create log path fail.");
11271136
let ret = new(
11281137
filename.as_str(),
11291138
&username(),

src/executor/shell_command.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22
use std::fmt::Debug;
3-
use std::fs::File;
3+
use std::fs::{remove_file, File};
44
use std::io::Write;
55
use std::path::Path;
66
use std::process::Stdio;
@@ -13,7 +13,7 @@ use crate::executor::proc::{BaseCommand, MyCommand};
1313
use crate::start_failed_err_info;
1414
use async_trait::async_trait;
1515
use libc;
16-
use log::{debug, error, info};
16+
use log::{debug, error, info, warn};
1717
use procfs::process::Process;
1818
use tokio::io::{AsyncReadExt, BufReader};
1919
use tokio::process::{Child, Command};
@@ -147,10 +147,13 @@ impl MyCommand for ShellCommand {
147147
let user = self.user_check()?;
148148

149149
let log_file = self.open_log_file()?;
150-
151150
// start the process async
152151
let mut child = self.prepare_cmd(user).spawn().map_err(|e| {
153152
*self.base.err_info.lock().unwrap() = e.to_string();
153+
// remove log_file when process run failed.
154+
if let Err(e) = remove_file(self.base.log_file_path.as_str()) {
155+
warn!("remove log file failed: {:?}", e)
156+
}
154157
format!(
155158
"ShellCommand {}, working_directory:{}, start fail: {}",
156159
self.base.cmd_path, self.base.work_dir, e

0 commit comments

Comments
 (0)