Skip to content

Commit ebf37f9

Browse files
committed
usability: add driver API check
In addition to the core queue drivers, customer drivers are exists. In the commit a check for a driver API implementation was added. Now, the consumer will be informed about the missing methods in the driver implementation. Closes #126
1 parent c844e06 commit ebf37f9

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

queue/abstract.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ local function tube_release_all_tasks(tube)
7575
return
7676
end
7777

78+
-- Custom drivers may not have an implementation of
79+
-- the "tasks_by_state" method.
80+
if tube.raw["tasks_by_state"] == nil then
81+
return
82+
end
83+
7884
log.info(prefix .. 'releasing all taken task (may take a while)')
7985
local released = 0
8086
for _, task in tube.raw:tasks_by_state(state.TAKEN) do
@@ -308,6 +314,45 @@ end
308314
-- methods
309315
local method = {}
310316

317+
-- Original list of required driver methods.
318+
local required_driver_methods = {
319+
'normalize_task',
320+
'put',
321+
'take',
322+
'delete',
323+
'release',
324+
'bury',
325+
'kick',
326+
'peek',
327+
'truncate'
328+
}
329+
330+
-- List of driver methods that were added to the original list.
331+
local new_driver_methods = {
332+
'tasks_by_state'
333+
}
334+
335+
-- gh-126 Check the driver API.
336+
local function check_driver_api(tube_impl, tube_type)
337+
for _, v in pairs(required_driver_methods) do
338+
if tube_impl[v] == nil then
339+
error('The "' .. tube_type ..'" driver does not have an'
340+
.. ' implementation of method "' .. v ..'".')
341+
end
342+
end
343+
344+
for _, v in pairs(new_driver_methods) do
345+
if tube_impl[v] == nil then
346+
log.warn('The "' .. tube_type ..'" driver doesn\'t have an'
347+
.. ' implementation of method "' .. v ..'". Some queue'
348+
.. ' functionality may not work properly.')
349+
end
350+
end
351+
end
352+
353+
-- Cache of already verified drivers.
354+
local checked_drivers = {}
355+
311356
local function make_self(driver, space, tube_name, tube_type, tube_id, opts)
312357
opts = opts or {}
313358
local self
@@ -362,6 +407,12 @@ local function make_self(driver, space, tube_name, tube_type, tube_id, opts)
362407
}, {
363408
__index = tube
364409
})
410+
411+
if checked_drivers[tube_type] == nil then
412+
check_driver_api(self.raw, tube_type)
413+
checked_drivers[tube_type] = true
414+
end
415+
365416
self:on_task_change(opts.on_task_change)
366417
queue.tube[tube_name] = self
367418

0 commit comments

Comments
 (0)