Skip to content

Commit

Permalink
Merge pull request #43 from AGScheduler/dev
Browse files Browse the repository at this point in the history
feat(Scheduler): supports `RunJob` `ScheduleJob`
  • Loading branch information
kwkwc authored Jun 6, 2024
2 parents 4777f33 + f32a638 commit 31e3717
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "agscheduler-cli"
version = "0.3.3"
version = "0.3.5"
edition = "2021"
description = "Command line interface for AGScheduler"
license = "MIT"
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,14 @@ Options:
$ agscheduler-cli
Connecting to `http://127.0.0.1:36370`...
? Select your operation › [Page 1/3]
Get Info
Get Funcs
Add Job
Get Job
❯ Get All Jobs
Update Job
Delete Job
Delete All Jobs
Pause Job
Resume Job

✔ Select your operation · Get All Jobs
+------------------+-------+----------+-----------+---------------------+---------------------+---------+
Expand Down
59 changes: 59 additions & 0 deletions src/api_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,51 @@ impl AGScheduler {
.await;
}

pub async fn run_or_schedule_job(&self, action: &str, interaction: &dyn InteractionTrait) {
let id = interaction.input_id();

match http::fetch(
format!("{}{}/{}", &self.endpoint, "/scheduler/job", id),
http::Options::default(),
)
.await
{
Ok(result) => {
let args = result["args"].to_string();
let args_value: Value = serde_json::from_str(&args).unwrap();
let queues = result["queues"].to_string();
let queues_value: Value = serde_json::from_str(&queues).unwrap();
let body = json!(
{
"id": id,
"name": result["name"].as_str().unwrap().to_string(),
"type": result["type"].as_str().unwrap().to_string(),
"start_at": result["start_at"].as_str().unwrap().to_string(),
"interval": result["interval"].as_str().unwrap().to_string(),
"cron_expr": result["cron_expr"].as_str().unwrap().to_string(),
"timezone": result["timezone"].as_str().unwrap().to_string(),
"func_name": result["func_name"].as_str().unwrap().to_string(),
"args": args_value,
"timeout": result["timeout"].as_str().unwrap().to_string(),
"queues": queues_value,
}
);
http::fetch_show_ok(
format!("{}{}/{}", &self.endpoint, "/scheduler/job", action),
http::Options {
method: Method::POST,
body: body.to_string(),
..Default::default()
},
)
.await;
}
Err(err) => {
println!("Error: {}", err);
}
}
}

pub async fn start_or_stop(&self, action: &str) {
http::fetch_show_ok(
format!("{}{}/{}", &self.endpoint, "/scheduler", action),
Expand Down Expand Up @@ -698,6 +743,18 @@ mod tests {
.with_body(&empty_data)
.create_async()
.await;
server
.mock("POST", "/scheduler/job/run")
.with_status(200)
.with_body(&empty_data)
.create_async()
.await;
server
.mock("POST", "/scheduler/job/schedule")
.with_status(200)
.with_body(&empty_data)
.create_async()
.await;
server
.mock("POST", "/scheduler/start")
.with_status(200)
Expand Down Expand Up @@ -852,6 +909,8 @@ mod tests {
ags.delete_all_jobs(&mock).await;
ags.pause_or_resume_job("pause", &mock).await;
ags.pause_or_resume_job("resume", &mock).await;
ags.run_or_schedule_job("run", &mock).await;
ags.run_or_schedule_job("schedule", &mock).await;
ags.start_or_stop("start").await;
ags.start_or_stop("stop").await;
ags.get_records(&mock).await;
Expand Down
34 changes: 19 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ async fn main() {

loop {
let selections = &[
"Add Job",
"Get Job",
"Get All Jobs",
"Add Job",
"Update Job",
"Delete Job",
"Delete All Jobs",
"Pause Job",
"Resume Job",
"Run Job",
"Schedule Job",
"Start",
"Stop",
"Get Records",
"Get All Records",
"Delete Records",
"Delete All Records",
"Get Info",
"Get Funcs",
"Start",
"Stop",
"Get Cluster Nodes",
];

Expand All @@ -58,23 +60,25 @@ async fn main() {
.unwrap();

match selection {
0 => ags.get_job(&interaction).await,
1 => ags.get_all_jobs().await,
2 => ags.add_job(&interaction).await,
0 => ags.add_job(&interaction).await,
1 => ags.get_job(&interaction).await,
2 => ags.get_all_jobs().await,
3 => ags.update_job(&interaction).await,
4 => ags.delete_job(&interaction).await,
5 => ags.delete_all_jobs(&interaction).await,
6 => ags.pause_or_resume_job("pause", &interaction).await,
7 => ags.pause_or_resume_job("resume", &interaction).await,
8 => ags.get_records(&interaction).await,
9 => ags.get_all_records(&interaction).await,
10 => ags.delete_records(&interaction).await,
11 => ags.delete_all_records(&interaction).await,
12 => ags.get_info().await,
13 => ags.get_funcs().await,
14 => ags.start_or_stop("start").await,
15 => ags.start_or_stop("stop").await,
16 => ags.get_cluster_nodes().await,
8 => ags.run_or_schedule_job("run", &interaction).await,
9 => ags.run_or_schedule_job("schedule", &interaction).await,
10 => ags.start_or_stop("start").await,
11 => ags.start_or_stop("stop").await,
12 => ags.get_records(&interaction).await,
13 => ags.get_all_records(&interaction).await,
14 => ags.delete_records(&interaction).await,
15 => ags.delete_all_records(&interaction).await,
16 => ags.get_info().await,
17 => ags.get_funcs().await,
18 => ags.get_cluster_nodes().await,
_ => panic!("Error"),
};
}
Expand Down

0 comments on commit 31e3717

Please sign in to comment.