1 use anyhow
::{format_err, Error}
;
3 use proxmox
::api
::router
::SubdirMap
;
4 use proxmox
::{list_subdirs_api_method, sortable}
;
5 use proxmox
::api
::{api, ApiMethod, Router, RpcEnvironment}
;
7 use crate::api2
::types
::*;
8 use crate::server
::do_verification_job
;
9 use crate::server
::jobstate
::{Job, JobState}
;
10 use crate::config
::verify
;
11 use crate::config
::verify
::{VerificationJobConfig, VerificationJobStatus}
;
12 use serde_json
::Value
;
13 use crate::tools
::systemd
::time
::{parse_calendar_event, compute_next_event}
;
14 use crate::server
::UPID
;
20 schema
: DATASTORE_SCHEMA
,
26 description
: "List configured jobs and their status.",
28 items
: { type: verify::VerificationJobStatus }
,
31 /// List all verification jobs
32 pub fn list_verification_jobs(
33 store
: Option
<String
>,
35 mut rpcenv
: &mut dyn RpcEnvironment
,
36 ) -> Result
<Vec
<VerificationJobStatus
>, Error
> {
38 let (config
, digest
) = verify
::config()?
;
40 let mut list
: Vec
<VerificationJobStatus
> = config
41 .convert_to_typed_array("verification")?
43 .filter(|job
: &VerificationJobStatus
| {
44 if let Some(store
) = &store
{
51 for job
in &mut list
{
52 let last_state
= JobState
::load("verificationjob", &job
.id
)
53 .map_err(|err
| format_err
!("could not open statefile for {}: {}", &job
.id
, err
))?
;
55 let (upid
, endtime
, state
, starttime
) = match last_state
{
56 JobState
::Created { time }
=> (None
, None
, None
, time
),
57 JobState
::Started { upid }
=> {
58 let parsed_upid
: UPID
= upid
.parse()?
;
59 (Some(upid
), None
, None
, parsed_upid
.starttime
)
61 JobState
::Finished { upid, state }
=> {
62 let parsed_upid
: UPID
= upid
.parse()?
;
63 (Some(upid
), Some(state
.endtime()), Some(state
.to_string()), parsed_upid
.starttime
)
67 job
.last_run_upid
= upid
;
68 job
.last_run_state
= state
;
69 job
.last_run_endtime
= endtime
;
71 let last
= job
.last_run_endtime
.unwrap_or_else(|| starttime
);
73 job
.next_run
= (|| -> Option
<i64> {
74 let schedule
= job
.schedule
.as_ref()?
;
75 let event
= parse_calendar_event(&schedule
).ok()?
;
77 compute_next_event(&event
, last
, false).unwrap_or_else(|_
| None
)
81 rpcenv
["digest"] = proxmox
::tools
::digest_to_hex(&digest
).into();
90 schema
: JOB_ID_SCHEMA
,
95 /// Runs a verification job manually.
96 fn run_verification_job(
99 rpcenv
: &mut dyn RpcEnvironment
,
100 ) -> Result
<String
, Error
> {
101 let (config
, _digest
) = verify
::config()?
;
102 let verification_job
: VerificationJobConfig
= config
.lookup("verification", &id
)?
;
104 let userid
: Userid
= rpcenv
.get_user().unwrap().parse()?
;
106 let job
= Job
::new("verificationjob", &id
)?
;
108 let upid_str
= do_verification_job(job
, verification_job
, &userid
, None
)?
;
114 const VERIFICATION_INFO_SUBDIRS
: SubdirMap
= &[("run", &Router
::new().post(&API_METHOD_RUN_VERIFICATION_JOB
))];
116 const VERIFICATION_INFO_ROUTER
: Router
= Router
::new()
117 .get(&list_subdirs_api_method
!(VERIFICATION_INFO_SUBDIRS
))
118 .subdirs(VERIFICATION_INFO_SUBDIRS
);
120 pub const ROUTER
: Router
= Router
::new()
121 .get(&API_METHOD_LIST_VERIFICATION_JOBS
)
122 .match_all("id", &VERIFICATION_INFO_ROUTER
);