Skip to content

Commit

Permalink
[integritee-service/teeracle] renamed interval to periodic/period to …
Browse files Browse the repository at this point in the history
…align with the substrate terms used in the scheduler pallet.
  • Loading branch information
clangenb committed Jun 7, 2023
1 parent 68e37f7 commit b8e23ea
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions service/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![cfg_attr(test, feature(assert_matches))]

#[cfg(feature = "teeracle")]
use crate::teeracle::{schedule_teeracle_reregistration_thread, start_interval_market_update};
use crate::teeracle::{schedule_teeracle_reregistration_thread, start_periodic_market_update};

#[cfg(not(feature = "dcap"))]
use crate::utils::check_files;
Expand Down Expand Up @@ -483,7 +483,7 @@ fn start_worker<E, T, D, InitializationHandler, WorkerModeProvider>(
run_config.reregister_teeracle_interval(),
);

start_interval_market_update(
start_periodic_market_update(
&node_api,
run_config.teeracle_update_interval(),
enclave.as_ref(),
Expand Down
19 changes: 10 additions & 9 deletions service/src/teeracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use crate::{error::ServiceResult, teeracle::interval_scheduling::schedule_on_repeating_intervals};
use crate::{error::ServiceResult, teeracle::schedule_periodic::schedule_periodic};
use codec::{Decode, Encode};
use itp_enclave_api::teeracle_api::TeeracleApi;
use itp_node_api::api_client::ParentchainApi;
Expand All @@ -28,7 +28,7 @@ use substrate_api_client::{SubmitAndWatch, XtStatus};
use teeracle_metrics::{increment_number_of_request_failures, set_extrinsics_inclusion_success};
use tokio::runtime::Handle;

pub(crate) mod interval_scheduling;
pub(crate) mod schedule_periodic;
pub(crate) mod teeracle_metrics;

pub(crate) fn schedule_teeracle_reregistration_thread(
Expand All @@ -40,7 +40,7 @@ pub(crate) fn schedule_teeracle_reregistration_thread(
std::thread::Builder::new()
.name("teeracle_reregistration_thread".to_owned())
.spawn(move || {
schedule_on_repeating_intervals(
schedule_periodic(
|| {
println!("Reregistering the teeracle.");
if let Some(block_hash) = send_register_xt() {
Expand All @@ -58,11 +58,12 @@ pub(crate) fn schedule_teeracle_reregistration_thread(
.unwrap();
}

/// Send extrinsic to chain according to the market data update interval in the settings
/// with the current market data (for now only exchange rate).
pub(crate) fn start_interval_market_update<E: TeeracleApi>(
/// Executes a periodic teeracle data update and sends the new data to the parentchain.
///
/// Note: Puts the current thread to sleep for `period`.
pub(crate) fn start_periodic_market_update<E: TeeracleApi>(
api: &ParentchainApi,
interval: Duration,
period: Duration,
enclave_api: &E,
tokio_handle: &Handle,
) {
Expand All @@ -84,8 +85,8 @@ pub(crate) fn start_interval_market_update<E: TeeracleApi>(
info!("Teeracle will update now");
updates_to_run();

info!("Schedule teeracle updates every {:?}", interval);
schedule_on_repeating_intervals(updates_to_run, interval);
info!("Schedule teeracle updates every {:?}", period);
schedule_periodic(updates_to_run, period);
}

fn execute_oracle_update<F>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ use std::{
time::{Duration, Instant},
};

/// Schedules a task on perpetually looping intervals.
/// Schedules a periodic task in the current thread.
///
/// In case the task takes longer than is scheduled by the interval duration,
/// the interval timing will drift. The task is responsible for
/// ensuring it does not use up more time than is scheduled.
pub(super) fn schedule_on_repeating_intervals<T>(task: T, interval_duration: Duration)
pub(super) fn schedule_periodic<T>(task: T, period: Duration)
where
T: Fn(),
{
let mut interval_start = Instant::now();
loop {
let elapsed = interval_start.elapsed();

if elapsed >= interval_duration {
if elapsed >= period {
// update interval time
interval_start = Instant::now();
task();
} else {
// sleep for the rest of the interval
let sleep_time = interval_duration - elapsed;
let sleep_time = period - elapsed;
thread::sleep(sleep_time);
}
}
Expand Down

0 comments on commit b8e23ea

Please sign in to comment.