diff --git a/.luacheckrc b/.luacheckrc index b56daf1f5..cb1b73554 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -137,6 +137,7 @@ files["spec/unit/*"].globals = { "package", "requireBackgroundRunner", "stopBackgroundRunner", + "notifyBackgroundJobsUpdated", } -- TODO: clean up and enforce max line width (631) diff --git a/frontend/device/cervantes/powerd.lua b/frontend/device/cervantes/powerd.lua index 22b8541ec..09c6644d6 100644 --- a/frontend/device/cervantes/powerd.lua +++ b/frontend/device/cervantes/powerd.lua @@ -178,6 +178,11 @@ function CervantesPowerD:calculateAutoWarmth() end end, }) + if package.loaded["ui/uimanager"] ~= nil then + local Event = require("ui/event") + local UIManager = require("ui/uimanager") + UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated")) + end self.autowarmth_job_running = true end end diff --git a/frontend/device/kobo/powerd.lua b/frontend/device/kobo/powerd.lua index a80a8e1b8..e577ba98c 100644 --- a/frontend/device/kobo/powerd.lua +++ b/frontend/device/kobo/powerd.lua @@ -303,6 +303,11 @@ function KoboPowerD:calculateAutoWarmth() end end, }) + if package.loaded["ui/uimanager"] ~= nil then + local Event = require("ui/event") + local UIManager = require("ui/uimanager") + UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated")) + end self.autowarmth_job_running = true end end diff --git a/frontend/ui/plugin/background_task_plugin.lua b/frontend/ui/plugin/background_task_plugin.lua index e430aed13..eb8e75eeb 100644 --- a/frontend/ui/plugin/background_task_plugin.lua +++ b/frontend/ui/plugin/background_task_plugin.lua @@ -26,6 +26,9 @@ function BackgroundTaskPlugin:_schedule(settings_id) repeated = enabled, executable = self.executable, }) + local Event = require("ui/event") + local UIManager = require("ui/uimanager") + UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated")) end function BackgroundTaskPlugin:_start() diff --git a/plugins/autofrontlight.koplugin/main.lua b/plugins/autofrontlight.koplugin/main.lua index 43cc26e28..e6223ae79 100644 --- a/plugins/autofrontlight.koplugin/main.lua +++ b/plugins/autofrontlight.koplugin/main.lua @@ -48,6 +48,8 @@ function AutoFrontlight:_schedule(settings_id) end end }) + local Event = require("ui/event") + UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated")) end function AutoFrontlight:_action() diff --git a/plugins/backgroundrunner.koplugin/main.lua b/plugins/backgroundrunner.koplugin/main.lua index b16d42a00..b462a9317 100644 --- a/plugins/backgroundrunner.koplugin/main.lua +++ b/plugins/backgroundrunner.koplugin/main.lua @@ -171,7 +171,6 @@ function BackgroundRunner:_execute() local round = 0 while #self.jobs > 0 do local job = table.remove(self.jobs, 1) - logger.dbg("BackgroundRunner: run job ", job, " @ ", os.time()) if job.insert_sec == nil then -- Jobs are first inserted to jobs table from external users. So -- they may not have insert_sec field. @@ -206,6 +205,7 @@ function BackgroundRunner:_execute() end if should_execute then + logger.dbg("BackgroundRunner: run job ", job, " @ ", os.time()) assert(not should_ignore) self:_executeJob(job) break @@ -220,7 +220,11 @@ function BackgroundRunner:_execute() self.running = false if PluginShare.stopBackgroundRunner == nil then - self:_schedule() + if #self.jobs == 0 and not CommandRunner:pending() then + logger.dbg("BackgroundRunnerWidget: no job, stop running @ ", os.time()) + else + self:_schedule() + end else logger.dbg("BackgroundRunnerWidget: stop running @ ", os.time()) end @@ -229,9 +233,13 @@ end function BackgroundRunner:_schedule() assert(self ~= nil) if self.running == false then - logger.dbg("BackgroundRunnerWidget: start running @ ", os.time()) - self.running = true - UIManager:scheduleIn(2, function() self:_execute() end) + if #self.jobs == 0 and not CommandRunner:pending() then + logger.dbg("BackgroundRunnerWidget: no job, not running @ ", os.time()) + else + logger.dbg("BackgroundRunnerWidget: start running @ ", os.time()) + self.running = true + UIManager:scheduleIn(2, function() self:_execute() end) + end else logger.dbg("BackgroundRunnerWidget: a schedule is pending @ ", os.time()) @@ -262,4 +270,10 @@ function BackgroundRunnerWidget:onResume() BackgroundRunner:_schedule() end +function BackgroundRunnerWidget:onBackgroundJobsUpdated() + logger.dbg("BackgroundRunnerWidget:onBackgroundJobsUpdated() @ ", os.time()) + PluginShare.stopBackgroundRunner = nil + BackgroundRunner:_schedule() +end + return BackgroundRunnerWidget diff --git a/spec/unit/autofrontlight_spec.lua b/spec/unit/autofrontlight_spec.lua index 01a3af2a7..0735b8499 100644 --- a/spec/unit/autofrontlight_spec.lua +++ b/spec/unit/autofrontlight_spec.lua @@ -49,6 +49,7 @@ describe("AutoFrontlight widget tests", function() requireBackgroundRunner() class = dofile("plugins/autofrontlight.koplugin/main.lua") + notifyBackgroundJobsUpdated() -- Ensure the background runner has succeeded set the job.insert_sec. MockTime:increase(2) diff --git a/spec/unit/background_runner_spec.lua b/spec/unit/background_runner_spec.lua index 3a87f5553..6659b88fc 100644 --- a/spec/unit/background_runner_spec.lua +++ b/spec/unit/background_runner_spec.lua @@ -35,6 +35,8 @@ describe("BackgroundRunner widget tests", function() executed = true end, }) + notifyBackgroundJobsUpdated() + MockTime:increase(2) UIManager:handleInput() MockTime:increase(9) @@ -54,6 +56,7 @@ describe("BackgroundRunner widget tests", function() executed = executed + 1 end, }) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -77,6 +80,7 @@ describe("BackgroundRunner widget tests", function() executed = executed + 1 end, }) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -102,6 +106,7 @@ describe("BackgroundRunner widget tests", function() end, } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -125,6 +130,7 @@ describe("BackgroundRunner widget tests", function() end, } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -149,6 +155,7 @@ describe("BackgroundRunner widget tests", function() } } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -166,6 +173,7 @@ describe("BackgroundRunner widget tests", function() } job.end_sec = nil table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -196,6 +204,7 @@ describe("BackgroundRunner widget tests", function() end, } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -210,6 +219,7 @@ describe("BackgroundRunner widget tests", function() job.end_sec = nil env2 = "no" table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -232,6 +242,7 @@ describe("BackgroundRunner widget tests", function() } } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() while job.end_sec == nil do MockTime:increase(2) @@ -253,6 +264,7 @@ describe("BackgroundRunner widget tests", function() executed = executed + 1 end, }) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -281,6 +293,7 @@ describe("BackgroundRunner widget tests", function() executed = executed + 1 end, }) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -305,6 +318,7 @@ describe("BackgroundRunner widget tests", function() end, } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() MockTime:increase(2) UIManager:handleInput() @@ -338,6 +352,7 @@ describe("BackgroundRunner widget tests", function() end, } table.insert(PluginShare.backgroundJobs, job) + notifyBackgroundJobsUpdated() for i = 1, 10 do requireBackgroundRunner():onResume() diff --git a/spec/unit/background_task_plugin_spec.lua b/spec/unit/background_task_plugin_spec.lua index 61f795231..71bb2c6d0 100644 --- a/spec/unit/background_task_plugin_spec.lua +++ b/spec/unit/background_task_plugin_spec.lua @@ -4,12 +4,19 @@ describe("BackgroundTaskPlugin", function() local MockTime = require("mock_time") local UIManager = require("ui/uimanager") + local BackgroundTaskPlugin_schedule_orig = BackgroundTaskPlugin._schedule setup(function() MockTime:install() local Device = require("device") Device.input.waitEvent = function() end UIManager._run_forever = true requireBackgroundRunner() + -- Monkey patch this method to notify BackgroundRunner + -- as it is not accessible to UIManager in these tests + BackgroundTaskPlugin._schedule = function(...) + BackgroundTaskPlugin_schedule_orig(...) + notifyBackgroundJobsUpdated() + end end) teardown(function() @@ -17,6 +24,7 @@ describe("BackgroundTaskPlugin", function() package.unloadAll() require("document/canvascontext"):init(require("device")) stopBackgroundRunner() + BackgroundTaskPlugin._schedule = BackgroundTaskPlugin_schedule_orig end) local createTestPlugin = function(executable) diff --git a/spec/unit/commonrequire.lua b/spec/unit/commonrequire.lua index 538bdbc86..2e8342b5d 100644 --- a/spec/unit/commonrequire.lua +++ b/spec/unit/commonrequire.lua @@ -103,3 +103,9 @@ stopBackgroundRunner = function() background_runner = nil require("pluginshare").stopBackgroundRunner = true end + +notifyBackgroundJobsUpdated = function() + if background_runner then + background_runner:onBackgroundJobsUpdated() + end +end