N
Novuβ€’5mo ago
Axel Dickson

Running npx novu@latest dev with Express executes all workflows on startup with empty payload

Hi - I'm new here and Novu is the solution we wanted for a long time at Cling Systems πŸ”‹πŸ‡ΈπŸ‡ͺ. I followed the Express quickstart quide for @novu/framework. I run my express server on port 5003 and register my workflows like this:
import { serve } from '@novu/framework/express';
import { workflows } from '@cling/notifications';

app.use(express.json());
app.use('/api/novu', serve({ workflows: workflows }));
import { serve } from '@novu/framework/express';
import { workflows } from '@cling/notifications';

app.use(express.json());
app.use('/api/novu', serve({ workflows: workflows }));
Here are two dummy workflows I try to setup:
import { logger } from '@cling/logger';
import { workflow } from '@novu/framework';
import { z } from 'zod';

const workflow1 = workflow(
'first-workflow',
async ({ payload, step }) => {
logger.info(`Running first workflow with payload: ${payload}`);

await step.delay('dummy-delay', () => {
return {
amount: 10,
unit: 'seconds',
};
});
},
{
payloadSchema: z.object({ firstName: z.string() }),
},
);

const workflow2 = workflow(
'second-workflow',
async ({ payload, step }) => {
logger.info(`Running second workflow with payload: ${payload}`);

await step.delay('dummy-delay', () => {
return {
amount: 10,
unit: 'seconds',
};
});
},
{
payloadSchema: z.object({ firstName: z.string() }),
},
);

export const workflows = [workflow1, workflow2];
import { logger } from '@cling/logger';
import { workflow } from '@novu/framework';
import { z } from 'zod';

const workflow1 = workflow(
'first-workflow',
async ({ payload, step }) => {
logger.info(`Running first workflow with payload: ${payload}`);

await step.delay('dummy-delay', () => {
return {
amount: 10,
unit: 'seconds',
};
});
},
{
payloadSchema: z.object({ firstName: z.string() }),
},
);

const workflow2 = workflow(
'second-workflow',
async ({ payload, step }) => {
logger.info(`Running second workflow with payload: ${payload}`);

await step.delay('dummy-delay', () => {
return {
amount: 10,
unit: 'seconds',
};
});
},
{
payloadSchema: z.object({ firstName: z.string() }),
},
);

export const workflows = [workflow1, workflow2];
When I start the local novu studio like with command npx novu@latest dev --port 5003 the both the workflows run. See attached image of logs where the logger output is displayed before the console output of discovered workflows. Is this how it's intended to work? If so what is the recommended way to handle more complex workflows where we trigger different database queries and other service calls? Any help appreciated πŸ™
No description
2 Replies
Dima Grossman
Dima Grossmanβ€’5mo ago
Yes, this is the intedned behaviour, as a discover command builds the workflow structure. The discover phase however doesn't run the "step" handler functions. Running any code in your database needs to happen in a step handler, or if it's reused than in a custom step that returns the value. Doing any side effect actions not inside a step is not recommended
Axel Dickson
Axel DicksonOPβ€’5mo ago
That makes sense Dima, thanks a lot 🌟

Did you find this page helpful?