improved hook-runner (ttl + duplicate-check)
This commit is contained in:
parent
c78dc87575
commit
299ddaf253
1 changed files with 27 additions and 23 deletions
|
|
@ -1,40 +1,44 @@
|
||||||
|
# this calls the xrforge unix file-hooks in /root/hook.d/experience_updated (but in a safe way)
|
||||||
|
# why: the database-write are sometimes chatty/duplicated.
|
||||||
|
# therefore it ratelimits and prevents processing unchanged files.
|
||||||
|
|
||||||
require 'pp'
|
require 'pp'
|
||||||
|
require 'digest'
|
||||||
|
|
||||||
Rails.application.config.to_prepare do
|
Rails.application.config.to_prepare do
|
||||||
Model # Zeitwerk autoload model
|
Model # Zeitwerk autoload model
|
||||||
ModelFile
|
ModelFile
|
||||||
|
|
||||||
# extend by adding listeners
|
class ModelFile
|
||||||
class Model
|
|
||||||
|
|
||||||
attr_accessor :last_action_execution_time
|
|
||||||
|
|
||||||
# The macro is now run within the context of the existing Model class.
|
# The macro is now run within the context of the existing Model class.
|
||||||
after_commit :run_cli_hooks
|
after_save :run_cli_hooks
|
||||||
|
|
||||||
# Define the new method to be executed on save.
|
|
||||||
def run_cli_hooks
|
def run_cli_hooks
|
||||||
# Your custom logic to execute after Model.save or Model.update! goes here.
|
|
||||||
|
|
||||||
now = Time.current
|
file = "#{self.model.library.path}/#{self.path_within_library()}"
|
||||||
rate_limit_seconds = 2.0
|
|
||||||
last_run_time = @last_action_execution_time
|
|
||||||
|
|
||||||
if self.path.match('#')
|
if File.exist?(file)
|
||||||
if last_run_time.nil? || (now - last_run_time) > rate_limit_seconds
|
cache_key = "ttl:file:cli_hook:#{self.id}#{Digest::MD5.file(file).hexdigest}"
|
||||||
self.last_action_execution_time = now
|
ttl = 60.0 # dont trigger hook twice for the same file within 60 seconds
|
||||||
|
now = Time.current
|
||||||
|
|
||||||
# 📝 Example Logic:
|
# 1. Read the last run time from the shared cache
|
||||||
puts "\n-------- MODEL --------- #{self.id} #{self.path}\n\n"
|
last_run_time_str = Rails.cache.read(cache_key)
|
||||||
pp self
|
last_run_time = last_run_time_str ? Time.parse(last_run_time_str) : nil
|
||||||
puts "\n\n"
|
|
||||||
|
|
||||||
command = "TS_SLOTS=5 ts /manyfold/cli/manyfold.sh hook experience_updated #{self.library.path}/#{self.path} &"
|
if last_run_time.nil? || (now - last_run_time) > ttl
|
||||||
|
# 2. Write the new execution time to the shared cache
|
||||||
|
# Use `write` to set the new time. Caching a string representation is often safer/easier.
|
||||||
|
Rails.cache.write(cache_key, now.to_s, expires_in: ttl + 1.minute)
|
||||||
|
|
||||||
|
puts "[app/config/initializers/xrforge.rb] runnin hook\n"
|
||||||
|
command = "TS_SLOTS=5 ts /manyfold/cli/manyfold.sh hook experience_updated #{file} &"
|
||||||
system(command)
|
system(command)
|
||||||
|
else
|
||||||
|
puts "[app/config/initializers/xrforge.rb] skipping hook\n"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue