Record status along with other relevant information of transactions or tasks. These tasks can be a cron job, large background jobs or a simple method. Any task can be plugged into a transaction block. Transaction uses Redis to store the current status along with other information. The events within the transaction block can be published via Pubsub client (ex. Pusher, PubNub or any valid pubsub client).These events can be subscribed in the client app for the live status of the transaction.

rails-gem, redis, ruby-gem, rubygems, transaction, transaction-management
gem install transaction -v 1.0.0


Gem Version Build Status Maintainability Test Coverage Depfu


Transaction is a small library which helps track the status of running/upcoming tasks. These tasks can be a cron job, background jobs or a simple method. Any task can be plugged into a transaction block.

Transaction uses Redis to store the current status along with the additional attributes(provided during the initialization or transaction update.)

The events within the transaction block can be published via Pubsub client(ex. Pusher, PubNub or any valid pubsub client). These events can be subscribed in the client app for the live status of the transaction.



Valid Pubsub client if messages need to be published.


Add this line to your application's Gemfile:

gem 'transaction'

And then execute:

$ bundle

Or install it yourself as:

$ gem install transaction


Initializing Transaction gem -

In your rails app - create config/transaction.rb

  # Ex. pusher looks like | any pubsub client can be used.
  require 'pusher'
  PubsubClient =
    key: "app_key",
    secret: "app_secret",
    cluster: "cluster_info",
    use_tls: true

  Transaction.configure do |config|
    config.redis = # defaults to localhost
    config.pubsub_client = {client: PubsubClient, trigger: 'trigger'}

There's an option to directly connect as well.

  Transaction.redis = 'redis://redis_url:6379')
  Transaction.pubsub_client = {client: PubsubClient, trigger: 'trigger'}

The pubsub client accepts the following options -

param description
client Any valid pubsub client. Ex. Pusher, PubNub.
trigger The method name which sends a message to a pubsub client. Ex. 'trigger' for Pusher. 'publish' for PubNub.
channel_name (Optional) Channel in which the message be published. Defaults to the value of transaction_id.
event (Optional) Event for which message will be published. Defaults to 'status'

Initializing a transaction

A transaction can be newly initialized or be found with the given transaction_id. If no transaction is found then a new transaction is created.

  attributes = {created_at:, count: 0 }
  transaction = attributes) # creates a new instance of transaction
  transaction1 = transaction.transaction_id) # finds the transaction.

Accepted methods for a transaction

The default status of any new transaction is queued. Accepted statuses: ['queued', 'processing', 'success', 'error']. Any transaction at any point will be in one of the states.

method params description
start! - moves to status processing from queued. Publishes {message: Processing} to pubsub client if enabled.
finish! (status: 'success', clear: false, data: {}) moves to the passed status (default: success). Any additional data passed is merged with default {message: 'Done}'. clear = true destroy the transaction entry from Redis cache.
clear! - destroys the current entry from Redis cache.
refresh! - sync current instance transaction with the latest cache (Other instance of a transaction initiated with same transaction id can update the attributes. Hence refresh! is required.).
update_status status moves to the passed status. Raises 'Invalid Status' error if the status is not in one of the Accepted statuses defined above.
update_attributes options = {} merges the passed options hash to the current attributes object.
trigger_event! data = {} publishes the data via pubsub. Current status along with-param data is published(Note: Pubsub client needs to be configured.)

Ex 1: Simple transaction

    def sum_numbers
      arr = (0...10_000).to_a
      options = { created_at:, total: arr.count  }
      transaction = options)

      puts transaction.status # Status moves from `queued` to `processing`

      count = 0
      (1..10_000).each do |i|
        # do some other stuff
        transaction.update_attributes(count: count += 1)
        # do some other stuff

      transaction.finish! # By default moves to status 'success'.

      puts transaction.status # 'success'
      puts transaction.attributes # {:status=>:success, :created_at=>2019-07-19 06:06:43 +0530, :total=>10000, :count=>10000}

Ex 2: Initialize or find a transaction with a transaction id.

    def task1
      transaction =
      SomeWorkerJob.perform_later(transaction.transaction_id) # sidekiq or resque

    class SomeWorkerJob < ApplicationJob
      queue_as :default

      def perform transaction_id
        tr = transaction_id) # intialize with given transaction_id

        # do a bunch of stuff

Keeping transactions in sync.

Let's say we have 2 transactions t1 and t2 both initialized with the same transaction id. If t2 updates the transaction, then t1 can simply refresh the transaction to get in sync with t2. Note: the transaction will be refreshed with the most recent values. (Versioning transaction updates ??? => Woah that's a nice PR idea.)

  def task1
    transaction =

    puts transaction.status # 'processing'
    puts transaction.status # 'error'

  def task2 transaction_id # in some other context altogether. Task 2 is not at all related to task 1.
    transaction = transaction_id)
    # do some stuff

Pushing messages to client via pubsub

If a task is running in the background and the client needs to know the status. PubSub can be configured to do so.

   # configure pubsub client as defined above in Initializing Transaction gem

   def long_task
    transaction =

    items = (0..10_000).to_a

    items.each do |index|
      transaction.trigger_event!(count: index, total: items.count)
      # do something

    transaction.finish!(data: {count: items.count, total: items.count})


After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to


Bug reports and pull requests are welcome on GitHub at This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.


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

Code of Conduct

Everyone interacting in the Transaction project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.