Sha256: 1c889f00a39fa178ecb672c8db6f6c81aa7adbfef1eda64cf043082731189d2a

Contents?: true

Size: 1.77 KB

Versions: 25

Compression:

Stored size: 1.77 KB

Contents

#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative 'example_helper'

example_description = <<DESC
  Memory limit watcher Example
  ===========================

In this example we are setting a watcher that will terminate our world object
when process memory consumption exceeds a limit that will be set.


DESC

module MemorylimiterExample
  class SampleAction < Dynflow::Action
    def plan(memory_to_use)
      plan_self(number: memory_to_use)
    end

    def run
      array = Array.new(input[:number].to_i)
      puts "[action] allocated #{input[:number]} cells"
    end
  end
end

if $0 == __FILE__
  puts example_description

  world = ExampleHelper.create_world do |config|
    config.exit_on_terminate = false
  end

  world.terminated.on_resolution do
    puts '[world] The world has been terminated'
  end

  require 'get_process_mem'
  memory_info_provider = GetProcessMem.new
  puts '[info] Preparing memory watcher: '
  require 'dynflow/watchers/memory_consumption_watcher'
  puts "[info] now the process consumes #{memory_info_provider.bytes} bytes."
  limit = memory_info_provider.bytes + 500_000
  puts "[info] Setting memory limit to #{limit} bytes"
  watcher = Dynflow::Watchers::MemoryConsumptionWatcher.new(world, limit, polling_interval: 1)
  puts '[info] Small action: '
  world.trigger(MemorylimiterExample::SampleAction, 10)
  sleep 2
  puts "[info] now the process consumes #{memory_info_provider.bytes} bytes."
  puts '[info] Big action: '
  world.trigger(MemorylimiterExample::SampleAction, 500_000)
  sleep 2
  puts "[info] now the process consumes #{memory_info_provider.bytes} bytes."
  puts '[info] Small action again - will not execute, the world is not accepting requests'
  world.trigger(MemorylimiterExample::SampleAction, 500_000)
  sleep 2
  puts 'Done'
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
dynflow-1.8.2 examples/memory_limit_watcher.rb
dynflow-1.8.1 examples/memory_limit_watcher.rb
dynflow-1.8.0 examples/memory_limit_watcher.rb
dynflow-1.7.0 examples/memory_limit_watcher.rb
dynflow-1.6.11 examples/memory_limit_watcher.rb
dynflow-1.6.10 examples/memory_limit_watcher.rb
dynflow-1.6.8 examples/memory_limit_watcher.rb
dynflow-1.6.7 examples/memory_limit_watcher.rb
dynflow-1.6.6 examples/memory_limit_watcher.rb
dynflow-1.6.5 examples/memory_limit_watcher.rb
dynflow-1.6.4 examples/memory_limit_watcher.rb
dynflow-1.6.3 examples/memory_limit_watcher.rb
dynflow-1.6.2 examples/memory_limit_watcher.rb
dynflow-1.6.1 examples/memory_limit_watcher.rb
dynflow-1.4.9 examples/memory_limit_watcher.rb
dynflow-1.4.8 examples/memory_limit_watcher.rb
dynflow-1.5.0 examples/memory_limit_watcher.rb
dynflow-1.4.7 examples/memory_limit_watcher.rb
dynflow-1.4.6 examples/memory_limit_watcher.rb
dynflow-1.4.5 examples/memory_limit_watcher.rb