Skip to content
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

Fix taskWorker #553

Merged
merged 7 commits into from
Jun 1, 2022
Merged
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
6 changes: 6 additions & 0 deletions .changeset/big-wolves-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@sw-internal/e2e-realtime-api': patch
'@signalwire/realtime-api': patch
---

Fix `task.received` handler on the Task namespace.
33 changes: 27 additions & 6 deletions internal/e2e-realtime-api/src/task.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { createTestRunner } from './utils'
const handler = () => {
return new Promise<number>(async (resolve, reject) => {
const context = 'task-e2e'
const jobPayload = {
const firstPayload = {
id: Date.now(),
item: 'foo',
item: 'first',
}
const lastPayload = {
id: Date.now(),
item: 'last',
}

const client = new Task.Client({
Expand All @@ -16,20 +20,37 @@ const handler = () => {
contexts: [context],
})

let counter = 0

client.on('task.received', (payload) => {
if (payload.id === jobPayload.id && payload.item === 'foo') {
if (payload.id === firstPayload.id && payload.item === 'first') {
counter++
} else if (payload.id === lastPayload.id && payload.item === 'last') {
counter++
} else {
console.error('Invalid payload on `task.received`', payload)
return reject(4)
}

if (counter === 2) {
return resolve(0)
}
console.error('Invalid payload on `task.received`', payload)
return reject(4)
})

await Task.send({
host: process.env.RELAY_HOST as string,
project: process.env.RELAY_PROJECT as string,
token: process.env.RELAY_TOKEN as string,
context,
message: jobPayload,
message: firstPayload,
})

await Task.send({
host: process.env.RELAY_HOST as string,
project: process.env.RELAY_PROJECT as string,
token: process.env.RELAY_TOKEN as string,
context,
message: lastPayload,
})
})
}
Expand Down
27 changes: 17 additions & 10 deletions packages/realtime-api/src/task/workers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
getLogger,
sagaEffects,
SagaIterator,
SDKWorker,
Expand All @@ -7,17 +8,23 @@ import {
import type { Task } from './Task'

export const taskWorker: SDKWorker<Task> = function* (options): SagaIterator {
getLogger().trace('taskWorker started')
const { channels } = options
const { swEventChannel, pubSubChannel } = channels
const action = yield sagaEffects.take(
swEventChannel,
(action: SDKActions) => {
return action.type === 'queuing.relay.tasks'
}
)

yield sagaEffects.put(pubSubChannel, {
type: 'task.received',
payload: action.payload.message,
})
while (true) {
const action = yield sagaEffects.take(
swEventChannel,
(action: SDKActions) => {
return action.type === 'queuing.relay.tasks'
}
)

yield sagaEffects.put(pubSubChannel, {
type: 'task.received',
payload: action.payload.message,
})
}

getLogger().trace('taskWorker ended')
}