Skip to content

Commit bccdb5d

Browse files
committed
update the json atomically
1 parent 6c7ca52 commit bccdb5d

2 files changed

Lines changed: 10 additions & 10 deletions

File tree

tasktiger/redis_scripts.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,15 @@
261261
"""
262262

263263
# KEYS = { scheduled_zset_key, task_data_key }
264-
# ARGV = { score, member, serialized_task_data }
264+
# ARGV = { score, member }
265+
# score is used both as the new ZSET score and as the new scheduled_at value.
265266
UPDATE_SCHEDULED_TIME = """
266267
local score = redis.call('zscore', KEYS[1], ARGV[2])
267268
if score then
268269
redis.call('zadd', KEYS[1], ARGV[1], ARGV[2])
269-
redis.call('set', KEYS[2], ARGV[3])
270+
local data = cjson.decode(redis.call('get', KEYS[2]))
271+
data['scheduled_at'] = tonumber(ARGV[1])
272+
redis.call('set', KEYS[2], cjson.encode(data))
270273
return 1
271274
else
272275
return 0
@@ -587,16 +590,15 @@ def update_scheduled_time(
587590
task_data_key: str,
588591
score: float,
589592
member: str,
590-
serialized_task_data: str,
591593
) -> bool:
592594
"""
593-
Atomically updates a task's scheduled time in the ZSET and its _data
594-
blob. Returns True if the task was found and updated, False if the task
595-
was not present in the ZSET.
595+
Atomically updates a task's scheduled time in the ZSET and patches
596+
scheduled_at in the _data blob. Returns True if the task was found and
597+
updated, False if the task was not present in the ZSET.
596598
"""
597599
result = self._update_scheduled_time(
598600
keys=[scheduled_zset_key, task_data_key],
599-
args=[score, member, serialized_task_data],
601+
args=[score, member],
600602
)
601603
return bool(result)
602604

tasktiger/task.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,11 @@ def update_scheduled_time(
431431
ts = get_timestamp(when)
432432
assert ts
433433

434-
new_data = {**self._data, "scheduled_at": ts}
435434
found = tiger.scripts.update_scheduled_time(
436435
scheduled_zset_key=tiger._key(SCHEDULED, self.queue),
437436
task_data_key=tiger._key("task", self.id),
438437
score=ts,
439438
member=self.id,
440-
serialized_task_data=json.dumps(new_data),
441439
)
442440
if not found:
443441
raise TaskNotFound(
@@ -446,7 +444,7 @@ def update_scheduled_time(
446444
)
447445
)
448446

449-
self._data = new_data
447+
self._data["scheduled_at"] = ts
450448
self._ts = ts
451449

452450
def __repr__(self) -> str:

0 commit comments

Comments
 (0)