diff --git a/index-scheduler/src/lib.rs b/index-scheduler/src/lib.rs index 0f0ecf52a..c1b2deb27 100644 --- a/index-scheduler/src/lib.rs +++ b/index-scheduler/src/lib.rs @@ -794,6 +794,12 @@ impl IndexScheduler { Ok(res) } + // Return true if there is at least one task that is processing. + pub fn is_task_processing(&self) -> Result { + let processing_tasks = self.processing_tasks.read().unwrap().processing.len(); + Ok(processing_tasks > 0) + } + /// 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 { @@ -1808,6 +1814,24 @@ 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![]); + + let (file0, documents_count0) = sample_documents(&index_scheduler, 0, 0); + file0.persist().unwrap(); + + let _ = index_scheduler + .register(replace_document_import_task("catto", None, 0, documents_count0)) + .unwrap(); + snapshot!(snapshot_index_scheduler(&index_scheduler), name: "registered_a_task"); + + handle.advance_till([Start, BatchCreated, InsideProcessBatch]); + snapshot!(snapshot_index_scheduler(&index_scheduler), name: "initial_task_processing"); + + assert_eq!(index_scheduler.is_task_processing().unwrap(), true); + } + /// 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/meilisearch/src/metrics.rs b/meilisearch/src/metrics.rs index 79332f360..009c3e472 100644 --- a/meilisearch/src/metrics.rs +++ b/meilisearch/src/metrics.rs @@ -50,4 +50,14 @@ 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 a7d41e33e..f6ee3291b 100644 --- a/meilisearch/src/routes/metrics.rs +++ b/meilisearch/src/routes/metrics.rs @@ -48,6 +48,9 @@ pub async fn get_metrics( } } + crate::metrics::MEILISEARCH_LAST_UPDATE.set(response.last_update.unwrap().unix_timestamp() as i64); + crate::metrics::MEILISEARCH_IS_INDEXING.set(index_scheduler.is_task_processing().unwrap() as i64); + let encoder = TextEncoder::new(); let mut buffer = vec![]; encoder.encode(&prometheus::gather(), &mut buffer).expect("Failed to encode metrics");