Tame BackgroundRunner: stop running when no more job (#6605)

A BackgroundRunner plugin instance will stop running
(rescheduling a check every 2 seconds) when there is no
(or no more) job to run.
Clients of this service now have to emit an event after
adding a job into PluginShare.backgroundJobs, so an
already loaded but stopped BackgroundRunner can notice
it and start running again.
reviewable/pr6602/r2
poire-z 4 years ago committed by GitHub
parent 0e7f1ba317
commit 962fd02c98
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -137,6 +137,7 @@ files["spec/unit/*"].globals = {
"package", "package",
"requireBackgroundRunner", "requireBackgroundRunner",
"stopBackgroundRunner", "stopBackgroundRunner",
"notifyBackgroundJobsUpdated",
} }
-- TODO: clean up and enforce max line width (631) -- TODO: clean up and enforce max line width (631)

@ -178,6 +178,11 @@ function CervantesPowerD:calculateAutoWarmth()
end end
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 self.autowarmth_job_running = true
end end
end end

@ -303,6 +303,11 @@ function KoboPowerD:calculateAutoWarmth()
end end
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 self.autowarmth_job_running = true
end end
end end

@ -26,6 +26,9 @@ function BackgroundTaskPlugin:_schedule(settings_id)
repeated = enabled, repeated = enabled,
executable = self.executable, executable = self.executable,
}) })
local Event = require("ui/event")
local UIManager = require("ui/uimanager")
UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated"))
end end
function BackgroundTaskPlugin:_start() function BackgroundTaskPlugin:_start()

@ -48,6 +48,8 @@ function AutoFrontlight:_schedule(settings_id)
end end
end end
}) })
local Event = require("ui/event")
UIManager:broadcastEvent(Event:new("BackgroundJobsUpdated"))
end end
function AutoFrontlight:_action() function AutoFrontlight:_action()

@ -171,7 +171,6 @@ function BackgroundRunner:_execute()
local round = 0 local round = 0
while #self.jobs > 0 do while #self.jobs > 0 do
local job = table.remove(self.jobs, 1) local job = table.remove(self.jobs, 1)
logger.dbg("BackgroundRunner: run job ", job, " @ ", os.time())
if job.insert_sec == nil then if job.insert_sec == nil then
-- Jobs are first inserted to jobs table from external users. So -- Jobs are first inserted to jobs table from external users. So
-- they may not have insert_sec field. -- they may not have insert_sec field.
@ -206,6 +205,7 @@ function BackgroundRunner:_execute()
end end
if should_execute then if should_execute then
logger.dbg("BackgroundRunner: run job ", job, " @ ", os.time())
assert(not should_ignore) assert(not should_ignore)
self:_executeJob(job) self:_executeJob(job)
break break
@ -220,7 +220,11 @@ function BackgroundRunner:_execute()
self.running = false self.running = false
if PluginShare.stopBackgroundRunner == nil then 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 else
logger.dbg("BackgroundRunnerWidget: stop running @ ", os.time()) logger.dbg("BackgroundRunnerWidget: stop running @ ", os.time())
end end
@ -229,9 +233,13 @@ end
function BackgroundRunner:_schedule() function BackgroundRunner:_schedule()
assert(self ~= nil) assert(self ~= nil)
if self.running == false then if self.running == false then
logger.dbg("BackgroundRunnerWidget: start running @ ", os.time()) if #self.jobs == 0 and not CommandRunner:pending() then
self.running = true logger.dbg("BackgroundRunnerWidget: no job, not running @ ", os.time())
UIManager:scheduleIn(2, function() self:_execute() end) else
logger.dbg("BackgroundRunnerWidget: start running @ ", os.time())
self.running = true
UIManager:scheduleIn(2, function() self:_execute() end)
end
else else
logger.dbg("BackgroundRunnerWidget: a schedule is pending @ ", logger.dbg("BackgroundRunnerWidget: a schedule is pending @ ",
os.time()) os.time())
@ -262,4 +270,10 @@ function BackgroundRunnerWidget:onResume()
BackgroundRunner:_schedule() BackgroundRunner:_schedule()
end end
function BackgroundRunnerWidget:onBackgroundJobsUpdated()
logger.dbg("BackgroundRunnerWidget:onBackgroundJobsUpdated() @ ", os.time())
PluginShare.stopBackgroundRunner = nil
BackgroundRunner:_schedule()
end
return BackgroundRunnerWidget return BackgroundRunnerWidget

@ -49,6 +49,7 @@ describe("AutoFrontlight widget tests", function()
requireBackgroundRunner() requireBackgroundRunner()
class = dofile("plugins/autofrontlight.koplugin/main.lua") class = dofile("plugins/autofrontlight.koplugin/main.lua")
notifyBackgroundJobsUpdated()
-- Ensure the background runner has succeeded set the job.insert_sec. -- Ensure the background runner has succeeded set the job.insert_sec.
MockTime:increase(2) MockTime:increase(2)

@ -35,6 +35,8 @@ describe("BackgroundRunner widget tests", function()
executed = true executed = true
end, end,
}) })
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
MockTime:increase(9) MockTime:increase(9)
@ -54,6 +56,7 @@ describe("BackgroundRunner widget tests", function()
executed = executed + 1 executed = executed + 1
end, end,
}) })
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -77,6 +80,7 @@ describe("BackgroundRunner widget tests", function()
executed = executed + 1 executed = executed + 1
end, end,
}) })
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -102,6 +106,7 @@ describe("BackgroundRunner widget tests", function()
end, end,
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -125,6 +130,7 @@ describe("BackgroundRunner widget tests", function()
end, end,
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -149,6 +155,7 @@ describe("BackgroundRunner widget tests", function()
} }
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -166,6 +173,7 @@ describe("BackgroundRunner widget tests", function()
} }
job.end_sec = nil job.end_sec = nil
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -196,6 +204,7 @@ describe("BackgroundRunner widget tests", function()
end, end,
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -210,6 +219,7 @@ describe("BackgroundRunner widget tests", function()
job.end_sec = nil job.end_sec = nil
env2 = "no" env2 = "no"
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -232,6 +242,7 @@ describe("BackgroundRunner widget tests", function()
} }
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
while job.end_sec == nil do while job.end_sec == nil do
MockTime:increase(2) MockTime:increase(2)
@ -253,6 +264,7 @@ describe("BackgroundRunner widget tests", function()
executed = executed + 1 executed = executed + 1
end, end,
}) })
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -281,6 +293,7 @@ describe("BackgroundRunner widget tests", function()
executed = executed + 1 executed = executed + 1
end, end,
}) })
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -305,6 +318,7 @@ describe("BackgroundRunner widget tests", function()
end, end,
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
MockTime:increase(2) MockTime:increase(2)
UIManager:handleInput() UIManager:handleInput()
@ -338,6 +352,7 @@ describe("BackgroundRunner widget tests", function()
end, end,
} }
table.insert(PluginShare.backgroundJobs, job) table.insert(PluginShare.backgroundJobs, job)
notifyBackgroundJobsUpdated()
for i = 1, 10 do for i = 1, 10 do
requireBackgroundRunner():onResume() requireBackgroundRunner():onResume()

@ -4,12 +4,19 @@ describe("BackgroundTaskPlugin", function()
local MockTime = require("mock_time") local MockTime = require("mock_time")
local UIManager = require("ui/uimanager") local UIManager = require("ui/uimanager")
local BackgroundTaskPlugin_schedule_orig = BackgroundTaskPlugin._schedule
setup(function() setup(function()
MockTime:install() MockTime:install()
local Device = require("device") local Device = require("device")
Device.input.waitEvent = function() end Device.input.waitEvent = function() end
UIManager._run_forever = true UIManager._run_forever = true
requireBackgroundRunner() 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) end)
teardown(function() teardown(function()
@ -17,6 +24,7 @@ describe("BackgroundTaskPlugin", function()
package.unloadAll() package.unloadAll()
require("document/canvascontext"):init(require("device")) require("document/canvascontext"):init(require("device"))
stopBackgroundRunner() stopBackgroundRunner()
BackgroundTaskPlugin._schedule = BackgroundTaskPlugin_schedule_orig
end) end)
local createTestPlugin = function(executable) local createTestPlugin = function(executable)

@ -103,3 +103,9 @@ stopBackgroundRunner = function()
background_runner = nil background_runner = nil
require("pluginshare").stopBackgroundRunner = true require("pluginshare").stopBackgroundRunner = true
end end
notifyBackgroundJobsUpdated = function()
if background_runner then
background_runner:onBackgroundJobsUpdated()
end
end

Loading…
Cancel
Save