Giter VIP home page Giter VIP logo

capacity_keeper's Introduction

CapacityKeeper stability-wip Build Status

WIP

Overview

If this method call is system-unfriendly

innocent_method

โ†“ By using capacity_keeper gem, this may change to be system-friendly

within_capacity(keeper: TestKeeper) do
  innocent_method
end

Installation

Add this line to your application's Gemfile:

gem 'capacity_keeper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install capacity_keeper

Simple Usage

First, you need to implement keeper class of capacity_keeper. Your keeper class must be inherited CapacityKeeper::Keeper class, and override performable?, begin_process and finish_process abstract methods.

Example of keeper implementation as follows

# CapacityKeeper::Keeper must be inherited.
class TestKeeper < CapacityKeeper::Keeper

  # Define class original config
  set_config :max, 10

  @@counter = 0

  # @override
  def performable?
    # check current counter is not over max value using ":max" config value
    @@counter <= config[:max]
  end

  private

  # @override
  def begin_process
    # enqueue counter
    @@counter += 1
  end

  # @override
  def finish_process
    # dequeue counter
    @@counter -= 1 if @@counter > 0
  end
end

If your process need to keep capacity, include CapacityKeeper module, and use within_capacity method

require 'capacity_keeper'

class Example
  # must be included CapacityKeeper module
  include CapacityKeeper

  def xxx
    # enclose with within_capacity method, and assign keeper class for keeping capacity
    within_capacity(keeper: TestKeeper) do
      # some system-unfriendly method
      innocent_method
    end
  end
end

Exection sequence

  1. Execute performable? of your keeper implementation for capacity satisfation check
  2. If performable,
    1. Execute before of your keeper implementation
    2. Execute your assigned block
    3. Execute after of your keeper implementation

Use dynamic runtime options

If your keeper class want to refer @opts variable, please assign opts argument of within_capacity method calling.

require 'capacity_keeper'

class Example
  include CapacityKeeper

  def xxx
    within_capacity(keeper: TestKeeper, opts: { hello: 'world' }) do
      innocent_method
    end
  end
end

@opts variable can be refered from your keeper class.

class TestKeeper < CapacityKeeper::Keeper

  set_config :max, 10

  @@counter = 0

  # @override
  def performable?
    puts @opts.inspect  # @opts variable can be refered => { hello: 'world' }
    @@counter <= config[:max]
  end

  private

  # @override
  def begin_process
    @@counter += 1
  end

  # @override
  def finish_process
    @@counter -= 1 if @@counter > 0
  end
end

Capacity keeping with multi keepers

You can assign multi keepers by calling add_keeper method.

within_capacity(keeper: KeeperA).add_keeper(KeeperB).add_keeper(KeeperC) do
  # applied KeeperA and KeeperB and KeeperC
  innocent_method
end

Exection sequence

  1. Execute performable? of KeeperA for capacity satisfation check
  2. Execute performable? of KeeperB for capacity satisfation check
  3. Execute performable? of KeeperC for capacity satisfation check
  4. If performable on all keeperList,
    1. Execute before of KeeperA
    2. Execute before of KeeperB
    3. Execute before of KeeperC
    4. Execute your assigned block
    5. Execute after of KeeperA
    6. Execute after of KeeperB
    7. Execute after of KeeperC

Add some keepers under specific conditions

if you want to add some keepers under specific conditions, you can use CapacityKeeper::KeeperList#perform method.

str = 'C'

keeper_list = within_capacity(keeper: KeeperA)

if str == 'B'
  keeper_list.add_keeper(KeeperB)
end

if str == 'C'
  keeper_list.add_keeper(KeeperC)
end

keeper_list.perform do
  # applied KeeperA and KeeperC
  innocent_method
end

Configurations

Default keeper configuration on your app using CapacityKeeper.configure

CapacityKeeper.configure do |config|
  config.retry_count = 5

  config.retry_interval_second = 5

  config.raise_on_retry_fail = false

  config.logger = ::Logger.new(STDOUT)

  config.verbose = false
end

Override class original configs

class TestKeeper < CapacityKeeper::Keeper

  # Override default configs by class original values
  retry_count 10
  retry_interval_second 10
  raise_on_retry_fail true
  logger TestLogger.new
  verbose true

:
:

Development

Setup

$ git clone [email protected]:goldeneggg/capacity_keeper.git
$ bin/setup

Run tests

$ bundle exec rake spec

Debug using pry

$ bin/console

To release a new version, update the version number in version.rb.

Generate new CHANGELOG.md

$ bin/changelog

Contributing

Bug reports and pull requests are welcome on GitHub at issues

Author

goldeneggg

capacity_keeper's People

Contributors

goldeneggg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.