Skip to content

Commit 5e7922f

Browse files
committed
fix: correctly declare keys in redis scripts
1 parent 15279da commit 5e7922f

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/lib/redis/scripts.ts

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,51 +25,66 @@ export type RedisScripts = {
2525
};
2626

2727
const recordResult: RecordResultScript = defineScript({
28-
NUMBER_OF_KEYS: 4,
28+
NUMBER_OF_KEYS: 2,
2929
SCRIPT: `
30-
local measurementId = KEYS[1]
31-
local testId = KEYS[2]
32-
local data = KEYS[3]
33-
local date = KEYS[4]
34-
local key = 'gp:m:'..measurementId..':results'
35-
local awaitingKey = 'gp:m:'..measurementId..':probes_awaiting'
30+
local keyMeasurementResults = KEYS[1]
31+
local keyMeasurementAwaiting = KEYS[2]
32+
local testId = ARGV[1]
33+
local data = ARGV[2]
34+
local date = ARGV[3]
3635
37-
local probesAwaiting = redis.call('GET', awaitingKey)
36+
local probesAwaiting = redis.call('GET', keyMeasurementAwaiting)
3837
if not probesAwaiting then
3938
return
4039
end
4140
42-
probesAwaiting = redis.call('DECR', awaitingKey)
43-
redis.call('JSON.SET', key, '$.results['..testId..'].result', data)
44-
redis.call('JSON.SET', key, '$.updatedAt', date)
41+
probesAwaiting = redis.call('DECR', keyMeasurementAwaiting)
42+
redis.call('JSON.SET', keyMeasurementResults, '$.results['..testId..'].result', data)
43+
redis.call('JSON.SET', keyMeasurementResults, '$.updatedAt', date)
4544
4645
if probesAwaiting ~= 0 then
4746
return
4847
end
4948
50-
return redis.call('JSON.GET', key)
49+
return redis.call('JSON.GET', keyMeasurementResults)
5150
`,
5251
transformArguments (measurementId, testId, data) {
53-
return [ measurementId, testId, JSON.stringify(data), `"${new Date().toISOString()}"` ];
52+
return [
53+
// keys
54+
`gp:m:${measurementId}:results`,
55+
`gp:m:${measurementId}:probes_awaiting`,
56+
// values
57+
testId,
58+
JSON.stringify(data),
59+
`"${new Date().toISOString()}"`,
60+
];
5461
},
5562
transformReply (reply) {
5663
return JSON.parse(reply) as MeasurementRecord | null;
5764
},
5865
});
5966

6067
const markFinished: MarkFinishedScript = defineScript({
61-
NUMBER_OF_KEYS: 1,
68+
NUMBER_OF_KEYS: 3,
6269
SCRIPT: `
63-
local measurementId = KEYS[1]
64-
local key = 'gp:m:'..measurementId..':results'
65-
local awaitingKey = 'gp:m:'..measurementId..':probes_awaiting'
70+
local keyInProgress = KEYS[1]
71+
local keyMeasurementResults = KEYS[2]
72+
local keyMeasurementAwaiting = KEYS[3]
73+
local measurementId = ARGV[1]
6674
67-
redis.call('HDEL', 'gp:in-progress', measurementId)
68-
redis.call('DEL', awaitingKey)
69-
redis.call('JSON.SET', key, '$.status', '"finished"')
75+
redis.call('HDEL', keyInProgress, measurementId)
76+
redis.call('DEL', keyMeasurementAwaiting)
77+
redis.call('JSON.SET', keyMeasurementResults, '$.status', '"finished"')
7078
`,
7179
transformArguments (measurementId) {
72-
return [ measurementId ];
80+
return [
81+
// keys
82+
'gp:in-progress',
83+
`gp:m:${measurementId}:results`,
84+
`gp:m:${measurementId}:probes_awaiting`,
85+
// values
86+
measurementId,
87+
];
7388
},
7489
transformReply () {
7590
return null;

0 commit comments

Comments
 (0)