2020-06-22 22:18:10 +00:00
|
|
|
local Device = require("device")
|
|
|
|
|
|
|
|
-- disable on android, since it breaks expect behaviour of an activity.
|
|
|
|
-- it is also unused by other plugins.
|
|
|
|
-- See https://github.com/koreader/koreader/issues/6297
|
|
|
|
if Device:isAndroid() then
|
|
|
|
return { disabled = true, }
|
|
|
|
end
|
|
|
|
|
2017-07-28 14:48:19 +00:00
|
|
|
local CommandRunner = require("commandrunner")
|
|
|
|
local PluginShare = require("pluginshare")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
|
|
local logger = require("logger")
|
2022-05-05 19:00:22 +00:00
|
|
|
local time = require("ui/time")
|
2018-08-17 18:54:11 +00:00
|
|
|
local _ = require("gettext")
|
2017-07-28 14:48:19 +00:00
|
|
|
|
|
|
|
-- BackgroundRunner is an experimental feature to execute non-critical jobs in
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
-- the background.
|
|
|
|
-- A job is defined as a table in PluginShare.backgroundJobs table.
|
2017-07-28 14:48:19 +00:00
|
|
|
-- It contains at least following items:
|
|
|
|
-- when: number, string or function
|
|
|
|
-- number: the delay in seconds
|
|
|
|
-- string: "best-effort" - the job will be started when there is no other jobs
|
|
|
|
-- to be executed.
|
|
|
|
-- "idle" - the job will be started when the device is idle.
|
|
|
|
-- function: if the return value of the function is true, the job will be
|
|
|
|
-- executed immediately.
|
|
|
|
--
|
|
|
|
-- repeated: boolean or function or nil or number
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
-- boolean: true to repeat the job once it finished.
|
|
|
|
-- function: if the return value of the function is true, repeat the job
|
|
|
|
-- once it finishes. If the function throws an error, it equals to
|
2017-07-28 14:48:19 +00:00
|
|
|
-- return false.
|
|
|
|
-- nil: same as false.
|
|
|
|
-- number: times to repeat.
|
|
|
|
--
|
|
|
|
-- executable: string or function
|
|
|
|
-- string: the command line to be executed. The command or binary will be
|
|
|
|
-- executed in the lowest priority. Command or binary will be killed
|
|
|
|
-- if it executes for over 1 hour.
|
|
|
|
-- function: the action to be executed. The execution cannot be killed, but it
|
|
|
|
-- will be considered as timeout if it executes for more than 1
|
|
|
|
-- second.
|
|
|
|
-- If the executable times out, the job will be blocked, i.e. the repeated
|
|
|
|
-- field will be ignored.
|
|
|
|
--
|
|
|
|
-- environment: table or function or nil
|
|
|
|
-- table: the key-value pairs of all environments set for string executable.
|
|
|
|
-- function: the function to return a table of environments.
|
|
|
|
-- nil: ignore.
|
|
|
|
--
|
|
|
|
-- callback: function or nil
|
|
|
|
-- function: the action to be executed when executable has been finished.
|
|
|
|
-- Errors thrown from this function will be ignored.
|
|
|
|
-- nil: ignore.
|
|
|
|
--
|
|
|
|
-- If a job does not contain enough information, it will be ignored.
|
|
|
|
--
|
|
|
|
-- Once the job is finished, several items will be added to the table:
|
|
|
|
-- result: number, the return value of the command. In general, 0 means
|
|
|
|
-- succeeded.
|
|
|
|
-- For function executable, 1 if the function throws an error.
|
|
|
|
-- For string executable, several predefined values indicate the
|
|
|
|
-- internal errors. E.g. 223: the binary crashes. 222: the output is
|
|
|
|
-- invalid. 127: the command is invalid. 255: the command timed out.
|
|
|
|
-- Typically, consumers can use following states instead of hardcodeing
|
|
|
|
-- the error codes.
|
|
|
|
-- exception: error, the error returned from function executable. Not available
|
|
|
|
-- for string executable.
|
|
|
|
-- timeout: boolean, whether the command times out.
|
|
|
|
-- bad_command: boolean, whether the command is not found. Not available for
|
|
|
|
-- function executable.
|
|
|
|
-- blocked: boolean, whether the job is blocked.
|
2022-05-05 19:00:22 +00:00
|
|
|
-- start_time: number, the time (fts) when the job was started.
|
|
|
|
-- end_time: number, the time (fts) when the job was stopped.
|
|
|
|
-- insert_time: number, the time (fts) when the job was inserted into queue.
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
-- (All of them in the monotonic time scale, like the main event loop & task queue).
|
2017-07-28 14:48:19 +00:00
|
|
|
|
|
|
|
local BackgroundRunner = {
|
|
|
|
jobs = PluginShare.backgroundJobs,
|
2018-01-17 08:17:53 +00:00
|
|
|
running = false,
|
2017-07-28 14:48:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
--- Copies required fields from |job|.
|
|
|
|
-- @return a new table with required fields of a valid job.
|
|
|
|
function BackgroundRunner:_clone(job)
|
|
|
|
assert(job ~= nil)
|
|
|
|
local result = {}
|
|
|
|
result.when = job.when
|
|
|
|
result.repeated = job.repeated
|
|
|
|
result.executable = job.executable
|
|
|
|
result.callback = job.callback
|
|
|
|
result.environment = job.environment
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunner:_shouldRepeat(job)
|
|
|
|
if type(job.repeated) == "nil" then return false end
|
|
|
|
if type(job.repeated) == "boolean" then return job.repeated end
|
|
|
|
if type(job.repeated) == "function" then
|
|
|
|
local status, result = pcall(job.repeated)
|
|
|
|
if status then
|
|
|
|
return result
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if type(job.repeated) == "number" then
|
|
|
|
job.repeated = job.repeated - 1
|
|
|
|
return job.repeated > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunner:_finishJob(job)
|
|
|
|
if type(job.executable) == "function" then
|
2022-05-05 19:00:22 +00:00
|
|
|
local time_diff = job.end_time - job.start_time
|
|
|
|
local threshold = time.s(1)
|
|
|
|
job.timeout = (time_diff > threshold)
|
2017-07-28 14:48:19 +00:00
|
|
|
end
|
|
|
|
job.blocked = job.timeout
|
|
|
|
if not job.blocked and self:_shouldRepeat(job) then
|
|
|
|
self:_insert(self:_clone(job))
|
|
|
|
end
|
|
|
|
if type(job.callback) == "function" then
|
|
|
|
pcall(job.callback)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Executes |job|.
|
|
|
|
-- @treturn boolean true if job is valid.
|
|
|
|
function BackgroundRunner:_executeJob(job)
|
|
|
|
assert(not CommandRunner:pending())
|
|
|
|
if job == nil then return false end
|
|
|
|
if job.executable == nil then return false end
|
|
|
|
|
|
|
|
if type(job.executable) == "string" then
|
|
|
|
CommandRunner:start(job)
|
|
|
|
return true
|
|
|
|
elseif type(job.executable) == "function" then
|
2022-05-05 19:00:22 +00:00
|
|
|
job.start_time = UIManager:getTime()
|
2017-07-28 14:48:19 +00:00
|
|
|
local status, err = pcall(job.executable)
|
|
|
|
if status then
|
|
|
|
job.result = 0
|
|
|
|
else
|
|
|
|
job.result = 1
|
|
|
|
job.exception = err
|
|
|
|
end
|
2022-05-05 19:00:22 +00:00
|
|
|
job.end_time = time.now()
|
2017-07-28 14:48:19 +00:00
|
|
|
self:_finishJob(job)
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Polls the status of the pending CommandRunner.
|
|
|
|
function BackgroundRunner:_poll()
|
|
|
|
assert(CommandRunner:pending())
|
|
|
|
local result = CommandRunner:poll()
|
|
|
|
if result == nil then return end
|
|
|
|
|
|
|
|
self:_finishJob(result)
|
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunner:_execute()
|
|
|
|
logger.dbg("BackgroundRunner: _execute() @ ", os.time())
|
|
|
|
if CommandRunner:pending() then
|
|
|
|
self:_poll()
|
|
|
|
else
|
|
|
|
local round = 0
|
|
|
|
while #self.jobs > 0 do
|
|
|
|
local job = table.remove(self.jobs, 1)
|
2022-05-05 19:00:22 +00:00
|
|
|
if job.insert_time == nil then
|
The great Input/GestureDetector/TimeVal spring cleanup (a.k.a., a saner main loop) (#7415)
* ReaderDictionary: Port delay computations to TimeVal
* ReaderHighlight: Port delay computations to TimeVal
* ReaderView: Port delay computations to TimeVal
* Android: Reset gesture detection state on APP_CMD_TERM_WINDOW.
This prevents potentially being stuck in bogus gesture states when switching apps.
* GestureDetector:
* Port delay computations to TimeVal
* Fixed delay computations to handle time warps (large and negative deltas).
* Simplified timed callback handling to invalidate timers much earlier, preventing accumulating useless timers that no longer have any chance of ever detecting a gesture.
* Fixed state clearing to handle the actual effective slots, instead of hard-coding slot 0 & slot 1.
* Simplified timed callback handling in general, and added support for a timerfd backend for better performance and accuracy.
* The improved timed callback handling allows us to detect and honor (as much as possible) the three possible clock sources usable by Linux evdev events.
The only case where synthetic timestamps are used (and that only to handle timed callbacks) is limited to non-timerfd platforms where input events use
a clock source that is *NOT* MONOTONIC.
AFAICT, that's pretty much... PocketBook, and that's it?
* Input:
* Use the <linux/input.h> FFI module instead of re-declaring every constant
* Fixed (verbose) debug logging of input events to actually translate said constants properly.
* Completely reset gesture detection state on suspend. This should prevent bogus gesture detection on resume.
* Refactored the waitEvent loop to make it easier to comprehend (hopefully) and much more efficient.
Of specific note, it no longer does a crazy select spam every 100µs, instead computing and relying on sane timeouts,
as afforded by switching the UI event/input loop to the MONOTONIC time base, and the refactored timed callbacks in GestureDetector.
* reMarkable: Stopped enforcing synthetic timestamps on input events, as it should no longer be necessary.
* TimeVal:
* Refactored and simplified, especially as far as metamethods are concerned (based on <bsd/sys/time.h>).
* Added a host of new methods to query the various POSIX clock sources, and made :now default to MONOTONIC.
* Removed the debug guard in __sub, as time going backwards can be a perfectly normal occurrence.
* New methods:
* Clock sources: :realtime, :monotonic, :monotonic_coarse, :realtime_coarse, :boottime
* Utility: :tonumber, :tousecs, :tomsecs, :fromnumber, :isPositive, :isZero
* UIManager:
* Ported event loop & scheduling to TimeVal, and switched to the MONOTONIC time base.
This ensures reliable and consistent scheduling, as time is ensured never to go backwards.
* Added a :getTime() method, that returns a cached TimeVal:now(), updated at the top of every UI frame.
It's used throughout the codebase to cadge a syscall in circumstances where we are guaranteed that a syscall would return a mostly identical value,
because very few time has passed.
The only code left that does live syscalls does it because it's actually necessary for accuracy,
and the only code left that does that in a REALTIME time base is code that *actually* deals with calendar time (e.g., Statistics).
* DictQuickLookup: Port delay computations to TimeVal
* FootNoteWidget: Port delay computations to TimeVal
* HTMLBoxWidget: Port delay computations to TimeVal
* Notification: Port delay computations to TimeVal
* TextBoxWidget: Port delay computations to TimeVal
* AutoSuspend: Port to TimeVal
* AutoTurn:
* Fix it so that settings are actually honored.
* Port to TimeVal
* BackgroundRunner: Port to TimeVal
* Calibre: Port benchmarking code to TimeVal
* BookInfoManager: Removed unnecessary yield in the metadata extraction subprocess now that subprocesses get scheduled properly.
* All in all, these changes reduced the CPU cost of a single tap by a factor of ten (!), and got rid of an insane amount of weird poll/wakeup cycles that must have been hell on CPU schedulers and batteries..
2021-03-30 00:57:59 +00:00
|
|
|
-- Jobs are first inserted to jobs table from external users.
|
|
|
|
-- So they may not have an insert field.
|
2022-05-05 19:00:22 +00:00
|
|
|
job.insert_time = UIManager:getTime()
|
2017-07-28 14:48:19 +00:00
|
|
|
end
|
|
|
|
local should_execute = false
|
|
|
|
local should_ignore = false
|
|
|
|
if type(job.when) == "function" then
|
|
|
|
local status, result = pcall(job.when)
|
|
|
|
if status then
|
|
|
|
should_execute = result
|
|
|
|
else
|
|
|
|
should_ignore = true
|
|
|
|
end
|
|
|
|
elseif type(job.when) == "number" then
|
|
|
|
if job.when >= 0 then
|
2022-05-05 19:00:22 +00:00
|
|
|
should_execute = (UIManager:getTime() - job.insert_time >= time.s(job.when))
|
2017-07-28 14:48:19 +00:00
|
|
|
else
|
|
|
|
should_ignore = true
|
|
|
|
end
|
|
|
|
elseif type(job.when) == "string" then
|
2019-08-23 17:53:53 +00:00
|
|
|
--- @todo (Hzj_jie): Implement "idle" mode
|
2017-07-28 14:48:19 +00:00
|
|
|
if job.when == "best-effort" then
|
|
|
|
should_execute = (round > 0)
|
|
|
|
elseif job.when == "idle" then
|
|
|
|
should_execute = (round > 1)
|
|
|
|
else
|
|
|
|
should_ignore = true
|
|
|
|
end
|
|
|
|
else
|
|
|
|
should_ignore = true
|
|
|
|
end
|
|
|
|
|
|
|
|
if should_execute then
|
2020-08-31 14:48:29 +00:00
|
|
|
logger.dbg("BackgroundRunner: run job ", job, " @ ", os.time())
|
2017-07-28 14:48:19 +00:00
|
|
|
assert(not should_ignore)
|
|
|
|
self:_executeJob(job)
|
|
|
|
break
|
|
|
|
elseif not should_ignore then
|
|
|
|
table.insert(self.jobs, job)
|
|
|
|
end
|
|
|
|
|
|
|
|
round = round + 1
|
|
|
|
if round > 2 then break end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-17 08:17:53 +00:00
|
|
|
self.running = false
|
2017-07-28 14:48:19 +00:00
|
|
|
if PluginShare.stopBackgroundRunner == nil then
|
2020-08-31 14:48:29 +00:00
|
|
|
if #self.jobs == 0 and not CommandRunner:pending() then
|
|
|
|
logger.dbg("BackgroundRunnerWidget: no job, stop running @ ", os.time())
|
|
|
|
else
|
|
|
|
self:_schedule()
|
|
|
|
end
|
2018-01-17 08:17:53 +00:00
|
|
|
else
|
|
|
|
logger.dbg("BackgroundRunnerWidget: stop running @ ", os.time())
|
2017-07-28 14:48:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunner:_schedule()
|
2018-01-17 08:17:53 +00:00
|
|
|
if self.running == false then
|
2020-08-31 14:48:29 +00:00
|
|
|
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
|
2018-01-17 08:17:53 +00:00
|
|
|
else
|
|
|
|
logger.dbg("BackgroundRunnerWidget: a schedule is pending @ ",
|
|
|
|
os.time())
|
|
|
|
end
|
2017-07-28 14:48:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunner:_insert(job)
|
2022-05-05 19:00:22 +00:00
|
|
|
job.insert_time = UIManager:getTime()
|
2017-07-28 14:48:19 +00:00
|
|
|
table.insert(self.jobs, job)
|
|
|
|
end
|
|
|
|
|
|
|
|
BackgroundRunner:_schedule()
|
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local BackgroundRunnerWidget = WidgetContainer:extend{
|
2017-07-28 14:48:19 +00:00
|
|
|
name = "backgroundrunner",
|
|
|
|
runner = BackgroundRunner,
|
|
|
|
}
|
|
|
|
|
2018-01-17 08:17:53 +00:00
|
|
|
function BackgroundRunnerWidget:onSuspend()
|
|
|
|
logger.dbg("BackgroundRunnerWidget:onSuspend() @ ", os.time())
|
|
|
|
PluginShare.stopBackgroundRunner = true
|
|
|
|
end
|
|
|
|
|
|
|
|
function BackgroundRunnerWidget:onResume()
|
|
|
|
logger.dbg("BackgroundRunnerWidget:onResume() @ ", os.time())
|
|
|
|
PluginShare.stopBackgroundRunner = nil
|
|
|
|
BackgroundRunner:_schedule()
|
|
|
|
end
|
|
|
|
|
2020-08-31 14:48:29 +00:00
|
|
|
function BackgroundRunnerWidget:onBackgroundJobsUpdated()
|
|
|
|
logger.dbg("BackgroundRunnerWidget:onBackgroundJobsUpdated() @ ", os.time())
|
|
|
|
PluginShare.stopBackgroundRunner = nil
|
|
|
|
BackgroundRunner:_schedule()
|
|
|
|
end
|
|
|
|
|
2017-07-28 14:48:19 +00:00
|
|
|
return BackgroundRunnerWidget
|