feat: setup logique pour remonter les cronjob si l'application redemarre
This commit is contained in:
parent
ecadf7a90d
commit
9c21b0dfdf
@ -58,8 +58,14 @@ pub async fn start(ctx: Context<'_>) -> Result<(), Error> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (concour, success) =
|
let (concour, success) = match start_concour(
|
||||||
match start_concour(guild.get(), ctx.channel_id().get(), &mut cron_schedule).await {
|
guild.get(),
|
||||||
|
ctx.channel_id().get(),
|
||||||
|
&mut cron_schedule,
|
||||||
|
ctx.http(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(concour) => {
|
Ok(concour) => {
|
||||||
if concour.is_none() {
|
if concour.is_none() {
|
||||||
(
|
(
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use poise::serenity_prelude::Http;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tracing::{info, instrument};
|
use tracing::{info, instrument};
|
||||||
|
|
||||||
@ -16,11 +17,12 @@ pub enum StartConcourError {
|
|||||||
UnknownError(String),
|
UnknownError(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "info", skip(cron_scheduler))]
|
#[instrument(level = "info", skip(cron_scheduler, http))]
|
||||||
pub async fn start_concour(
|
pub async fn start_concour(
|
||||||
server_id: u64,
|
server_id: u64,
|
||||||
channel_id: u64,
|
channel_id: u64,
|
||||||
cron_scheduler: &mut ScheduleJob,
|
cron_scheduler: &mut ScheduleJob,
|
||||||
|
http: &Http,
|
||||||
) -> Result<Option<Concour>, StartConcourError> {
|
) -> Result<Option<Concour>, StartConcourError> {
|
||||||
let concour = match Concour::find_by_server_id_channel_id(&server_id, &channel_id).await {
|
let concour = match Concour::find_by_server_id_channel_id(&server_id, &channel_id).await {
|
||||||
Ok(list_concour) => list_concour,
|
Ok(list_concour) => list_concour,
|
||||||
@ -51,7 +53,7 @@ pub async fn start_concour(
|
|||||||
return Err(StartConcourError::AlreadyOnGoing);
|
return Err(StartConcourError::AlreadyOnGoing);
|
||||||
}
|
}
|
||||||
match cron_scheduler
|
match cron_scheduler
|
||||||
.add_concour_cron_job(server_id, channel_id, "*/1 * * * * *".to_string())
|
.add_concour_cron_job(server_id, channel_id, "*/1 * * * * *".to_string(), http)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
|
@ -181,7 +181,6 @@ impl Concour {
|
|||||||
};
|
};
|
||||||
Ok(Some(concour))
|
Ok(Some(concour))
|
||||||
}
|
}
|
||||||
#[instrument(level = "info")]
|
|
||||||
pub async fn find_by_server_id(server_id: &u64) -> Result<Vec<Concour>, surrealdb::Error> {
|
pub async fn find_by_server_id(server_id: &u64) -> Result<Vec<Concour>, surrealdb::Error> {
|
||||||
let sql = format!("SELECT * FROM {} WHERE server_id = {}", CONCOUR, server_id);
|
let sql = format!("SELECT * FROM {} WHERE server_id = {}", CONCOUR, server_id);
|
||||||
let mut results = match DB.query(&sql).await {
|
let mut results = match DB.query(&sql).await {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
use poise::serenity_prelude::Http;
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::{self, Display},
|
fmt::{self, Display},
|
||||||
@ -8,6 +9,8 @@ use tokio_cron_scheduler::{Job, JobScheduler};
|
|||||||
use tracing::{error, info, instrument};
|
use tracing::{error, info, instrument};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::db::concour::{Concour, ConcourStatus};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum StopScheduleJob {
|
pub enum StopScheduleJob {
|
||||||
JobNotFound,
|
JobNotFound,
|
||||||
@ -58,6 +61,37 @@ impl ScheduleJob {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument(skip(self, http), level = "info")]
|
||||||
|
pub async fn load_all_concour_cron_job(&mut self, http: &Http) -> Result<(), bool> {
|
||||||
|
let concours = match Concour::find_by_status(&ConcourStatus::OnGoing).await {
|
||||||
|
Ok(concour) => concour,
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error getting concour: {:?}", e);
|
||||||
|
return Err(true);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for concour in concours {
|
||||||
|
match self
|
||||||
|
.add_concour_cron_job(
|
||||||
|
concour.server_id,
|
||||||
|
concour.channel_id,
|
||||||
|
"0 0 17 * * *".to_string(),
|
||||||
|
http,
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
info!("Concour cron job added");
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
error!("Error adding concour cron job");
|
||||||
|
return Err(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), level = "info")]
|
#[instrument(skip(self), level = "info")]
|
||||||
pub async fn stop_cron_scheduler(&mut self) -> &mut Self {
|
pub async fn stop_cron_scheduler(&mut self) -> &mut Self {
|
||||||
let job_id = self.job_id.write().await;
|
let job_id = self.job_id.write().await;
|
||||||
@ -90,15 +124,27 @@ impl ScheduleJob {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), level = "info")]
|
#[instrument(skip(self, _http), level = "info")]
|
||||||
pub async fn add_concour_cron_job(
|
pub async fn add_concour_cron_job(
|
||||||
&mut self,
|
&mut self,
|
||||||
server_id: u64,
|
server_id: u64,
|
||||||
channel_id: u64,
|
channel_id: u64,
|
||||||
cron_expression: String,
|
cron_expression: String,
|
||||||
|
_http: &Http,
|
||||||
) -> Result<Uuid, ()> {
|
) -> Result<Uuid, ()> {
|
||||||
let job = match Job::new_async(cron_expression.as_str(), |uuid, _l| {
|
let job = match Job::new_async(cron_expression.as_str(), |uuid, _l| {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
|
// Send the message to announce the end of the concour
|
||||||
|
// Get concour data
|
||||||
|
// Get All message since the announcement
|
||||||
|
// filter out the bot's message
|
||||||
|
// count the number of reactions per message
|
||||||
|
// get the user comment with the highest reaction
|
||||||
|
// announce the winner
|
||||||
|
// Give the winner the role reward
|
||||||
|
// update concour with the winner and increment the index
|
||||||
|
// Announce the next concour
|
||||||
|
// Or not if there is no more keyword
|
||||||
info!("Cron job fired: {:?}", uuid);
|
info!("Cron job fired: {:?}", uuid);
|
||||||
})
|
})
|
||||||
}) {
|
}) {
|
||||||
|
12
src/main.rs
12
src/main.rs
@ -38,6 +38,18 @@ async fn main() -> std::io::Result<()> {
|
|||||||
let (tx_bot, rx_bot) = oneshot::channel();
|
let (tx_bot, rx_bot) = oneshot::channel();
|
||||||
let http = start_bot(config.clone(), rx_bot, cron_scheduler.clone()).await;
|
let http = start_bot(config.clone(), rx_bot, cron_scheduler.clone()).await;
|
||||||
info!("API Server started on port {}", port);
|
info!("API Server started on port {}", port);
|
||||||
|
match cron_scheduler
|
||||||
|
.load_all_concour_cron_job(&http.clone())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(_) => {
|
||||||
|
info!("All concour cron job loaded");
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
println!("Error loading all concour cron job");
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut openapi = ApiDocs::openapi();
|
let mut openapi = ApiDocs::openapi();
|
||||||
openapi.info.title = format!("{} Api", config.bot_name.clone());
|
openapi.info.title = format!("{} Api", config.bot_name.clone());
|
||||||
openapi.info.version = env!("CARGO_PKG_VERSION").to_string();
|
openapi.info.version = env!("CARGO_PKG_VERSION").to_string();
|
||||||
|
Loading…
Reference in New Issue
Block a user