Created
March 23, 2016 11:20
-
-
Save cdump/cef0dfa76d438a99268c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_part(file_hash, part_number, quality, timeout) | |
-- Trying to select the requested fragment | |
local t = v.fragments_space.index.main:select(file_hash, part_number, quality) | |
-- If it exists — returning immediately | |
if t ~= nil then | |
return t | |
end | |
-- Creating a key to identify the requested fragment, and an ipc channel, then writing it | |
-- in a table in order to receive a “task completed” notification later | |
local table_key = msgpack.encode{file_hash, part_number, quality} | |
local ch = fiber.channel(1) | |
v.ctable[table_key] = ch | |
-- Creating a record about the fragment with the status “want to be converted” | |
v.fragments_space:insert(file_hash, part_number, quality, STATUS_QUEUED) | |
-- If we have idle workers, let’s notify them about the new task | |
if s.waitch:has_readers() then | |
s.waitch:put(true, 0) | |
end | |
-- Waiting for task completion for no more than “timeout” seconds | |
local body = ch:get(timeout) | |
if body ~= nil then | |
if body == false then | |
-- Couldn’t complete the task — return error | |
return box.tuple.new{RET_ERROR} | |
else | |
-- Task completed, selecting and returning the result | |
return v.fragments_space.index.main:select{file_hash, part_number, quality} | |
end | |
else | |
-- Timeout error is returned | |
return box.tuple.new{RET_ERROR} | |
end | |
end | |
local table_key = msgpack.encode{file_hash, part_number, quality} | |
v.ctable[table_key]:put(true, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment