Skip to content

Fix notification retry bug and align message format #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/notify/amqp/ampq.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ export class AmqpService implements NotifyProvider, OnModuleDestroy {
const { url: _, exchange, routingKey, responseTimeout, reply, ...options } = settings;

const channel = await this.getChannel(args.service);
const message = args.message ?? createMessage(process, args.service);
const message =
args.message ?? {
process: process.id,
...createMessage(process, args.service),
etag: etag(process),
};

options.appId ??= DEFAULT_APPID;
options.messageId = etag(process);
Expand Down
23 changes: 23 additions & 0 deletions src/notify/notify.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,27 @@ describe('NotifyService', () => {
it('should be defined', () => {
expect(service).toBeDefined();
});

describe('onRetry', () => {
const process = { current: { notify: [{ service: 'a' }, { service: 'b' }] } } as any;

it('should notify all services if no list provided', async () => {
const notifySpy = jest.spyOn(service as any, 'notify').mockResolvedValue(undefined);

await service.onRetry({ process });

expect(notifySpy).toHaveBeenCalledTimes(2);
expect(notifySpy).toHaveBeenCalledWith(process, { service: 'a' });
expect(notifySpy).toHaveBeenCalledWith(process, { service: 'b' });
});

it('should notify only listed services', async () => {
const notifySpy = jest.spyOn(service as any, 'notify').mockResolvedValue(undefined);

await service.onRetry({ process, services: ['b'] });

expect(notifySpy).toHaveBeenCalledTimes(1);
expect(notifySpy).toHaveBeenCalledWith(process, { service: 'b' });
});
});
});
2 changes: 1 addition & 1 deletion src/notify/notify.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class NotifyService implements NotifyProvider {
@OnEvent('process.retry')
async onRetry({ process, services }: { process: Process; services?: string[] }) {
for (const args of process.current.notify) {
if (!services && !services.includes(args.service)) continue;
if (services && !services.includes(args.service)) continue;
await this.notify(process, args);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/notify/webhook/webhook.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@/common/config/config.service';
import { createMessage, Notify, Process } from '@letsflow/core/process';
import { createMessage, etag, Notify, Process } from '@letsflow/core/process';
import { NotifyProvider } from '@/notify/notify-provider.interface';

interface WebhookSettings extends RequestInit {
Expand All @@ -27,7 +27,12 @@ export class WebhookService implements NotifyProvider {
throw new Error(`Service '${args.service}' is missing url setting`);
}

const message = args.message ?? createMessage(process, args.service);
const message =
args.message ?? {
process: process.id,
...createMessage(process, args.service),
etag: etag(process),
};
settings.headers ??= {};
settings.headers['Content-Type'] ??= typeof message === 'string' ? 'text/plain' : 'application/json';

Expand Down
9 changes: 7 additions & 2 deletions src/notify/zeromq/zeromq.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Inject, Injectable, OnModuleDestroy } from '@nestjs/common';
import { createMessage, Notify, Process } from '@letsflow/core/process';
import { createMessage, etag, Notify, Process } from '@letsflow/core/process';
import { Push, Reply, SocketOptions } from 'zeromq';
import { ConfigService } from '@/common/config/config.service';
import { NotifyProvider } from '../notify-provider.interface';
Expand Down Expand Up @@ -41,7 +41,12 @@ export class ZeromqService implements NotifyProvider, OnModuleDestroy {

async notify(process: Process, args: Notify): Promise<any> {
const socket = this.getSocket(args.service);
const message = args.message ?? createMessage(process, args.service);
const message =
args.message ?? {
process: process.id,
...createMessage(process, args.service),
etag: etag(process),
};

await socket.send(typeof message === 'string' ? message : JSON.stringify(message));

Expand Down
3 changes: 0 additions & 3 deletions src/process/process.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,6 @@ describe('ProcessService', () => {
expect(process.current.key).toEqual('initial');
expect(process.current.actions).toEqual([
{
$schema: 'https://schemas.letsflow.io/v1.0/action',
actor: ['actor'],
description: '',
title: 'complete',
Expand Down Expand Up @@ -307,7 +306,6 @@ describe('ProcessService', () => {
key: 'initial',
actions: {
complete: {
$schema: 'https://schemas.letsflow.io/v1.0/action',
actor: ['actor'],
description: '',
title: 'complete',
Expand Down Expand Up @@ -355,7 +353,6 @@ describe('ProcessService', () => {
key: 'initial',
actions: {
complete: {
$schema: 'https://schemas.letsflow.io/v1.0/action',
actor: ['actor'],
description: '',
title: 'complete',
Expand Down