tcrm_task/tasks/tokio/
state.rs

1use std::{sync::Arc, time::SystemTime};
2
3use crate::tasks::{
4    state::{ProcessState, TaskState},
5    tokio::{context::TaskExecutorContext, executor::TaskExecutor},
6};
7
8impl TaskExecutor {
9    /// Updates the task state and records appropriate timestamps.
10    ///
11    /// Sets the new state in the shared context and updates the corresponding
12    /// timestamp (running_at for Running state, finished_at for Finished state).
13    ///
14    /// # Arguments
15    ///
16    /// * `shared_context` - The shared task execution context
17    /// * `new_state` - The new state to set
18    ///
19    /// # Returns
20    ///
21    /// The timestamp when the state change occurred
22    pub(crate) fn update_state(
23        shared_context: &Arc<TaskExecutorContext>,
24        new_state: TaskState,
25    ) -> SystemTime {
26        shared_context.set_task_state(new_state);
27
28        let time = match new_state {
29            TaskState::Running => {
30                shared_context.set_process_state(ProcessState::Running);
31                shared_context.set_running_at()
32            }
33            TaskState::Finished => {
34                shared_context.set_process_state(ProcessState::Stopped);
35                shared_context.set_finished_at()
36            }
37            _ => SystemTime::now(),
38        };
39        time
40    }
41}