Sha256: ccfcd8edc0f5d377b6aaa92a6c7d1abdd5e713b7aa3a413e77956865b6ae3bf2

Contents?: true

Size: 1.74 KB

Versions: 29

Compression:

Stored size: 1.74 KB

Contents

#!/usr/bin/env ruby

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_completion 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

29 entries across 29 versions & 1 rubygems

Version Path
dynflow-1.1.6 examples/memory_limit_watcher.rb
dynflow-1.1.5 examples/memory_limit_watcher.rb
dynflow-1.1.4 examples/memory_limit_watcher.rb
dynflow-1.1.3 examples/memory_limit_watcher.rb
dynflow-1.1.2 examples/memory_limit_watcher.rb
dynflow-1.1.1 examples/memory_limit_watcher.rb
dynflow-1.1.0 examples/memory_limit_watcher.rb
dynflow-1.0.5 examples/memory_limit_watcher.rb
dynflow-1.0.4 examples/memory_limit_watcher.rb
dynflow-1.0.3 examples/memory_limit_watcher.rb
dynflow-1.0.2 examples/memory_limit_watcher.rb
dynflow-1.0.1 examples/memory_limit_watcher.rb
dynflow-1.0.0 examples/memory_limit_watcher.rb
dynflow-0.8.37 examples/memory_limit_watcher.rb
dynflow-0.8.36 examples/memory_limit_watcher.rb
dynflow-0.8.35 examples/memory_limit_watcher.rb
dynflow-0.8.34 examples/memory_limit_watcher.rb
dynflow-0.8.33 examples/memory_limit_watcher.rb
dynflow-0.8.32 examples/memory_limit_watcher.rb
dynflow-0.8.31 examples/memory_limit_watcher.rb