Sha256: f7f23428095387dfd625b3572b3f68a1c5fc7f7975b99a098e93db26847cadcf

Contents?: true

Size: 1.16 KB

Versions: 2

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true
require_relative 'time_bomb/bomb'

module Ragent
  module Plugin
    class TimeBomb
      include Ragent::Plugin

      plugin_name 'time_bomb'
      commands :tick, :list, :disarm

      def configure(*_args)
        @next_time_bomb_id = 1
        @time_bombs = {}
        subscribe('time-bomb-boom', :boom_callback)
      end

      def command_disarm(options)
        bomb_name = "time_bomb_#{options['bomb']}"
        bomb = agents(bomb_name)
        if bomb
          bomb.terminate
          @time_bombs.delete(bomb_name)
          "terminated #{bomb_name}"
        else
          "no such bomb #{bomb_name}"
        end
      end

      def command_list(_options)
        @time_bombs.keys.join("\n")
      end

      def command_tick(_options)
        # how to track the bombs to disarm them?
        as = "time_bomb_#{@next_time_bomb_id}"
        @time_bombs[as] = true
        @next_time_bomb_id += 1

        agent(type: TimeBomb::Bomb, as: as)

        "starting #{as}"
      end

      def boom_callback(_topic, params)
        debug "boom: #{params.inspect}"
      end
    end
  end
end

Ragent.ragent.plugins.register(Ragent::Plugin::TimeBomb)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ragent-0.0.6 examples/lib/ragent/plugin/time_bomb.rb
ragent-0.0.5 examples/lib/ragent/plugin/time_bomb.rb