Skip to content

Commit

Permalink
Fix async tasks race condition for selecting task to be executed
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
otaviojacobi committed Oct 27, 2024
1 parent 6e5e079 commit 582c7cb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/tasks/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export class Worker {
return;
}
await sbvrUtils.db.transaction(async (tx) => {
const result = await sbvrUtils.db.executeSql(
const result = await tx.executeSql(
`SELECT ${selectColumns}
FROM task AS t
WHERE
Expand Down
62 changes: 31 additions & 31 deletions test/08-tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -475,39 +475,39 @@ describe('08 task tests', function () {
attempt_count: 1,
});
});
});

it('should only run task once with multiple pine instances', async () => {
const { body: device } = await pineTest
.post({
apiPrefix: 'example/',
resource: 'device',
body: {
name: randomUUID(),
type: randomUUID(),
count: 0,
},
})
.expect(201);
it('should only run task once with multiple pine instances', async () => {
const { body: device } = await pineTest
.post({
apiPrefix: 'example/',
resource: 'device',
body: {
name: randomUUID(),
type: randomUUID(),
count: 0,
},
})
.expect(201);

for (let j = 0; j < 30; j++) {
await createTask(pineTest, apikey, {
is_executed_by__handler: 'increment_device_count',
is_executed_with__parameter_set: {
deviceId: device.id,
},
});
for (let j = 0; j < 30; j++) {
await createTask(pineTest, apikey, {
is_executed_by__handler: 'increment_device_count',
is_executed_with__parameter_set: {
deviceId: device.id,
},
});

await waitFor(async () => {
const { body: deviceAfter } = await pineTest
.get({
apiPrefix: 'example/',
resource: 'device',
id: device.id,
})
.expect(200);
return deviceAfter?.count === j + 1;
});
}
await waitFor(async () => {
const { body: deviceAfter } = await pineTest
.get({
apiPrefix: 'example/',
resource: 'device',
id: device.id,
})
.expect(200);
return deviceAfter?.count === j + 1;
});
}
});
});
});
1 change: 0 additions & 1 deletion test/lib/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { expect } from 'chai';
import net, { type AddressInfo } from 'node:net';

export function assertExists(v: unknown): asserts v is NonNullable<typeof v> {
expect(v).to.exist;
Expand Down

0 comments on commit 582c7cb

Please sign in to comment.