Skip to content

Commit ccb847a

Browse files
committed
Try to wait for workers a bit longer
...in case we have none when starting the jobs. We may still start the jobs with just one worker, but that's better than being unable to handle slow startup at all. Too bad no issue # exists for this, but fixed!
1 parent f00fa66 commit ccb847a

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/thumbnailer_server.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ mp.register_script_message("mpv_thumbnail_script-job", do_worker_job)
241241

242242
-- Register this worker with the master script
243243
local register_timer = nil
244-
local register_timeout = mp.get_time() + 3
244+
local register_timeout = mp.get_time() + 1.5
245245

246246
local register_function = function()
247247
if mp.get_time() > register_timeout and register_timer then

src/thumbnailer_shared.lua

+31-6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ local Thumbnailer = {
2525
-- Extra options for the workers
2626
worker_extra = {},
2727
},
28+
-- Set in register_client
29+
worker_register_timeout = nil,
30+
-- A timer used to wait for more workers in case we have none
31+
worker_wait_timer = nil,
2832
workers = {}
2933
}
3034

@@ -253,6 +257,8 @@ function Thumbnailer:get_thumbnail_path(time_position)
253257
end
254258

255259
function Thumbnailer:register_client()
260+
self.worker_register_timeout = mp.get_time() + 2
261+
256262
mp.register_script_message("mpv_thumbnail_script-ready", function(index, path)
257263
self:on_thumb_ready(tonumber(index), path)
258264
end)
@@ -285,8 +291,8 @@ function Thumbnailer:register_client()
285291
local thumb_script_key = not thumbnailer_options.disable_keybinds and "T" or nil
286292
mp.add_key_binding(thumb_script_key, "generate-thumbnails", function()
287293
if self.state.available then
288-
self:start_worker_jobs()
289294
mp.osd_message("Started thumbnailer jobs")
295+
self:start_worker_jobs()
290296
else
291297
mp.osd_message("Thumbnailing unavailabe")
292298
end
@@ -353,8 +359,6 @@ function Thumbnailer:prepare_source_path()
353359
end
354360

355361
function Thumbnailer:start_worker_jobs()
356-
self.state.enabled = true
357-
358362
-- Create directory for the thumbnails, if needed
359363
local l, err = utils.readdir(self.state.thumbnail_directory)
360364
if err then
@@ -372,12 +376,33 @@ function Thumbnailer:start_worker_jobs()
372376

373377
local worker_count = #worker_list
374378

379+
-- In case we have a worker timer created already, clear it
380+
-- (For example, if the video-dec-params change in quick succession or the user pressed T, etc)
381+
if self.worker_wait_timer then
382+
self.worker_wait_timer:stop()
383+
end
384+
375385
if worker_count == 0 then
376-
local err = "No thumbnail workers found. Make sure you are not missing a script!"
377-
msg.error(err)
378-
mp.osd_message(err, 3)
386+
local now = mp.get_time()
387+
if mp.get_time() > self.worker_register_timeout then
388+
-- Workers have had their time to register but we have none!
389+
local err = "No thumbnail workers found. Make sure you are not missing a script!"
390+
msg.error(err)
391+
mp.osd_message(err, 3)
392+
393+
else
394+
-- We may be too early. Delay the work start a bit to try again.
395+
msg.warn("No workers found. Waiting a bit more for them.")
396+
-- Wait at least half a second
397+
local wait_time = math.max(self.worker_register_timeout - now, 0.5)
398+
self.worker_wait_timer = mp.add_timeout(wait_time, function() self:start_worker_jobs() end)
399+
end
379400

380401
else
402+
-- We have at least one worker. This may not be all of them, but they have had
403+
-- their time to register; we've done our best waiting for them.
404+
self.state.enabled = true
405+
381406
msg.debug( ("Splitting %d thumbnails amongst %d worker(s)"):format(self.state.thumbnail_count, worker_count) )
382407

383408
local frame_job_order = self:_create_thumbnail_job_order()

0 commit comments

Comments
 (0)