1use std::{
2 collections::{HashMap, HashSet},
3 env,
4 time::Duration,
5};
6
7use chrono::Utc;
8use dotenv::dotenv;
9use sqlx::{PgConnection, PgPool};
10use url::Url;
11use uuid::Uuid;
12
13use crate::setup_tracing;
14
15use headless_lms_chatbot::{
16 azure_blob_storage::AzureBlobClient,
17 azure_datasources::{create_azure_datasource, does_azure_datasource_exist},
18 azure_search_index::{create_search_index, does_search_index_exist},
19 azure_search_indexer::{
20 check_search_indexer_status, create_search_indexer, does_search_indexer_exist,
21 run_search_indexer_now,
22 },
23 azure_skillset::{create_skillset, does_skillset_exist},
24 content_cleaner::convert_material_blocks_to_markdown_with_llm,
25};
26use headless_lms_models::{
27 chapters::DatabaseChapter,
28 page_history::PageHistory,
29 pages::{Page, PageVisibility},
30};
31use headless_lms_utils::{
32 ApplicationConfiguration,
33 document_schema_processor::{GutenbergBlock, remove_sensitive_attributes},
34 url_encoding::url_encode,
35};
36
37const SYNC_INTERVAL_SECS: u64 = 10;
38const PRINT_STILL_RUNNING_MESSAGE_TICKS_THRESHOLD: u32 = 60;
39const FAILURE_COOLDOWN_SECS: i64 = 300;
40const MAX_CONSECUTIVE_FAILURES: i32 = 5;
41
42pub async fn main() -> anyhow::Result<()> {
43 initialize_environment()?;
44 let config = initialize_configuration().await?;
45 if config.app_configuration.azure_configuration.is_none() {
46 warn!("Azure configuration not provided. Not running chatbot syncer.");
47 loop {
49 tokio::time::sleep(Duration::from_secs(u64::MAX)).await;
50 }
51 }
52 if config.app_configuration.test_chatbot {
53 warn!(
54 "Using mock azure configuration, this must be a test/dev environment. Not running chatbot syncer."
55 );
56 loop {
58 tokio::time::sleep(Duration::from_secs(u64::MAX)).await;
59 }
60 }
61
62 let db_pool = initialize_database_pool(&config.database_url).await?;
63 let mut conn = db_pool.acquire().await?;
64 let blob_client = initialize_blob_client(&config).await?;
65
66 let mut interval = tokio::time::interval(Duration::from_secs(SYNC_INTERVAL_SECS));
67 let mut ticks = 0;
68
69 info!("Starting chatbot syncer.");
70
71 loop {
72 interval.tick().await;
73 ticks += 1;
74
75 if ticks >= PRINT_STILL_RUNNING_MESSAGE_TICKS_THRESHOLD {
76 ticks = 0;
77 info!("Still syncing for chatbot.");
78 }
79 if let Err(e) = sync_pages(&mut conn, &config, &blob_client).await {
80 error!("Error during synchronization: {:?}", e);
81 }
82 }
83}
84
85fn initialize_environment() -> anyhow::Result<()> {
86 unsafe { env::set_var("RUST_LOG", "info,actix_web=info,sqlx=warn") };
88 dotenv().ok();
89 setup_tracing()?;
90 Ok(())
91}
92
93struct SyncerConfig {
94 database_url: String,
95 name: String,
96 app_configuration: ApplicationConfiguration,
97}
98
99async fn initialize_configuration() -> anyhow::Result<SyncerConfig> {
100 let database_url = env::var("DATABASE_URL")
101 .unwrap_or_else(|_| "postgres://localhost/headless_lms_dev".to_string());
102
103 let base_url = Url::parse(&env::var("BASE_URL").expect("BASE_URL must be defined"))
104 .expect("BASE_URL must be a valid URL");
105
106 let name = base_url
107 .host_str()
108 .expect("BASE_URL must have a host")
109 .replace(".", "-");
110
111 let app_configuration = ApplicationConfiguration::try_from_env()?;
112
113 Ok(SyncerConfig {
114 database_url,
115 name,
116 app_configuration,
117 })
118}
119
120async fn initialize_database_pool(database_url: &str) -> anyhow::Result<PgPool> {
122 PgPool::connect(database_url).await.map_err(|e| {
123 anyhow::anyhow!(
124 "Failed to connect to the database at {}: {:?}",
125 database_url,
126 e
127 )
128 })
129}
130
131async fn initialize_blob_client(config: &SyncerConfig) -> anyhow::Result<AzureBlobClient> {
133 let blob_client = AzureBlobClient::new(&config.app_configuration, &config.name).await?;
134 blob_client.ensure_container_exists().await?;
135 Ok(blob_client)
136}
137
138async fn sync_pages(
140 conn: &mut PgConnection,
141 config: &SyncerConfig,
142 blob_client: &AzureBlobClient,
143) -> anyhow::Result<()> {
144 let base_url = Url::parse(&config.app_configuration.base_url)?;
145 let chatbot_configs =
146 headless_lms_models::chatbot_configurations::get_for_azure_search_maintenance(conn).await?;
147
148 let course_ids: Vec<Uuid> = chatbot_configs
149 .iter()
150 .map(|config| config.course_id)
151 .collect::<HashSet<_>>()
152 .into_iter()
153 .collect();
154
155 let sync_statuses =
156 headless_lms_models::chatbot_page_sync_statuses::ensure_sync_statuses_exist(
157 conn,
158 &course_ids,
159 )
160 .await?;
161
162 let latest_histories =
163 headless_lms_models::page_history::get_latest_history_entries_for_pages_by_course_ids(
164 conn,
165 &course_ids,
166 )
167 .await?;
168
169 let shared_index_name = config.name.clone();
170 ensure_search_index_exists(
171 &shared_index_name,
172 &config.app_configuration,
173 &blob_client.container_name,
174 )
175 .await?;
176
177 if !check_search_indexer_status(&shared_index_name, &config.app_configuration).await? {
178 warn!("Search indexer is not ready to index. Skipping synchronization.");
179 return Ok(());
180 }
181
182 let mut any_changes = false;
183
184 for (course_id, statuses) in sync_statuses.iter() {
185 let page_ids: Vec<Uuid> = statuses.iter().map(|s| s.page_id).collect();
186 let public_pages_set: HashSet<Uuid> =
187 headless_lms_models::pages::get_by_ids_and_visibility(
188 conn,
189 &page_ids,
190 PageVisibility::Public,
191 )
192 .await?
193 .into_iter()
194 .map(|p| p.id)
195 .collect();
196
197 let outdated_statuses: Vec<_> = statuses
198 .iter()
199 .filter(|status| {
200 if !public_pages_set.contains(&status.page_id) {
201 return false;
202 }
203
204 let is_outdated = latest_histories
205 .get(&status.page_id)
206 .is_some_and(|history| status.synced_page_revision_id != Some(history.id));
207
208 if !is_outdated {
209 return false;
210 }
211
212 if status.consecutive_failures >= MAX_CONSECUTIVE_FAILURES {
213 debug!(
214 "Skipping page {} due to permanent failure ({} consecutive failures). Manual intervention required.",
215 status.page_id, status.consecutive_failures
216 );
217 return false;
218 }
219
220 if let Some(error_msg) = &status.error_message {
221 if !error_msg.is_empty() {
222 let error_age_seconds = (Utc::now() - status.updated_at).num_seconds();
223 if error_age_seconds < FAILURE_COOLDOWN_SECS {
224 debug!(
225 "Skipping page {} due to recent failure ({} seconds ago, {} consecutive failures): {}",
226 status.page_id, error_age_seconds, status.consecutive_failures, error_msg
227 );
228 return false;
229 }
230 }
231 }
232
233 true
234 })
235 .collect();
236
237 if outdated_statuses.is_empty() {
238 continue;
239 }
240
241 any_changes = true;
242 info!(
243 "Syncing {} pages for course id: {}.",
244 outdated_statuses.len(),
245 course_id
246 );
247 for status in &outdated_statuses {
248 info!(
249 "Page id: {}, synced page revision id: {:?}.",
250 status.page_id, status.synced_page_revision_id
251 );
252 }
253
254 let page_ids: Vec<Uuid> = outdated_statuses.iter().map(|s| s.page_id).collect();
255 let pages = headless_lms_models::pages::get_by_ids_and_visibility(
256 conn,
257 &page_ids,
258 PageVisibility::Public,
259 )
260 .await?;
261
262 if !pages.is_empty() {
263 sync_pages_batch(
264 conn,
265 &pages,
266 blob_client,
267 &base_url,
268 &config.app_configuration,
269 &latest_histories,
270 )
271 .await?;
272 } else {
273 info!("No pages to sync for course id: {}.", course_id);
274 }
275
276 let hidden_page_ids: Vec<Uuid> = statuses
277 .iter()
278 .filter(|status| {
279 !public_pages_set.contains(&status.page_id)
280 && status.synced_page_revision_id.is_some()
281 })
282 .map(|s| s.page_id)
283 .collect();
284
285 if !hidden_page_ids.is_empty() {
286 info!(
287 "Clearing sync statuses for {} hidden pages: {:?}",
288 hidden_page_ids.len(),
289 hidden_page_ids
290 );
291 headless_lms_models::chatbot_page_sync_statuses::clear_sync_statuses(
292 conn,
293 &hidden_page_ids,
294 )
295 .await?;
296 }
297
298 delete_old_files(conn, *course_id, blob_client).await?;
299 }
300
301 if any_changes {
302 run_search_indexer_now(&shared_index_name, &config.app_configuration).await?;
303 info!("New files have been synced and the search indexer has been started.");
304 }
305
306 Ok(())
307}
308
309async fn ensure_search_index_exists(
311 name: &str,
312 app_config: &ApplicationConfiguration,
313 container_name: &str,
314) -> anyhow::Result<()> {
315 if !does_search_index_exist(name, app_config).await? {
316 create_search_index(name.to_owned(), app_config).await?;
317 }
318 if !does_skillset_exist(name, app_config).await? {
319 create_skillset(name, name, app_config).await?;
320 }
321 if !does_azure_datasource_exist(name, app_config).await? {
322 create_azure_datasource(name, container_name, app_config).await?;
323 }
324 if !does_search_indexer_exist(name, app_config).await? {
325 create_search_indexer(name, name, name, name, app_config).await?;
326 }
327
328 Ok(())
329}
330
331async fn sync_pages_batch(
333 conn: &mut PgConnection,
334 pages: &[Page],
335 blob_client: &AzureBlobClient,
336 base_url: &Url,
337 app_config: &ApplicationConfiguration,
338 latest_histories: &HashMap<Uuid, PageHistory>,
339) -> anyhow::Result<()> {
340 let course_id = pages
341 .first()
342 .ok_or_else(|| anyhow::anyhow!("No pages to sync."))?
343 .course_id
344 .ok_or_else(|| anyhow::anyhow!("The first page does not belong to any course."))?;
345
346 let course = headless_lms_models::courses::get_course(conn, course_id).await?;
347 let organization =
348 headless_lms_models::organizations::get_organization(conn, course.organization_id).await?;
349
350 let mut base_url = base_url.clone();
351 base_url.set_path(&format!(
352 "/org/{}/courses/{}",
353 organization.slug, course.slug
354 ));
355
356 let mut allowed_file_paths = Vec::new();
357
358 for page in pages {
359 info!("Syncing page id: {}.", page.id);
360
361 let mut page_url = base_url.clone();
362 page_url.set_path(&format!("{}{}", base_url.path(), page.url_path));
363
364 let parsed_content: Vec<GutenbergBlock> = serde_json::from_value(page.content.clone())?;
365 let sanitized_blocks = remove_sensitive_attributes(parsed_content);
366
367 let content_to_upload = match convert_material_blocks_to_markdown_with_llm(
368 &sanitized_blocks,
369 app_config,
370 )
371 .await
372 {
373 Ok(markdown) => {
374 info!("Successfully cleaned content for page {}", page.id);
375 if markdown.trim().is_empty() {
377 warn!(
378 "Markdown is empty for page {}. Generating fallback content with a fake heading.",
379 page.id
380 );
381 format!("# {}", page.title)
382 } else {
383 markdown
384 }
385 }
386 Err(e) => {
387 let error_msg = format!("Sync failed: LLM processing error: {}", e);
388 warn!(
389 "Failed to clean content with LLM for page {}: {}. Using serialized sanitized content instead.",
390 page.id, error_msg
391 );
392 if let Err(db_err) =
393 headless_lms_models::chatbot_page_sync_statuses::set_page_sync_error(
394 conn, page.id, &error_msg,
395 )
396 .await
397 {
398 warn!(
399 "Failed to record sync error for page {}: {:?}",
400 page.id, db_err
401 );
402 }
403 serde_json::to_string(&sanitized_blocks)?
405 }
406 };
407
408 let blob_path = generate_blob_path(page)?;
409 let chapter: Option<DatabaseChapter> = if page.chapter_id.is_some() {
410 match headless_lms_models::chapters::get_chapter_by_page_id(conn, page.id).await {
411 Ok(c) => Some(c),
412 Err(e) => {
413 debug!("Chapter lookup failed for page {}: {}", page.id, e);
414 None
415 }
416 }
417 } else {
418 None
419 };
420
421 allowed_file_paths.push(blob_path.clone());
422 let mut metadata = HashMap::new();
423 metadata.insert("url".to_string(), url_encode(page_url.as_ref()));
427 metadata.insert("title".to_string(), url_encode(&page.title));
428 metadata.insert(
429 "course_id".to_string(),
430 page.course_id.unwrap_or(Uuid::nil()).to_string().into(),
431 );
432 metadata.insert(
433 "language".to_string(),
434 course.language_code.to_string().into(),
435 );
436 metadata.insert("filepath".to_string(), blob_path.clone().into());
437 if let Some(c) = chapter {
438 metadata.insert(
439 "chunk_context".to_string(),
440 url_encode(&format!(
441 "This chunk is a snippet from page {} from chapter {}: {} of the course {}.",
442 page.title, c.chapter_number, c.name, course.name,
443 )),
444 );
445 } else {
446 metadata.insert(
447 "chunk_context".to_string(),
448 url_encode(&format!(
449 "This chunk is a snippet from page {} of the course {}.",
450 page.title, course.name,
451 )),
452 );
453 }
454
455 if let Err(e) = blob_client
456 .upload_file(&blob_path, content_to_upload.as_bytes(), Some(metadata))
457 .await
458 {
459 let error_msg = format!("Sync failed: Upload error: {}", e);
460 warn!("Failed to upload file {}: {:?}", blob_path, e);
461 if let Err(db_err) =
462 headless_lms_models::chatbot_page_sync_statuses::set_page_sync_error(
463 conn, page.id, &error_msg,
464 )
465 .await
466 {
467 warn!(
468 "Failed to record upload error for page {}: {:?}",
469 page.id, db_err
470 );
471 }
472 } else if let Some(history_id) = latest_histories.get(&page.id) {
473 let mut page_revision_map = HashMap::new();
474 page_revision_map.insert(page.id, history_id.id);
475 if let Err(e) =
476 headless_lms_models::chatbot_page_sync_statuses::update_page_revision_ids(
477 conn,
478 page_revision_map,
479 )
480 .await
481 {
482 let error_msg = format!("Sync failed: Status update error: {}", e);
483 warn!("Failed to update sync status for page {}: {:?}", page.id, e);
484 if let Err(db_err) =
485 headless_lms_models::chatbot_page_sync_statuses::set_page_sync_error(
486 conn, page.id, &error_msg,
487 )
488 .await
489 {
490 warn!(
491 "Failed to record status update error for page {}: {:?}",
492 page.id, db_err
493 );
494 }
495 }
496 }
497 }
498
499 Ok(())
500}
501
502fn generate_blob_path(page: &Page) -> anyhow::Result<String> {
504 let course_id = page
505 .course_id
506 .ok_or_else(|| anyhow::anyhow!("Page {} does not belong to any course.", page.id))?;
507
508 Ok(format!("courses/{}/pages/{}.md", course_id, page.id))
509}
510
511async fn delete_old_files(
514 conn: &mut PgConnection,
515 course_id: Uuid,
516 blob_client: &AzureBlobClient,
517) -> anyhow::Result<()> {
518 let mut courses_prefix = "courses/".to_string();
519 courses_prefix.push_str(&course_id.to_string());
520 let existing_files = blob_client.list_files_with_prefix(&courses_prefix).await?;
521
522 let pages = headless_lms_models::pages::get_all_by_course_id_and_visibility(
523 conn,
524 course_id,
525 PageVisibility::Public,
526 )
527 .await?;
528
529 let allowed_paths: HashSet<String> = pages
530 .iter()
531 .filter_map(|page| generate_blob_path(page).ok())
532 .collect();
533
534 for file in existing_files {
535 if !allowed_paths.contains(&file) {
536 info!("Deleting obsolete file: {}", file);
537 blob_client.delete_file(&file).await?;
538 }
539 }
540
541 Ok(())
542}