diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index 092851edd..1e7ba6e21 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -807,6 +807,11 @@ impl IndexScheduler { Ok(res) } + // Return true if there is at least one task that is processing. + pub fn is_task_processing(&self) -> Result { + Ok(!self.processing_tasks.read().unwrap().processing.is_empty()) + } + /// Return true iff there is at least one task associated with this index /// that is processing. pub fn is_index_processing(&self, index: &str) -> Result { @@ -1833,6 +1838,17 @@ mod tests { snapshot!(snapshot_index_scheduler(&index_scheduler), name: "registered_the_third_task"); } + #[test] + fn test_task_is_processing() { + let (index_scheduler, mut handle) = IndexScheduler::test(true, vec![]); + + index_scheduler.register(index_creation_task("index_a", "id")).unwrap(); + snapshot!(snapshot_index_scheduler(&index_scheduler), name: "registered_a_task"); + + handle.advance_till([Start, BatchCreated]); + assert!(index_scheduler.is_task_processing().unwrap()); + } + /// We send a lot of tasks but notify the tasks scheduler only once as /// we send them very fast, we must make sure that they are all processed. #[test] diff --git a/index-scheduler/src/snapshots/lib.rs/test_task_is_processing/registered_a_task.snap b/index-scheduler/src/snapshots/lib.rs/test_task_is_processing/registered_a_task.snap new file mode 100644 index 000000000..f17bfe38f --- /dev/null +++ b/index-scheduler/src/snapshots/lib.rs/test_task_is_processing/registered_a_task.snap @@ -0,0 +1,36 @@ +--- +source: index-scheduler/src/lib.rs +--- +### Autobatching Enabled = true +### Processing Tasks: +[] +---------------------------------------------------------------------- +### All Tasks: +0 {uid: 0, status: enqueued, details: { primary_key: Some("id") }, kind: IndexCreation { index_uid: "index_a", primary_key: Some("id") }} +---------------------------------------------------------------------- +### Status: +enqueued [0,] +---------------------------------------------------------------------- +### Kind: +"indexCreation" [0,] +---------------------------------------------------------------------- +### Index Tasks: +index_a [0,] +---------------------------------------------------------------------- +### Index Mapper: + +---------------------------------------------------------------------- +### Canceled By: + +---------------------------------------------------------------------- +### Enqueued At: +[timestamp] [0,] +---------------------------------------------------------------------- +### Started At: +---------------------------------------------------------------------- +### Finished At: +---------------------------------------------------------------------- +### File Store: + +---------------------------------------------------------------------- + diff --git a/meilisearch/src/metrics.rs b/meilisearch/src/metrics.rs index b3cbb4576..bfe704979 100644 --- a/meilisearch/src/metrics.rs +++ b/meilisearch/src/metrics.rs @@ -50,4 +50,10 @@ lazy_static! { &["kind", "value"] ) .expect("Can't create a metric"); + pub static ref MEILISEARCH_LAST_UPDATE: IntGauge = + register_int_gauge!(opts!("meilisearch_last_update", "Meilisearch Last Update")) + .expect("Can't create a metric"); + pub static ref MEILISEARCH_IS_INDEXING: IntGauge = + register_int_gauge!(opts!("meilisearch_is_indexing", "Meilisearch Is Indexing")) + .expect("Can't create a metric"); } diff --git a/meilisearch/src/routes/metrics.rs b/meilisearch/src/routes/metrics.rs index 492fec417..af1f2b536 100644 --- a/meilisearch/src/routes/metrics.rs +++ b/meilisearch/src/routes/metrics.rs @@ -49,6 +49,11 @@ pub async fn get_metrics( } } + if let Some(last_update) = response.last_update { + crate::metrics::MEILISEARCH_LAST_UPDATE.set(last_update.unix_timestamp()); + } + crate::metrics::MEILISEARCH_IS_INDEXING.set(index_scheduler.is_task_processing()? as i64); + let encoder = TextEncoder::new(); let mut buffer = vec![]; encoder.encode(&prometheus::gather(), &mut buffer).expect("Failed to encode metrics");