async function processScheduledEvents() {
    logger.debug('processing scheduled events');
    const client = await pgPool.connect();
    try {
        await client.query('BEGIN');
        const triggers = await client.query('SELECT t.id, t.pipeline_id FROM pipeline_trigger t WHERE t.state=$1 ORDER BY t.created_at FOR UPDATE SKIP LOCKED LIMIT 1', ['QUEUED']);

        if (triggers.rows.length === 0) {
            await new Promise(resolve => setTimeout(resolve, DELAY_IF_NOEVENTS));
            await client.query('COMMIT');
            return;
        }

        const trigger = triggers.rows[0];
        const timeNow = new Date().toISOString();

        while (true) {
            const result = await client.query('SELECT q.* FROM outbound_queue q INNER JOIN pipeline p ON q.pipeline_id = p.id WHERE q.pipeline_id = $1 and p.batch_process = true and q.state = $2 AND p.active = true AND q.created_at < $3 ORDER BY q.created_at FOR UPDATE SKIP LOCKED LIMIT $4', [trigger.pipeline_id, 'QUEUED', timeNow, QUERY_BATCH_SIZE]);
            if (result.rows.length === 0) {
                break;
            }
            try {
                await processEvents(client, result.rows, true); //batch mode flag to true
            } catch (err) {
                logger.error("Error processing events via pipeline trigger", err);
            }
        }
        await client.query('UPDATE pipeline_trigger SET state= $1 WHERE id  = $2', ['COMPLETED', trigger.id]);
        await client.query('COMMIT');
    } catch (err) {
        logger.error(err);
        await client.query('ROLLBACK');
        await new Promise(resolve => setTimeout(resolve, 5000));
    } finally {
        client.release();
    }
}
Method to process scheduled events in batch mode