Print job duration

This commit is contained in:
asonix 2020-04-21 16:08:19 -05:00
parent f1c4709e41
commit e52348a9ec
2 changed files with 17 additions and 4 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "background-jobs-core" name = "background-jobs-core"
description = "Core types for implementing an asynchronous jobs processor" description = "Core types for implementing an asynchronous jobs processor"
version = "0.8.0-alpha.0" version = "0.8.0-alpha.1"
license-file = "../LICENSE" license-file = "../LICENSE"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/Aardwolf/background-jobs" repository = "https://git.asonix.dog/Aardwolf/background-jobs"

View file

@ -1,4 +1,5 @@
use crate::{Job, JobError, JobInfo, ReturnJobInfo}; use crate::{Job, JobError, JobInfo, ReturnJobInfo};
use chrono::Utc;
use log::{error, info}; use log::{error, info};
use serde_json::Value; use serde_json::Value;
use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc}; use std::{collections::HashMap, future::Future, pin::Pin, sync::Arc};
@ -112,13 +113,25 @@ async fn process<S>(process_fn: &ProcessFn<S>, state: S, job: JobInfo) -> Return
let id = job.id(); let id = job.id();
let name = job.name().to_owned(); let name = job.name().to_owned();
match process_fn(args, state).await { let start = Utc::now();
let res = process_fn(args, state).await;
let end = Utc::now();
let duration = end - start;
let microseconds = duration.num_microseconds();
let seconds: f64 = if let Some(m) = microseconds {
m as f64 / 1_000_000_f64
} else {
0_f64
};
match res {
Ok(_) => { Ok(_) => {
info!("Job {} completed, {}", id, name); info!("Job {} {} completed {:.6}", id, name, seconds);
ReturnJobInfo::pass(id) ReturnJobInfo::pass(id)
} }
Err(e) => { Err(e) => {
info!("Job {} errored, {}, {}", id, name, e); info!("Job {} {} errored {} {:.6}", id, name, e, seconds);
ReturnJobInfo::fail(id) ReturnJobInfo::fail(id)
} }
} }