Skip to content

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 fieldsTurtle.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]]

Processing groups for shoryuken

To use Processing Groups, add groups: to your aws-sqs-configurator config:

queues:
- name: 'orders_queue'metadata:
priority: 2
- name: 'payments_queue'metadata:
priority: 1groups:
critical:
concurrency: 1queues:
- orders_queue
- payments_queue
Turtle.shoryuken_groups# => "{\"critical\":{\"concurrency\":1,\"queues\":[[\"app_production_orders_queue\",2],[\"app_production_payments_queue\",1]]}}"

In config/shoryuken.yml:

queues: <%= Turtle.shoryuken_queues_priorities %>groups: <%= Turtle.shoryuken_groups %>

Name for

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

Options

KeyDefault
regionENV['AWS_REGION']
prefixENV['APP_NAME']
environmentENV['APP_ENV']
suffixnil

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

KeyDefaultWhat's it?
delayfalseEnqueue the data through DelayedJob process. Pass true to use it.
seconds0Use AWS SQS delay. Pass an integer between 0 and 900.
modelnilEnvolope the data with the field model. It should be like Spree::Order, Subscription or any model name
eventnilEnvolope 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

KeyDefaultWhat's it?
delayfalseEnqueue the data through DelayedJob process. Pass true to use it.
modelnilEnvolope the data with the field model. It should be like Spree::Order, Subscription or any model name
eventnilEnvolope 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

includeTurtle::EventNotificatoract_as_notificationmodel: 'order',enveloped: true,serializer: OrderEventSerializer,serializer_options: {root: false},serializer_root: :data,states: %i(pendingcompleted),state_column: :state,actions: %i(createdupdateddestroyed),delayed: %i(createdupdatedcompleted)

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 envelopedOrderEventSerializer.new(self).to_json# => {# "hello": "world"# }

Options

KeyDefaultRequiredWhat's it?
modelniltrueThe model name.
serializerniltrueThe serializer used in the payload.
serializer_options{}falseThe serializer options.
serializer_rootnilfalseThe serializer root field. If nil will be returned the original root.
envelopedtruefalseIf true it allows to envelope the payload.
states[]falseThe states name list. It will publish in a topic if the state was changed.
state_column:statefalseThe state column name.
actions[]falseThe actions name list. It will publish in a topic all times that the event happens. It allows the values %i(created updated destroyed).
delayed[]falseThe 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

Stars

4 stars

Watchers

44 watching

Forks

Releases

Packages

Used by

Contributors

Languages