Gem Version example workflow

DeployPin

DeployPin

Sometimes we need to execute set of commands (tasks) after/before deployment. Most likely you use migrations for such things, but that is not what is related to migration at all. Also sometimes you need to execute some code before migration or later, after migration without blocking of main thread.

deploy_pins is exactly what you need.

Usage

DeployPin

To generate new task template file

rails g deploy_pin:task some_task_title
# or
rails g deploy_pin:task some_task_title --parallel

Also, you can specify author

rails g deploy_pin:task some_task_title -a author_name

To list all pending tasks

rake deploy_pin:list

To run all pending tasks

rake deploy_pin:run

Groupped tasks

Please define allowed groups in config/initializers/deploy_pin.rb if you want to group tasks around "allowed_group"

rails g deploy_pin:task task_title -g allowed_group
# or
rails g deploy_pin:task task_title -g allowed_group --parallel

To list all pending tasks

rake deploy_pin:list[allowed_group]

To run all pending tasks

rake deploy_pin:run[allowed_group]

Run by uuid

To run some specific task by uuid

rake deploy_pin:run['uuid_1, uuid_2']

Or you can combine uuid and group

rake deploy_pin:run['uuid, allowed_group']

In case if you want to rerun task you should add exclamation mark in the end of uuid

rake deploy_pin:run['uuid_1!, uuid_2!']

Installation

Add this line to your application's Gemfile:

gem 'deploy_pin'

And then execute:

$ bundle

Or install it yourself as:

$ gem install deploy_pin

then generate configuration file

rails g deploy_pin:install

and run migration

rake db:migrate

Database Timeout

By default, deploy_pin will run all the non-parallel tasks under a database statement timeout.
A default value must be defined in the deploy_pin initializer. Ex.:

# config/initializers/deploy_pin.rb
DeployPin.setup do
  statement_timeout 0.2.second # 200 ms
end 

In order to not use the default value, it's required to use explicitly in the task, like:

# Some deploy_pin task 
# 20190401135040:I
# task_title: Execute some query with timeout

# === task code goes down here ===
DeployPin::Database::execute_with_timeout do
 ActiveRecord::Base.connection.execute("select * from shipments;")
end

To know more about the params, please check the documentation here.

Parallel

To run parallel tasks using timeout, it's required to use the parallel wrapper, which mimics parallel interface,
but adding the timeout option.

In a deploy_pin task, instead of using Parallel.each(1..2, in_processes: 2), use:

parallel_each(1..2, in_processes: 2, timeout: 0.3.seconds) do |i|
  # ActiveRecord::Base.connection_pool.with_connection it's already include in the parallel wrapper.
  puts "Item: #{i}, Worker: #{Parallel.worker_number}"
  ActiveRecord::Base.connection.execute("<some db query>")
end

Check the documentation here.

Contributing

Contribution directions go here.

License

The gem is available as open source under the terms of the MIT License.