11use std:: sync:: Arc ;
22
3- use bellatrix:: { Bellatrix , orion_client:: OrionBuildRequest } ;
3+ use bellatrix:: {
4+ Bellatrix ,
5+ orion_client:: { BuildInfo , OrionBuildRequest } ,
6+ } ;
47use common:: errors:: MegaError ;
58use jupiter:: storage:: Storage ;
69
@@ -17,91 +20,77 @@ impl BuildDispatcher {
1720 Self { storage, bellatrix }
1821 }
1922
20- /// Dispatch a build trigger.
21- ///
22- /// This method:
23- /// 1. Persists the trigger to the database
24- /// 2. Sends the build request to Bellatrix/Orion asynchronously
25- ///
26- /// Returns the ID of the created trigger record.
2723 pub async fn dispatch ( & self , trigger : BuildTrigger ) -> Result < i64 , MegaError > {
2824 let trigger_payload = serde_json:: to_value ( & trigger. payload ) . map_err ( |e| {
2925 tracing:: error!( "Failed to serialize payload: {}" , e) ;
3026 MegaError :: Other ( format ! ( "Failed to serialize payload: {}" , e) )
3127 } ) ?;
3228
29+ // Determine task_id based on whether build system is enabled
30+ let task_id: Option < uuid:: Uuid > = if self . bellatrix . enable_build ( ) {
31+ // Extract data from payload using pattern matching
32+ let ( cl_link, repo, builds_json, cl_id) = match & trigger. payload {
33+ BuildTriggerPayload :: GitPush ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
34+ BuildTriggerPayload :: Manual ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
35+ BuildTriggerPayload :: Retry ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
36+ BuildTriggerPayload :: Webhook ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
37+ BuildTriggerPayload :: Schedule ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
38+ BuildTriggerPayload :: WebEdit ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
39+ } ;
40+
41+ let builds: Vec < SerializableBuildInfo > = serde_json:: from_value ( builds_json. clone ( ) )
42+ . map_err ( |e| {
43+ tracing:: error!( "Failed to deserialize builds from payload: {}" , e) ;
44+ MegaError :: Other ( format ! ( "Failed to deserialize builds from payload: {}" , e) )
45+ } ) ?;
46+
47+ let bellatrix_builds: Vec < BuildInfo > = builds
48+ . into_iter ( )
49+ . map ( |info| BuildInfo {
50+ changes : info. changes . into_iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ,
51+ } )
52+ . collect ( ) ;
53+
54+ let req = OrionBuildRequest {
55+ cl_link : cl_link. to_string ( ) ,
56+ mount_path : repo. to_string ( ) ,
57+ cl : cl_id. unwrap_or ( 0 ) ,
58+ builds : bellatrix_builds,
59+ } ;
60+
61+ let task_id_str = self . bellatrix . on_post_receive ( req) . await . map_err ( |e| {
62+ tracing:: error!( "Failed to dispatch build to Bellatrix: {}" , e) ;
63+ MegaError :: Other ( format ! ( "Failed to dispatch build to Bellatrix: {}" , e) )
64+ } ) ?;
65+
66+ let task_uuid = uuid:: Uuid :: parse_str ( & task_id_str) . map_err ( |e| {
67+ tracing:: error!( "Invalid task_id format '{}': {}" , task_id_str, e) ;
68+ MegaError :: Other ( format ! ( "Invalid task_id format '{}': {}" , task_id_str, e) )
69+ } ) ?;
70+
71+ Some ( task_uuid)
72+ } else {
73+ tracing:: info!( "BuildDispatcher: Build system disabled, skipping Orion call" ) ;
74+ None
75+ } ;
76+
77+ // Insert trigger record with task_id (complete record in one operation)
3378 let db_record = self
3479 . storage
3580 . build_trigger_storage ( )
3681 . insert (
3782 trigger. trigger_type . to_string ( ) ,
3883 trigger. trigger_source . to_string ( ) ,
3984 trigger_payload,
85+ task_id,
4086 )
4187 . await ?;
4288
43- // Persist to database
44- tracing:: info!( "BuildDispatcher: Persisted trigger ID: {}" , db_record. id) ;
45-
46- if !self . bellatrix . enable_build ( ) {
47- tracing:: info!( "BuildDispatcher: Completed (build system disabled)" ) ;
48- return Ok ( db_record. id ) ;
49- }
50-
51- // Extract data from payload using pattern matching
52- let ( cl_link, repo, builds_json, cl_id) = match & trigger. payload {
53- BuildTriggerPayload :: GitPush ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
54- BuildTriggerPayload :: Manual ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
55- BuildTriggerPayload :: Retry ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
56- BuildTriggerPayload :: Webhook ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
57- BuildTriggerPayload :: Schedule ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
58- BuildTriggerPayload :: WebEdit ( p) => ( & p. cl_link , & p. repo , & p. builds , p. cl_id ) ,
59- } ;
60-
61- let builds: Vec < SerializableBuildInfo > = serde_json:: from_value ( builds_json. clone ( ) )
62- . map_err ( |e| {
63- tracing:: error!( "Failed to deserialize builds from payload: {}" , e) ;
64- MegaError :: Other ( format ! ( "Failed to deserialize builds from payload: {}" , e) )
65- } ) ?;
66-
67- let bellatrix_builds: Vec < bellatrix:: orion_client:: BuildInfo > = builds
68- . into_iter ( )
69- . enumerate ( )
70- . map ( |( idx, info) | {
71- tracing:: debug!( " Build [{}]: {} change(s)" , idx + 1 , info. changes. len( ) ) ;
72- bellatrix:: orion_client:: BuildInfo {
73- changes : info. changes . into_iter ( ) . map ( |s| s. into ( ) ) . collect ( ) ,
74- }
75- } )
76- . collect ( ) ;
77-
78- let req = OrionBuildRequest {
79- cl_link : cl_link. to_string ( ) ,
80- mount_path : repo. to_string ( ) ,
81- cl : cl_id. unwrap_or ( 0 ) ,
82- builds : bellatrix_builds,
83- } ;
84-
85- // Dispatch asynchronously
86- let bellatrix = self . bellatrix . clone ( ) ;
87- let trigger_id = db_record. id ;
88- tokio:: spawn ( async move {
89- match bellatrix. on_post_receive ( req) . await {
90- Ok ( _) => {
91- tracing:: info!(
92- "BuildDispatcher: Build request sent to Bellatrix (Trigger ID: {})" ,
93- trigger_id
94- ) ;
95- }
96- Err ( err) => {
97- tracing:: error!(
98- "BuildDispatcher: Failed to dispatch build (Trigger ID: {}): {}" ,
99- trigger_id,
100- err
101- ) ;
102- }
103- }
104- } ) ;
89+ tracing:: info!(
90+ "BuildDispatcher: Trigger persisted (ID: {}, Task ID: {:?})" ,
91+ db_record. id,
92+ task_id
93+ ) ;
10594
10695 Ok ( db_record. id )
10796 }
0 commit comments