0.01
Low commit activity in last 3 years
No release in over a year
Gem to provide serialization and deserialization of Dry::Struct arguments for Sidekiq jobs
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 12.0
~> 3.0

Runtime

 Project Readme

Sidekiq::Dry

Gem version Build Status

Gem to provide serialization and deserialization of Dry::Struct arguments for Sidekiq jobs.

Read more about this gem here.

Rationale

Argument Validation

By using Dry::Struct arguments you can choose to validate some attributes and mark any of them as optional. An attempt to enqueue a job with invalid arguments will raise, minimizing resource waste from jobs which would otherwise be retried multiple times without any chance of completing successfully.

Example:

class Coupons::ApplyCouponJob::Params < Dry::Struct
  attribute :user_id,     Types::Strict::Integer
  attribute :coupon_code, Types::Strict::String
end

job_params = Coupons::ApplyCouponJob::Params.new(user_id: user.id, coupon_code: coupon.code)

Users::SendConfirmationEmailJob.perform_async(job_params)

Documentation

Instead of documenting the types of each job argument, which can easily become outdated, you can refer to the types of the attributes of the struct.

Positional Arguments are Error Prone

You won't ever have to remember the order of arguments of a job.

Versioning

Adding this gem does not break any existing jobs in your app. It only works on jobs enqueued with Dry::Struct objects.

Adding a new attribute to a parameter struct won't break already enqueued jobs.

It's trivial to version your structs using either a version attribute:

class Coupons::ApplyCouponJob::Params < Dry::Struct
  attribute :user_id,     Types::Strict::Integer
  attribute :coupon_code, Types::Strict::String
  attribute :version,     Types::Strict::String.default('1')
end

or versioned classes:

class Coupons::ApplyCouponJob::Params::V1 < Dry::Struct
  attribute :user_id,     Types::Strict::Integer
  attribute :coupon_code, Types::Strict::String
end

Installation

Add this line to your application's Gemfile:

gem 'sidekiq-dry'

And then execute:

$ bundle install

Configure Sidekiq to use the middlewares provided by this gem:

# File: config/initializers/sidekiq.rb
Sidekiq.configure_client do |config|
  config.client_middleware do |chain|
    chain.prepend Sidekiq::Dry::Client::SerializationMiddleware
  end
end

Sidekiq.configure_server do |config|
  # Ensure jobs enqueued by other jobs serialize their structs
  config.client_middleware do |chain|
    chain.prepend Sidekiq::Dry::Client::SerializationMiddleware
  end

  config.server_middleware do |chain|
    chain.add Sidekiq::Dry::Server::DeserializationMiddleware
  end
end

See Middleware usage on the Sidekiq wiki for more info.

Usage

Enqueuing Jobs

class Users::SendConfirmationEmailJob::Params < Dry::Struct
  attribute :email, Types::Strict::String
end

job_params = Users::SendConfirmationEmailJob::Params.new(email: user.email)

Users::SendConfirmationEmailJob.perform_async(job_params)

Authoring Jobs

class Users::SendConfirmationEmailJob
  include Sidekiq::Worker

  def perform(params)
    # params is an instance of Users::SendConfirmationEmailJob::Params
    mail(to: params.email, subject: 'Welcome!')
  end
end

Development

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 rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/zorbash/sidekiq-dry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

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

Code of Conduct

Everyone interacting in the Sidekiq::Dry project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.