Skip to content
/ turtle Public

A helper to use workers and topics with Ruby on Rails

License

Notifications You must be signed in to change notification settings

petlove/turtle

Repository files navigation

Run specs

A helper to use workers and topics with Ruby on Rails

Installation

Add this line to your application's Gemfile:

gem 'turtle', github: 'petlove/turtle'

Usage

Queues with priority for shoryuken

Turtle.shoryuken_queues_priorities
# => [["macaw_linquetab_enqueue_triggered_send_credit_earned", 2],
#  ["macaw_linquetab_enqueue_triggered_send_question_answer", 2],
#  ["macaw_linquetab_enqueue_triggered_send_product_back_in_stock", 2],
#  ["macaw_linquetab_perform_order_events", 3],
#  ["macaw_linquetab_perform_order_payment_pending_events", 3],
#  ["macaw_linquetab_perform_shipment_events", 3],
#  ["macaw_linquetab_update_data_extension_reviews", 1],
#  ["macaw_linquetab_update_data_extension_products", 1],
#  ["macaw_linquetab_update_data_extension_animal_pets", 1],
#  ["macaw_linquetab_update_data_extension_orders_carts", 1],
#  ["macaw_linquetab_update_data_extension_customers", 1],
#  ["macaw_linquetab_update_data_extension_customer_coupons", 1],
#  ["macaw_linquetab_update_list_subscribers", 1],
#  ["macaw_linquetab_perform_subscription_events", 3],
#  ["macaw_linquetab_update_data_extension_subscriptions", 1]]

# Filter by queue metadata fields
Turtle.shoryuken_queues_priorities(priority: 3)
# => [["macaw_linquetab_perform_order_events", 3],
#  ["macaw_linquetab_perform_order_payment_pending_events", 3],
#  ["macaw_linquetab_perform_shipment_events", 3],
#  ["macaw_linquetab_perform_subscription_events", 3]]

Name for

# Turtle.name_for(type, name, options)
Turtle.name_for(:queue, 'order_sync')
# => beagle_production_order_sync
Turtle.name_for(:topic, 'order_created')
# => beagle_production_order_created

Options

Key Default
region ENV['AWS_REGION']
prefix ENV['APP_NAME']
environment ENV['APP_ENV']
suffix nil

Default retry intervals

Turtle.retry_intervals
# => [5.minutes, 15.minutes, 30.minutes, 1.hour, 3.hours, 12.hours]

Setting DelayedJob

You should follow this steps:

  1. Add gem 'delayed_job_active_record' in your Gemfile
  2. Run rails generate delayed_job:active_record
  3. Run rails db:migrate
  4. Set in the file config/application.rb this code:
config.active_job.queue_adapter = :delayed_job
  1. Set the file config/initializers/delayed_job.rb with this code:
Delayed::Worker.queue_attributes = Turtle.delayed_job_queue_attributes
  1. Set the supervisor to run DelayedJob with this code:
[program:delayed_job]
command=bundle exec rake jobs:work
user = root
autostart=true
autorestart=true
redirect_stderr=false

See more about DelayedJob here.

Publish in a queue through Shoryuken

The worker should include Shoryuken::Worker and have the option :queue defined.

# Turtle.enqueue!(worker, data, options = {})
Turtle.enqueue!(SomeWorker, { hello: 'world' })

Options

Key Default What's it?
delay false Enqueue the data through DelayedJob process. Pass true to use it.
seconds 0 Use AWS SQS delay. Pass an integer between 0 and 900.
model nil Envolope the data with the field model. It should be like Spree::Order, Subscription or any model name
event nil Envolope the data with the field event. It should be like :created, :completed or any event name

Important: If the fields model or event exists, the data will be enveloped like this code:

{
  event: 'order_created',
  model: 'spree_order',
  data: {
    hello: 'world'
  }
}

Publish in a topic

# Turtle.publish!(name, data, options = {})
Turtle.publish!('product_event_created', { hello: 'world' })

Options

Key Default What's it?
delay false Enqueue the data through DelayedJob process. Pass true to use it.
model nil Envolope the data with the field model. It should be like Spree::Order, Subscription or any model name
event nil Envolope the data with the field event. It should be like :created, :completed or any event name

Important: If the fields model or event exists, the data will be enveloped like this code:

{
  event: 'order_created',
  model: 'spree_order',
  data: {
    hello: 'world'
  }
}

Using event notificator

include Turtle::EventNotificator

act_as_notification model: 'order',
                    enveloped: true,
                    serializer: OrderEventSerializer,
                    serializer_options: { root: false },
                    serializer_root: :data,
                    states: %i(pending completed),
                    state_column: :state,
                    actions: %i(created updated destroyed),
                    delayed: %i(created updated completed)

The topic name that will be publicated follows the structure:

"#{ENV['APP_NAME']}_#{ENV['APP_ENV']}_#{model}_event_#{event_raised}"
# => kangaroo_production_order_event_created

And the content will be:

# Enveloped
{
  event: event,
  model: model,
  data: OrderEventSerializer.new(self)
}.to_json
# => {
#   "event": "created",
#   "model": "order",
#   "data": {
#     "hello": "world"
#   }
# }

# Not enveloped
OrderEventSerializer.new(self).to_json
# => {
#   "hello": "world"
# }

Options

Key Default Required What's it?
model nil true The model name.
serializer nil true The serializer used in the payload.
serializer_options {} false The serializer options.
serializer_root nil false The serializer root field. If nil will be returned the original root.
enveloped true false If true it allows to envelope the payload.
states [] false The states name list. It will publish in a topic if the state was changed.
state_column :state false The state column name.
actions [] false The actions name list. It will publish in a topic all times that the event happens. It allows the values %i(created updated destroyed).
delayed [] false The events that you would like performing with delay. It requires DelayedJob. E.g: %i(created updated completed)

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

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

About

A helper to use workers and topics with Ruby on Rails

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published