Sha256: 2673359683d5c29f75ecb8ed5be07201131f7434e7dda83b533d31c71a09b85e

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

require_relative './executor'

module Fusuma
  module Plugin
    module Executors
      # Exector plugin
      class CommandExecutor < Executor
        # Executor parameter on config.yml
        # @return [Array<Symbol>]
        def execute_keys
          [:command]
        end

        def execute(event)
          search_command(event).tap do |command|
            break unless command

            MultiLogger.info(command: command, args: event.record.args)

            accel = args_accel(event)
            additional_env = event.record.args
                                  .deep_transform_keys(&:to_s)
                                  .deep_transform_values { |v| (v * accel).to_s }

            pid = Process.spawn(additional_env, command.to_s)
            Process.detach(pid)
          end
        end

        def executable?(event)
          event.tag.end_with?('_detector') &&
            event.record.type == :index &&
            search_command(event)
        end

        # @param event [Event]
        # @return [String]
        def search_command(event)
          command_index = Config::Index.new([*event.record.index.keys, :command])
          Config.search(command_index)
        end

        # @param event [Event]
        # @return [Float]
        def args_accel(event)
          accel_index = Config::Index.new([*event.record.index.keys, :accel])
          (Config.search(accel_index) || 1).to_f
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fusuma-2.0.3 lib/fusuma/plugin/executors/command_executor.rb