lib/tobox/plugins/stats.rb in tobox-0.4.5 vs lib/tobox/plugins/stats.rb in tobox-0.5.0

- old
+ new

@@ -23,28 +23,40 @@ def start return if @running config = @config + plugins = config.plugins.map(&:name) + interval = config.stats_interval_seconds @stats_handlers = Array(config.lifecycle_events[:stats]) return if @stats_handlers.empty? @error_handlers = Array(config.lifecycle_events[:error_worker]) @max_attempts = config[:max_attempts] + @created_at_column = config[:created_at_column] + @db = Sequel.connect(config.database.opts.merge(max_connections: 1)) + @db.loggers = config.database.loggers Array(config.lifecycle_events[:database_connect]).each { |cb| cb.call(@db) } - @outbox_table = config[:table] - @outbox_ds = @db[@outbox_table] + outbox_table = config[:table] + @outbox_ds = @db[outbox_table] - inbox_table = config[:inbox_table] - @inbox_ds = @db[inbox_table] if inbox_table + if plugins.include?("Tobox::Plugins::Inbox") + inbox_table = config[:inbox_table] + @inbox_ds = @db[inbox_table] + end + if @created_at_column + # discard already handled events + @oldest_event_age_ds = @outbox_ds.where(last_error: nil, run_at: nil).order(Sequel.asc(:id)) + end + logger = config.default_logger stats = method(:collect_event_stats) stats.instance_eval do alias collect call @@ -102,9 +114,16 @@ stats[:pending_count] ||= 0 stats[:failing_count] ||= 0 stats[:failed_count] ||= 0 stats[:inbox_count] = @inbox_ds.count if @inbox_ds + + if @oldest_event_age_ds + created_at = @oldest_event_age_ds.get(@created_at_column) + age = created_at ? (Time.now - created_at).to_i : 0 + stats[:oldest_event_age_in_seconds] = age + end + stats end end class << self