Skip to content

Commit

Permalink
Delay first poll
Browse files Browse the repository at this point in the history
  • Loading branch information
javalikescript committed Jul 25, 2024
1 parent a847081 commit 6b233c4
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions lha/Engine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,15 @@ return class.create(function(engine)
end
end

function engine:getHeartbeatDelay()
return math.floor(self.options.heartbeat * 1000 + 0.5)
end

function engine:startHeartbeat()
self.eventId = event:setInterval(function()
self.scheduler:runTo()
self:publishEvent('heartbeat')
end, math.floor(self.options.heartbeat * 1000 + 0.5))
end, self:getHeartbeatDelay())
end

function engine:stopHeartbeat()
Expand Down Expand Up @@ -217,12 +221,6 @@ return class.create(function(engine)
self:publishExtensionsEvent(nil, ...)
end

function engine:publishEventAsync(...)
event:setTimeout(function(...)
self:publishExtensionsEvent(nil, ...)
end, 0, ...)
end

function engine:publishExtensionsEvent(source, ...)
local name = ...
logger:finest('Publishing Extensions Event %s', name)
Expand Down Expand Up @@ -590,7 +588,6 @@ return class.create(function(engine)
end
self:createScheduler()
self:startHTTPServer()
self:startHeartbeat()
self:loadExtensions()
self:loadThings()
self:loadThingValues()
Expand All @@ -599,6 +596,7 @@ return class.create(function(engine)
self:publishEvent('configuration')
self:publishEvent('extensions')
self:publishEvent('things')
self:startHeartbeat()
end

function engine:stop()
Expand All @@ -616,7 +614,7 @@ end, function(Engine)

function Engine.launch(arguments)
local options, customOptions = tables.createArgumentTable(arguments, {
configPath = 'file',
configPath = 'engine',
emptyPath = 'work',
helpPath = 'help',
disableSchemaDefaults = true,
Expand All @@ -634,8 +632,11 @@ end, function(Engine)
rootLogger:setConfig(options.loglevel)
local engine = Engine:new(options)
engine:start(defaultConfig, customOptions.config)
-- Do we need to poll at startup?
engine:publishEventAsync('poll')
-- Poll before first heartbeat
event:setTimeout(function()
logger:info('Start polling')
engine:publishEvent('poll')
end, engine:getHeartbeatDelay() // 3)
return engine
end

Expand Down

0 comments on commit 6b233c4

Please sign in to comment.