2025-05-01 20:14:48 +00:00
|
|
|
// _opt/messageQueue-example.js
|
|
|
|
|
import { onMessageQueueEvent } from './pbUtils.js';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Example module that listens for 'test' messages in the message_queue collection.
|
|
|
|
|
*/
|
|
|
|
|
export const init = async (client, config) => {
|
2025-05-02 16:45:36 +00:00
|
|
|
client.logger.info('[module:messageQueueExample] Initializing Message Queue Example module');
|
2025-05-01 20:14:48 +00:00
|
|
|
onMessageQueueEvent(client, async (action, record) => {
|
|
|
|
|
// Only process newly created records
|
|
|
|
|
if (action !== 'create') return;
|
|
|
|
|
// Only process messages meant for this client
|
|
|
|
|
if (record.destination !== client.config.id) return;
|
|
|
|
|
// Only handle test dataType
|
|
|
|
|
if (record.dataType !== 'test') return;
|
|
|
|
|
|
|
|
|
|
// At this point we have a test message for us
|
2025-05-02 16:45:36 +00:00
|
|
|
client.logger.info('[module:messageQueueExample] Test message received');
|
2025-05-01 20:14:48 +00:00
|
|
|
|
|
|
|
|
// Delete the processed message from the queue
|
|
|
|
|
try {
|
|
|
|
|
await client.pb.deleteMessageQueue(record.id);
|
2025-05-02 16:45:36 +00:00
|
|
|
client.logger.debug(`[module:messageQueueExample] Deleted message_queue record ${record.id}`);
|
2025-05-01 20:14:48 +00:00
|
|
|
} catch (err) {
|
2025-05-02 16:45:36 +00:00
|
|
|
client.logger.error(`[module:messageQueueExample] Failed to delete message_queue record ${record.id}: ${err.message}`);
|
2025-05-01 20:14:48 +00:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|