Scheduled worker doesnt run all the code

Hi there! So I have a worker that runs every 1 min, and do an insert to a supabase db. But for some reason the logs start logging just before de insert is called, and the db is empty without any data. It seems there is no error cause the worker is strill running every min

Here is the code:

export default {
	async scheduled(event: any, env: any, ctx: any) {
		console.log('schedule');
		test();
	},
};

const test = async (): Promise<any> => {
	console.log('Start');
	const supabase = createClient(
		'url',
		'apkiKey
	);
	console.log({ supabase });
	try {
		console.log('Prev Insert'); // After this, nothing is logged
		const response = await supabase.from('positions').insert({
			id: `mercado_librex-${'TEST'}`,
			name: 'TEST',
			url: 'TEST',
			type: 'TEST',
		});

		console.log({ response });
	} catch (e) {
		console.log('Error on insert:', { e });
	}
};

You need to await your test function since it’s async. If you don’t, the promise gets cancelled/it doesn’t understand it’s still working.

export default {
	async scheduled(event: any, env: any, ctx: any) {
		console.log('schedule');
		await test(); // Add await here
	},
};
1 Like