48 lines
1.5 KiB
Lua
48 lines
1.5 KiB
Lua
--[[
|
|
Submits a Rhai script to a distributed worker and returns the task ID.
|
|
|
|
Since BLPOP cannot block inside Lua scripts (they execute atomically),
|
|
this script only submits the task and returns the task ID. The client
|
|
must then use BLPOP separately to wait for the result.
|
|
|
|
ARGV[1] (string): The target circle name (e.g., "default").
|
|
ARGV[2] (string): The Rhai script content to execute.
|
|
|
|
Returns:
|
|
- A JSON string containing the task ID and reply queue name.
|
|
]]
|
|
|
|
-- 1. Argument Validation
|
|
local circle_name = ARGV[1]
|
|
if not circle_name or circle_name == '' then
|
|
return cjson.encode({error = "ARGV[1] 'circle_name' is required."})
|
|
end
|
|
|
|
local rhai_script = ARGV[2]
|
|
if not rhai_script or rhai_script == '' then
|
|
return cjson.encode({error = "ARGV[2] 'rhai_script' is required."})
|
|
end
|
|
|
|
-- 2. Initialization
|
|
local task_details_prefix = "rhai_task_details:"
|
|
local tasks_queue_prefix = "rhai_tasks:"
|
|
local reply_queue_prefix = "rhai_reply:"
|
|
|
|
local task_id = redis.sha1hex(rhai_script .. redis.call('TIME')[1] .. redis.call('TIME')[2] .. math.random())
|
|
local reply_queue_key = reply_queue_prefix .. task_id
|
|
local task_details_key = task_details_prefix .. task_id
|
|
local task_queue_key = tasks_queue_prefix .. circle_name
|
|
|
|
-- 3. Task Creation & Queuing
|
|
redis.call('HSET', task_details_key,
|
|
'script', rhai_script,
|
|
'status', 'pending',
|
|
'replyToQueue', reply_queue_key
|
|
)
|
|
redis.call('LPUSH', task_queue_key, task_id)
|
|
|
|
-- 4. Return task information for client to wait on
|
|
return cjson.encode({
|
|
task_id = task_id,
|
|
reply_queue = reply_queue_key
|
|
}) |