@@ -139,17 +139,23 @@ async function compileWorkflow(tenancy: Tenancy, workflowId: string): Promise<Re
139139 }
140140 const workflow = tenancy . config . workflows . availableWorkflows [ workflowId ] ;
141141 const res = await timeout ( async ( ) => {
142+ console . log ( `Compiling workflow ${ workflowId } ...` ) ;
142143 const compiledCodeResult = await compileWorkflowSource ( workflow . tsSource ) ;
143144 if ( compiledCodeResult . status === "error" ) {
144145 return Result . error ( { compileError : `Failed to compile workflow: ${ compiledCodeResult . error } ` } ) ;
145146 }
146147
148+ console . log ( `Compiled workflow source for ${ workflowId } , running compilation trigger...` , { compiledCodeResult } ) ;
149+
147150 const compileTriggerResult = await triggerWorkflowRaw ( tenancy , compiledCodeResult . data , {
148151 type : "compile" ,
149152 } ) ;
150153 if ( compileTriggerResult . status === "error" ) {
151154 return Result . error ( { compileError : `Failed to initialize workflow: ${ compileTriggerResult . error } ` } ) ;
152155 }
156+
157+ console . log ( `Compilation trigger result:` , { compileTriggerResult } ) ;
158+
153159 const compileTriggerOutputResult = compileTriggerResult . data ;
154160 if ( typeof compileTriggerOutputResult !== "object" || ! compileTriggerOutputResult || ! ( "triggerOutput" in compileTriggerOutputResult ) ) {
155161 captureError ( "workflows-compile-trigger-output" , new StackAssertionError ( `Failed to parse compile trigger output` , { compileTriggerOutputResult } ) ) ;
@@ -161,6 +167,8 @@ async function compileWorkflow(tenancy: Tenancy, workflowId: string): Promise<Re
161167 return Result . error ( { compileError : `Failed to parse compile trigger output, should be array of strings` } ) ;
162168 }
163169
170+ console . log ( `Workflow ${ workflowId } compiled successfully, returning result...` , { registeredTriggers, compiledCodeResult } ) ;
171+
164172 return Result . ok ( {
165173 compiledCode : compiledCodeResult . data ,
166174 registeredTriggers : registeredTriggers ,
@@ -369,46 +377,48 @@ async function compileAndGetEnabledWorkflows(tenancy: Tenancy): Promise<Map<stri
369377}
370378
371379async function triggerWorkflowRaw ( tenancy : Tenancy , compiledWorkflowCode : string , trigger : WorkflowTrigger ) : Promise < Result < unknown , string > > {
372- const workflowToken = generateSecureRandomString ( ) ;
373- const workflowTriggerToken = await globalPrismaClient . workflowTriggerToken . create ( {
374- data : {
375- expiresAt : new Date ( Date . now ( ) + 1000 * 35 ) ,
376- tenancyId : tenancy . id ,
377- tokenHash : await hashWorkflowTriggerToken ( workflowToken ) ,
378- } ,
379- } ) ;
380+ return await traceSpan ( { description : `triggerWorkflowRaw ${ trigger . type } ` } , async ( ) => {
381+ const workflowToken = generateSecureRandomString ( ) ;
382+ const workflowTriggerToken = await globalPrismaClient . workflowTriggerToken . create ( {
383+ data : {
384+ expiresAt : new Date ( Date . now ( ) + 1000 * 35 ) ,
385+ tenancyId : tenancy . id ,
386+ tokenHash : await hashWorkflowTriggerToken ( workflowToken ) ,
387+ } ,
388+ } ) ;
380389
381- const tokenRefreshInterval = setInterval ( ( ) => {
382- runAsynchronously ( async ( ) => {
383- await globalPrismaClient . workflowTriggerToken . update ( {
384- where : {
385- tenancyId_id : {
386- tenancyId : tenancy . id ,
387- id : workflowTriggerToken . id ,
390+ const tokenRefreshInterval = setInterval ( ( ) => {
391+ runAsynchronously ( async ( ) => {
392+ await globalPrismaClient . workflowTriggerToken . update ( {
393+ where : {
394+ tenancyId_id : {
395+ tenancyId : tenancy . id ,
396+ id : workflowTriggerToken . id ,
397+ } ,
388398 } ,
399+ data : { expiresAt : new Date ( Date . now ( ) + 1000 * 35 ) } ,
400+ } ) ;
401+ } ) ;
402+ } , 10_000 ) ;
403+
404+ try {
405+ const freestyle = new Freestyle ( ) ;
406+ const freestyleRes = await freestyle . executeScript ( compiledWorkflowCode , {
407+ envVars : {
408+ STACK_WORKFLOW_TRIGGER_DATA : JSON . stringify ( trigger ) ,
409+ NEXT_PUBLIC_STACK_PROJECT_ID : tenancy . project . id ,
410+ NEXT_PUBLIC_STACK_API_URL : getEnvVariable ( "NEXT_PUBLIC_STACK_API_URL" ) . replace ( "http://localhost" , "http://host.docker.internal" ) , // the replace is a hardcoded hack for the Freestyle mock server
411+ NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY : "<placeholder publishable client key; the actual auth happens with the workflow token>" ,
412+ STACK_SECRET_SERVER_KEY : "<placeholder secret server key; the actual auth happens with the workflow token>" ,
413+ STACK_WORKFLOW_TOKEN_SECRET : workflowToken ,
389414 } ,
390- data : { expiresAt : new Date ( Date . now ( ) + 1000 * 35 ) } ,
415+ nodeModules : Object . fromEntries ( Object . entries ( externalPackages ) . map ( ( [ packageName , version ] ) => [ packageName , version ] ) ) ,
391416 } ) ;
392- } ) ;
393- } , 10_000 ) ;
394-
395- try {
396- const freestyle = new Freestyle ( ) ;
397- const freestyleRes = await freestyle . executeScript ( compiledWorkflowCode , {
398- envVars : {
399- STACK_WORKFLOW_TRIGGER_DATA : JSON . stringify ( trigger ) ,
400- NEXT_PUBLIC_STACK_PROJECT_ID : tenancy . project . id ,
401- NEXT_PUBLIC_STACK_API_URL : getEnvVariable ( "NEXT_PUBLIC_STACK_API_URL" ) . replace ( "http://localhost" , "http://host.docker.internal" ) , // the replace is a hardcoded hack for the Freestyle mock server
402- NEXT_PUBLIC_STACK_PUBLISHABLE_CLIENT_KEY : "<placeholder publishable client key; the actual auth happens with the workflow token>" ,
403- STACK_SECRET_SERVER_KEY : "<placeholder secret server key; the actual auth happens with the workflow token>" ,
404- STACK_WORKFLOW_TOKEN_SECRET : workflowToken ,
405- } ,
406- nodeModules : Object . fromEntries ( Object . entries ( externalPackages ) . map ( ( [ packageName , version ] ) => [ packageName , version ] ) ) ,
407- } ) ;
408- return Result . map ( freestyleRes , ( data ) => data . result ) ;
409- } finally {
410- clearInterval ( tokenRefreshInterval ) ;
411- }
417+ return Result . map ( freestyleRes , ( data ) => data . result ) ;
418+ } finally {
419+ clearInterval ( tokenRefreshInterval ) ;
420+ }
421+ } ) ;
412422}
413423
414424async function createScheduledTrigger ( tenancy : Tenancy , workflowId : string , trigger : WorkflowTrigger , scheduledAt : Date ) {
0 commit comments