WIP
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
Add this line to your application's Gemfile:
gem 'capacity_keeper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install capacity_keeper
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
- Execute
performable?
of your keeper implementation for capacity satisfation check - If performable,
- Execute
before
of your keeper implementation - Execute your assigned block
- Execute
after
of your keeper implementation
- Execute
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
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
- Execute
performable?
of KeeperA for capacity satisfation check - Execute
performable?
of KeeperB for capacity satisfation check - Execute
performable?
of KeeperC for capacity satisfation check - If performable on all keeperList,
- Execute
before
of KeeperA - Execute
before
of KeeperB - Execute
before
of KeeperC - Execute your assigned block
- Execute
after
of KeeperA - Execute
after
of KeeperB - Execute
after
of KeeperC
- Execute
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
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
:
:
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
Bug reports and pull requests are welcome on GitHub at issues