runwhen/src/timer.rs

61 lines
1.7 KiB
Rust
Raw Normal View History

2017-01-29 16:39:48 -06:00
// Copyright 2017 Jeremy Wall <jeremy@marzhillstudios.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::thread;
use std::time::Duration;
use error::CommandError;
use exec::run_cmd;
2021-12-30 18:57:55 -05:00
use traits::Process;
2017-01-29 16:39:48 -06:00
pub struct TimerProcess<'a> {
cmd: &'a str,
env: Option<Vec<&'a str>>,
2017-01-29 16:39:48 -06:00
poll_duration: Duration,
max_repeat: Option<u32>,
2017-01-29 16:39:48 -06:00
}
impl<'a> TimerProcess<'a> {
2021-12-30 18:57:55 -05:00
pub fn new(
cmd: &'a str,
env: Option<Vec<&'a str>>,
poll_duration: Duration,
max_repeat: Option<u32>,
) -> TimerProcess<'a> {
TimerProcess {
cmd: cmd,
env: env,
poll_duration: poll_duration,
max_repeat: max_repeat,
}
2017-01-29 16:39:48 -06:00
}
}
impl<'a> Process for TimerProcess<'a> {
fn run(&self) -> Result<(), CommandError> {
let mut counter = 0;
loop {
if self.max_repeat.is_some() && counter >= self.max_repeat.unwrap() {
return Ok(());
}
if let Err(err) = run_cmd(self.cmd, &self.env) {
2017-01-29 16:39:48 -06:00
println!("{:?}", err)
}
thread::sleep(self.poll_duration);
if self.max_repeat.is_some() {
counter += 1
}
2017-01-29 16:39:48 -06:00
}
}
}