Sha256: ebe3489e8fa39be16441ab4430c093fa580c7b56682fef83ef8b26230e061b82

Contents?: true

Size: 1.67 KB

Versions: 44

Compression:

Stored size: 1.67 KB

Contents

module Perus::Pinger
    class Script < Command
        option :commands
        abstract!

        def run
            actions = []
            results = []
            late_actions = []

            options.commands.each do |config|
                begin
                    command = ::Perus::Pinger.const_get(config['type'])
                    actions << command.new(config['options'], config['id'])
                rescue => e
                    if config['id']
                        results[config['id']] = e.inspect
                    else
                        puts 'Error - action does not have an associated id'
                        p config
                    end
                end
            end

            actions.each do |action|
                begin
                    result = action.run

                    if result.instance_of?(Proc)
                        late_actions << result
                        result = true
                    end

                    results << result
                rescue => e
                    results << e.inspect
                end
            end

            failed = results.any? {|result| result.is_a?(String)}
            return results.join(', ') if failed

            if late_actions.empty?
                true
            else
                Proc.new do
                    late_actions.each do |code|
                        begin
                            code.call
                        rescue => e
                            puts 'Error running late action'
                            puts e.inspect
                        end
                    end
                end
            end
        end
    end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
perus-0.1.3 lib/perus/pinger/commands/script.rb
perus-0.1.2 lib/perus/pinger/commands/script.rb
perus-0.1.1 lib/perus/pinger/commands/script.rb
perus-0.1.0 lib/perus/pinger/commands/script.rb