Module: Ocpp::Rails::SessionHookManager

Defined in:
app/services/ocpp/rails/session_hook_manager.rb

Constant Summary collapse

EVENTS =
[ "started", "stopped" ].freeze

Class Method Summary collapse

Class Method Details

.execute_hooks(charging_session, event) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/services/ocpp/rails/session_hook_manager.rb', line 6

def self.execute_hooks(charging_session, event)
  hooks = Ocpp::Rails.configuration.session_hooks

  hooks.each do |hook|
    begin
      if hook.respond_to?(:async?) && hook.async?
        # Enqueue async hook job
        Ocpp::Rails::SessionAsyncHookJob.perform_later(charging_session.id, event, hook.class.name)
      else
        # Execute synchronously
        hook.call(charging_session, event)
      end
    rescue => error
      ::Rails.logger.error("SessionHook #{hook.class.name} failed: #{error.message}")
      ::Rails.logger.error(error.backtrace.join("\n"))
    end
  end

  true
end