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",
"requireBackgroundRunner",
"stopBackgroundRunner",
"notifyBackgroundJobsUpdated",
}
-- TODO: clean up and enforce max line width (631)

@ -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

@ -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

@ -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()

@ -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()

@ -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

@ -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)

@ -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()

@ -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)

@ -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

Loading…
Cancel
Save