Skip to content

Commit

Permalink
fix(processor): properly apply config file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Dec 28, 2023
1 parent 498afb9 commit 3a302cd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/controllers/monitor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class EdgeAI::Monitor < EdgeAI::Base
begin
info = File.info(file)
!info.size.zero? && info.modification_time >= created_after
rescue err : File::NotFoundError
rescue File::NotFoundError
nil
rescue error
puts "Error obtaining file info for #{file}\n#{error.inspect_with_backtrace}"
Expand Down
2 changes: 2 additions & 0 deletions src/models/detection_signal.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,7 @@ class EdgeAI::DetectionSignal
def shutdown
@server.try &.close
File.delete(@path) rescue nil
connections = @mutex.synchronize { @connections.dup }
connections.each(&.close)
end
end
19 changes: 11 additions & 8 deletions src/processor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,8 @@ class EdgeAI::Processor
alias Pipeline = TensorflowLite::Pipeline::Configuration::Pipeline

def initialize
@pipelines = if File.exists?(PIPELINE_CONFIG)
NamedTuple(pipelines: Hash(String, Pipeline)).from_yaml(File.read(PIPELINE_CONFIG))[:pipelines]
else
File.write EdgeAI::PIPELINE_CONFIG, %({"pipelines": {}})
{} of String => Pipeline
end
File.write EdgeAI::PIPELINE_CONFIG, %({"pipelines": {}}) unless File.exists?(PIPELINE_CONFIG)
@pipelines = read_config
end

@shutdown : Bool = false
Expand All @@ -22,12 +18,19 @@ class EdgeAI::Processor

@pipelines : Hash(String, Pipeline)

def read_config : Hash(String, Pipeline)
NamedTuple(pipelines: Hash(String, Pipeline)).from_yaml(File.read(PIPELINE_CONFIG))[:pipelines]
rescue error
Log.warn(exception: error) { "failed to read configuration file, applying default" }
{} of String => Pipeline
end

def monitor_config
monitor = ConfigChange.instance
monitor.on_change do |file_data|
monitor.on_change do |_file_data|
begin
Log.info { "config update detected" }
update_config NamedTuple(pipelines: Hash(String, Pipeline)).from_yaml(file_data)[:pipelines]
update_config read_config
rescue error
Log.warn(exception: error) { "failed to apply configuration change" }
end
Expand Down

0 comments on commit 3a302cd

Please sign in to comment.