agscheduler-cli 0.5.0

Command line interface for AGScheduler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
use std::collections::HashMap;

use comfy_table::{ContentArrangement, Table};
use reqwest::Method;
use serde_json::{json, Value};

use crate::interaction::InteractionTrait;
use crate::{datetime, http};

pub struct AGScheduler {
    pub endpoint: String,
}

impl AGScheduler {
    async fn _edit_job(
        &self,
        data: HashMap<&str, String>,
        method: Method,
        interaction: &dyn InteractionTrait,
    ) {
        let name = interaction.input_name(data.get("name").unwrap());
        let _type = interaction.select_type().to_lowercase();

        let mut start_at = String::new();
        let mut interval = String::new();
        let mut cron_expr = String::new();
        match _type.as_str() {
            "datetime" => {
                start_at = interaction.input_start_at(data.get("start_at").unwrap());
            }
            "interval" => {
                interval = interaction.input_interval(data.get("interval").unwrap());
            }
            "cron" => {
                cron_expr = interaction.input_cron_expr(data.get("cron_expr").unwrap());
            }
            _ => {}
        }

        let mut tz = iana_time_zone::get_timezone().unwrap();
        if !data.get("timezone").unwrap().is_empty() {
            tz = data.get("timezone").unwrap().to_string();
        }
        let timezone = interaction.input_timezone(&tz);

        let mut fn_selections: Vec<String> = vec![];
        match http::fetch(
            format!("{}{}", &self.endpoint, "/funcs"),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                if let Value::Array(list) = result {
                    for f in list {
                        let f_name = f["name"].as_str().unwrap().to_string();
                        fn_selections.push(f_name);
                    }
                }
            }
            Err(err) => {
                println!("Error: {}", err);
                return;
            }
        }
        let func_name = interaction.select_func_name(fn_selections);

        let args = interaction.input_args(data.get("args").unwrap());
        let timeout = interaction.input_timeout(data.get("timeout").unwrap());
        let queues = interaction.input_queues(data.get("queues").unwrap());

        let args_value: Value = serde_json::from_str(&args).unwrap();
        let queues_value: Value = serde_json::from_str(&queues).unwrap();
        let body = json!(
            {
                "id": data.get("id").unwrap(),
                "name": name,
                "type": _type,
                "start_at": start_at,
                "interval": interval,
                "cron_expr": cron_expr,
                "timezone": timezone,
                "func_name": func_name,
                "args": args_value,
                "timeout": timeout,
                "queues": queues_value,
            }
        );
        http::fetch_show_json(
            format!("{}{}", &self.endpoint, "/scheduler/job"),
            http::Options {
                method,
                body: body.to_string(),
                ..Default::default()
            },
        )
        .await;
    }

    pub async fn add_job(&self, interaction: &dyn InteractionTrait) {
        let mut data = HashMap::new();
        for key in [
            "id",
            "name",
            "type",
            "start_at",
            "interval",
            "cron_expr",
            "timezone",
            "func_name",
            "args",
            "timeout",
            "queues",
        ] {
            data.insert(key, "".to_string());
        }

        self._edit_job(data, Method::POST, interaction).await;
    }

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

        let mut data = HashMap::new();
        match http::fetch(
            format!("{}{}/{}", &self.endpoint, "/scheduler/job", id),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                data.insert("id", id);
                data.insert("args", result["args"].to_string());
                data.insert("queues", result["queues"].to_string());

                for key in [
                    "name",
                    "type",
                    "start_at",
                    "interval",
                    "cron_expr",
                    "timezone",
                    "func_name",
                    "timeout",
                ] {
                    data.insert(key, result[key].as_str().unwrap().to_string());
                }
            }
            Err(err) => {
                println!("Error: {}", err);
                return;
            }
        }

        self._edit_job(data, Method::PUT, interaction).await;
    }

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

        http::fetch_show_json(
            format!("{}{}/{}", &self.endpoint, "/scheduler/job", id),
            http::Options::default(),
        )
        .await;
    }

    pub async fn get_all_jobs(&self) {
        match http::fetch(
            format!("{}{}", &self.endpoint, "/scheduler/jobs"),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                let mut table = Table::new();
                table
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec![
                        "ID",
                        "Name",
                        "Type",
                        "TypeValue",
                        "LastRunTime",
                        "NextRunTime",
                        "Status",
                    ]);

                if let Value::Array(list) = result {
                    let total = list.len();
                    for j in list {
                        let _type = j["type"].as_str().unwrap();
                        let mut type_value = "";
                        match _type {
                            "datetime" => {
                                type_value = j["start_at"].as_str().unwrap();
                            }
                            "interval" => {
                                type_value = j["interval"].as_str().unwrap();
                            }
                            "cron" => {
                                type_value = j["cron_expr"].as_str().unwrap();
                            }
                            _ => {}
                        }
                        let last_run_time =
                            datetime::parse_iso8601_to_local(j["last_run_time"].as_str().unwrap())
                                .unwrap()
                                .format("%Y-%m-%d %H:%M:%S")
                                .to_string();
                        let next_run_time =
                            datetime::parse_iso8601_to_local(j["next_run_time"].as_str().unwrap())
                                .unwrap()
                                .format("%Y-%m-%d %H:%M:%S")
                                .to_string();
                        table.add_row(vec![
                            j["id"].as_str().unwrap(),
                            j["name"].as_str().unwrap(),
                            _type,
                            type_value,
                            &last_run_time[..],
                            &next_run_time[..],
                            j["status"].as_str().unwrap(),
                        ]);
                    }

                    println!("{table}");
                    println!("Total {}", total);
                }
            }
            Err(err) => {
                println!("Error: {}", err)
            }
        }
    }

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

        if !interaction.confirm_delete() {
            return;
        }

        http::fetch_show_ok(
            format!("{}{}/{id}", &self.endpoint, "/scheduler/job"),
            http::Options {
                method: Method::DELETE,
                ..Default::default()
            },
        )
        .await;
    }

    pub async fn delete_all_jobs(&self, interaction: &dyn InteractionTrait) {
        if !interaction.confirm_delete() {
            return;
        }

        http::fetch_show_ok(
            format!("{}{}", &self.endpoint, "/scheduler/jobs"),
            http::Options {
                method: Method::DELETE,
                ..Default::default()
            },
        )
        .await;
    }

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

        http::fetch_show_ok(
            format!("{}{}/{}/{}", &self.endpoint, "/scheduler/job", id, action),
            http::Options {
                method: Method::POST,
                ..Default::default()
            },
        )
        .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),
            http::Options {
                method: Method::POST,
                ..Default::default()
            },
        )
        .await;
    }

    async fn _get_records(&self, job_id: &str, interaction: &dyn InteractionTrait) {
        let page = interaction.input_page("");
        let page_size = interaction.input_page_size("");

        let mut url_path = String::from("/recorder/records");
        if !job_id.is_empty() {
            url_path = format!("{}/{}", url_path, job_id);
        }
        let query: String = format!("page={}&page_size={}", page, page_size);
        url_path = format!("{}?{}", url_path, query);
        match http::fetch(
            format!("{}{}", &self.endpoint, url_path),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                let mut table = Table::new();
                table
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec![
                        "ID", "JobName", "JobId", "Status", "StartAt", "EndAt", "Result",
                    ]);

                if let Value::Object(map) = result {
                    if let Value::Array(list) = &map["res"] {
                        for r in list {
                            let start_at =
                                datetime::parse_iso8601_to_local(r["start_at"].as_str().unwrap())
                                    .unwrap()
                                    .format("%Y-%m-%d %H:%M:%S")
                                    .to_string();
                            let mut end_at = String::from("");
                            if r["status"] != "running" {
                                end_at =
                                    datetime::parse_iso8601_to_local(r["end_at"].as_str().unwrap())
                                        .unwrap()
                                        .format("%Y-%m-%d %H:%M:%S")
                                        .to_string();
                            }
                            table.add_row(vec![
                                &r["id"].to_string(),
                                r["job_name"].as_str().unwrap(),
                                r["job_id"].as_str().unwrap(),
                                r["status"].as_str().unwrap(),
                                &start_at[..],
                                &end_at[..],
                                r["result"].as_str().unwrap(),
                            ]);
                        }

                        println!("{table}");
                    }

                    let page = map["page"].to_string().parse::<f32>().unwrap();
                    let page_size = map["page_size"].to_string().parse::<f32>().unwrap();
                    let total = map["total"].to_string().parse::<f32>().unwrap();
                    let page_count = total / page_size;
                    println!(
                        "Page {}/{}  PageSize {}  Total {}",
                        page,
                        page_count.ceil(),
                        page_size,
                        total
                    );
                }
            }
            Err(err) => {
                println!("Error: {}", err)
            }
        }
    }

    pub async fn get_records(&self, interaction: &dyn InteractionTrait) {
        let job_id = interaction.input_job_id();
        self._get_records(&job_id, interaction).await;
    }

    pub async fn get_all_records(&self, interaction: &dyn InteractionTrait) {
        self._get_records("", interaction).await;
    }

    async fn _delete_records(&self, job_id: &str, interaction: &dyn InteractionTrait) {
        if !interaction.confirm_delete() {
            return;
        }

        let mut url_path = String::from("/recorder/records");
        if !job_id.is_empty() {
            url_path = format!("{}/{}", url_path, job_id);
        }
        http::fetch_show_ok(
            format!("{}{}", &self.endpoint, url_path),
            http::Options {
                method: Method::DELETE,
                ..Default::default()
            },
        )
        .await;
    }

    pub async fn delete_records(&self, interaction: &dyn InteractionTrait) {
        let job_id = interaction.input_job_id();
        self._delete_records(&job_id, interaction).await;
    }

    pub async fn delete_all_records(&self, interaction: &dyn InteractionTrait) {
        self._delete_records("", interaction).await;
    }

    pub async fn get_info(&self) {
        http::fetch_show_json(
            format!("{}{}", &self.endpoint, "/info"),
            http::Options::default(),
        )
        .await;
    }

    pub async fn get_funcs(&self) {
        match http::fetch(
            format!("{}{}", &self.endpoint, "/funcs"),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                let mut table = Table::new();
                table
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec!["name", "info"]);

                if let Value::Array(list) = result {
                    let total = list.len();
                    for f in list {
                        table.add_row(vec![
                            f["name"].as_str().unwrap(),
                            f["info"].as_str().unwrap(),
                        ]);
                    }

                    println!("{table}");
                    println!("Total {}", total);
                }
            }
            Err(err) => {
                println!("Error: {}", err)
            }
        }
    }

    pub async fn get_queues(&self) {
        match http::fetch(
            format!("{}{}", &self.endpoint, "/broker/queues"),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                let mut table = Table::new();
                table
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec!["Name", "Type", "Count", "Workers"]);

                if let Value::Array(list) = result {
                    let total = list.len();
                    for q in list {
                        table.add_row(vec![
                            q["name"].as_str().unwrap(),
                            q["type"].as_str().unwrap(),
                            &q["count"].to_string(),
                            &q["workers"].to_string(),
                        ]);
                    }

                    println!("{table}");
                    println!("Total {}", total);
                }
            }
            Err(err) => {
                println!("Error: {}", err)
            }
        }
    }

    pub async fn get_cluster_nodes(&self) {
        match http::fetch(
            format!("{}{}", &self.endpoint, "/cluster/nodes"),
            http::Options::default(),
        )
        .await
        {
            Ok(result) => {
                let mut table = Table::new();
                table
                    .set_content_arrangement(ContentArrangement::Dynamic)
                    .set_header(vec![
                        "Endpoint",
                        "Leader",
                        "EndpointGRPC",
                        "EndpointHTTP",
                        "EndpointMain",
                        "Queue",
                        "Mode",
                        "Version",
                        "Health",
                        "RegisterTime",
                        "LastHeartbeatTime",
                    ]);

                if let Value::Object(map) = result {
                    let total = map.len();
                    for (_, n) in map.iter() {
                        let mut is_leader = false;
                        if n["endpoint"] == n["endpoint_main"] {
                            is_leader = true;
                        }
                        let register_time =
                            datetime::parse_iso8601_to_local(n["register_time"].as_str().unwrap())
                                .unwrap()
                                .format("%Y-%m-%d %H:%M:%S")
                                .to_string();
                        let last_heartbeat_time = datetime::parse_iso8601_to_local(
                            n["last_heartbeat_time"].as_str().unwrap(),
                        )
                        .unwrap()
                        .format("%Y-%m-%d %H:%M:%S")
                        .to_string();
                        table.add_row(vec![
                            n["endpoint"].as_str().unwrap(),
                            &is_leader.to_string(),
                            n["endpoint_grpc"].as_str().unwrap(),
                            n["endpoint_http"].as_str().unwrap(),
                            n["endpoint_main"].as_str().unwrap(),
                            n["queue"].as_str().unwrap(),
                            n["mode"].as_str().unwrap(),
                            n["version"].as_str().unwrap(),
                            &n["health"].as_bool().unwrap().to_string()[..],
                            &register_time[..],
                            &last_heartbeat_time[..],
                        ]);
                    }

                    println!("{table}");
                    println!("Total {}", total);
                }
            }
            Err(err) => {
                println!("Error: {}", err)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    use crate::interaction::MockInteractionTrait;

    #[tokio::test]
    async fn it_api_client() {
        let mut server = mockito::Server::new_async().await;
        let url = server.url();

        let id = String::from("00227fbf671f4ed2");
        let job_id = String::from("b1638cfb7a8d4247");
        let empty_data = json!({"data": null, "error": ""}).to_string();

        let page = String::from("1");
        let page_size = String::from("10");

        server
            .mock("POST", "/scheduler/job")
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("GET", format!("/scheduler/job/{}", id).as_str())
            .with_status(200)
            .with_body(
                json!({
                    "data":  {
                        "args": {

                        },
                        "cron_expr": "",
                        "end_at": "",
                        "func_name": "github.com/agscheduler/agscheduler/examples.PrintMsg",
                        "id": "00227fbf671f4ed2",
                        "interval": "60s",
                        "last_run_time": "0001-01-01T00:00:00Z",
                        "name": "myJob",
                        "next_run_time": "2024-04-15T04:19:12Z",
                        "queues": [],
                        "start_at": "",
                        "status": "running",
                        "timeout": "1h",
                        "timezone": "UTC",
                        "type": "interval"
                    },
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("GET", "/scheduler/jobs")
            .with_status(200)
            .with_body(
                json!({
                    "data": [
                        {
                            "args": {

                            },
                            "cron_expr": "",
                            "end_at": "",
                            "func_name": "github.com/agscheduler/agscheduler/examples.PrintMsg",
                            "id": "00227fbf671f4ed2",
                            "interval": "60s",
                            "last_run_time": "0001-01-01T00:00:00Z",
                            "name": "myJob",
                            "next_run_time": "2024-04-15T04:19:12Z",
                            "queues": [],
                            "start_at": "",
                            "status": "running",
                            "timeout": "1h",
                            "timezone": "UTC",
                            "type": "interval"
                        },
                        {
                            "args": {

                            },
                            "cron_expr": "",
                            "end_at": "",
                            "func_name": "github.com/agscheduler/agscheduler/examples.PrintMsg",
                            "id": "5hy65y56yh65y56h",
                            "interval": "",
                            "last_run_time": "0001-01-01T00:00:00Z",
                            "name": "myJob2",
                            "next_run_time": "2024-04-15T04:19:12Z",
                            "queues": [],
                            "start_at": "2024-05-16 17:16:08",
                            "status": "running",
                            "timeout": "1h",
                            "timezone": "UTC",
                            "type": "datetime"
                        },
                        {
                            "args": {

                            },
                            "cron_expr": "*/1 * * * *",
                            "end_at": "",
                            "func_name": "github.com/agscheduler/agscheduler/examples.PrintMsg",
                            "id": "n4yhb56j3gj45h56",
                            "interval": "",
                            "last_run_time": "0001-01-01T00:00:00Z",
                            "name": "myJob3",
                            "next_run_time": "2024-04-15T04:19:12Z",
                            "queues": [],
                            "start_at": "",
                            "status": "running",
                            "timeout": "1h",
                            "timezone": "UTC",
                            "type": "cron"
                        }
                    ],
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("PUT", "/scheduler/job")
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("DELETE", format!("/scheduler/job/{}", id).as_str())
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("DELETE", "/scheduler/jobs")
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("POST", format!("/scheduler/job/{}/pause", id).as_str())
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("POST", format!("/scheduler/job/{}/resume", id).as_str())
            .with_status(200)
            .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)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("POST", "/scheduler/stop")
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock(
                "GET",
                format!(
                    "/recorder/records/{}?page={}&page_size={}",
                    job_id, page, page_size
                )
                .as_str(),
            )
            .with_status(200)
            .with_body(
                json!({
                    "data": {
                        "page": 1,
                        "page_size": 10,
                        "res": [
                            {
                                "id": 516544388,
                                "job_id": "b1638cfb7a8d4247",
                                "job_name": "myJob5",
                                "status": "completed",
                                "result": "",
                                "start_at": "2024-06-03T11:27:28.002Z",
                                "end_at": "2024-06-03T11:27:28.027Z"
                            }
                        ],
                        "total": 1
                    },
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock(
                "GET",
                format!("/recorder/records?page={}&page_size={}", page, page_size).as_str(),
            )
            .with_status(200)
            .with_body(
                json!({
                    "data": {
                        "page": 1,
                        "page_size": 10,
                        "res": [
                            {
                                "id": 516544388,
                                "job_id": "b1638cfb7a8d4247",
                                "job_name": "myJob5",
                                "status": "completed",
                                "result": "",
                                "start_at": "2024-06-03T11:27:28.002Z",
                                "end_at": "2024-06-03T11:27:28.027Z"
                            },
                            {
                                "id": 516541097,
                                "job_id": "e99532afe9f44e63",
                                "job_name": "myJob4",
                                "status": "error",
                                "result": "error: something error",
                                "start_at": "2024-06-03T10:54:46.034Z",
                                "end_at": "2024-06-03T10:54:51.069Z"
                            }
                        ],
                        "total": 2
                    },
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("DELETE", format!("/recorder/records/{}", job_id).as_str())
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("DELETE", "/recorder/records")
            .with_status(200)
            .with_body(&empty_data)
            .create_async()
            .await;
        server
            .mock("GET", "/info")
            .with_status(200)
            .with_body(
                json!({
                    "data": {
                        "cluster_main_node": {
                            "endpoint": "127.0.0.1:36380",
                            "endpoint_grpc": "127.0.0.1:36360",
                            "endpoint_http": "127.0.0.1:36370",
                            "endpoint_main": "127.0.0.1:36380",
                            "mode": ""
                        },
                        "is_cluster_mode": true,
                        "is_running": false,
                        "version": "0.6.1"
                    },
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("GET", "/funcs")
            .with_status(200)
            .with_body(
                json!({
                    "data": [
                        {
                            "info": "",
                            "name": "github.com/agscheduler/agscheduler/examples.PrintMsg"
                        }
                    ],
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("GET", "/broker/queues")
            .with_status(200)
            .with_body(
                json!({
                    "data": [
                        {
                            "name": "default",
                            "type": "Memory",
                            "count": 1,
                            "workers": 2,
                        },
                    ],
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;
        server
            .mock("GET", "/cluster/nodes")
            .with_status(200)
            .with_body(
                json!({
                    "data": {
                        "127.0.0.1:36380": {
                            "endpoint": "127.0.0.1:36380",
                            "endpoint_grpc": "127.0.0.1:36360",
                            "endpoint_http": "127.0.0.1:36370",
                            "endpoint_main": "127.0.0.1:36380",
                            "health": true,
                            "last_heartbeat_time": "2024-04-15T04:30:08.489043439Z",
                            "mode": "",
                            "queue": "default",
                            "register_time": "2024-04-15T04:08:10.438222846Z",
                            "version": "0.6.1"
                        }
                    },
                    "error": ""
                })
                .to_string(),
            )
            .create_async()
            .await;

        let mut mock = MockInteractionTrait::new();
        mock.expect_input_id().return_const(id);
        mock.expect_input_job_id().return_const(job_id);
        mock.expect_confirm_delete().return_const(true);
        mock.expect_input_name().return_const("myJob");
        mock.expect_input_start_at()
            .return_const("2024-04-16 15:23:51");
        mock.expect_input_interval().return_const("60s");
        mock.expect_input_cron_expr().return_const("*/1 * * * *");
        mock.expect_input_timezone().return_const("UTC");
        mock.expect_input_args().return_const("{}");
        mock.expect_input_timeout().return_const("1h");
        mock.expect_input_queues().return_const("[]");
        mock.expect_input_page().return_const(page);
        mock.expect_input_page_size().return_const(page_size);
        mock.expect_select_type().return_const("Interval");
        mock.expect_select_func_name()
            .return_const("github.com/agscheduler/agscheduler/examples.PrintMsg");

        let ags = AGScheduler { endpoint: url };

        ags.add_job(&mock).await;
        ags.get_job(&mock).await;
        ags.get_all_jobs().await;
        ags.update_job(&mock).await;
        ags.delete_job(&mock).await;
        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;
        ags.get_all_records(&mock).await;
        ags.delete_records(&mock).await;
        ags.delete_all_records(&mock).await;
        ags.get_info().await;
        ags.get_funcs().await;
        ags.get_queues().await;
        ags.get_cluster_nodes().await;
    }
}