tulpje-framework 0.17.0

Multi-purpose discord bot & framework
Documentation
use std::{future::Future, pin::Pin};

use async_cron_scheduler::cron::Schedule;
use chrono::{DateTime, Utc};

use crate::Error;
use crate::context::TaskContext;

pub(crate) type TaskFunc<T> =
    fn(TaskContext<T>) -> Pin<Box<dyn Future<Output = Result<(), Error>> + Send>>;

#[derive(Clone)]
pub struct TaskHandler<T: Clone + Send + Sync> {
    pub module: String,
    pub name: String,
    pub cron: Schedule,
    pub func: TaskFunc<T>,
}

impl<T: Clone + Send + Sync> TaskHandler<T> {
    #[tracing::instrument(name="task-handler", skip_all, fields(
        module=self.module,
        name=self.name
    ))]
    pub async fn run(&self, ctx: TaskContext<T>) -> Result<(), Error> {
        // can add more handling/parsing/etc here in the future
        (self.func)(ctx).await
    }

    pub fn next_run(&self) -> Option<DateTime<Utc>> {
        self.cron.upcoming(Utc).next()
    }
}