Rails ActiveJob and Sidekiq - best practices.

103 views
Skip to first unread message

Juraj Chlebec

unread,
Jan 26, 2017, 5:31:16 AM1/26/17
to Sidekiq
I have two application on two different server.

First application:
- only create jobs

Second application:
- execute jobs through sidekiq workers

On first application i have installed and configured sidekiq only as client and my job looks like:

# app/jobs/core/zone_access_log_job.rb
class
Core::ZoneAccessLogJob < ApplicationJob
  queue_as :default

  def perform(visited_at, key, access_from, access_type, identifier, data: {})
   
# Fake job class for ActiveJob/Sidekiq job creation
  end
end

On second application is real job class looks like:

# app/jobs/core/zone_access_log_job.rb
class Core::ZoneAccessLogJob < ApplicationJob
  queue_as :default

  def perform(visited_at, key, access_from, access_type, identifier, data: {})
   
zone    = Core::Zone.joins(project: [:space]).includes(project: [:space]).find_by(code: key)
   
project = zone.project
   
space   = zone.project.space

   
# Create new log access to extended log.
    log                 = ZoneAccess::ExtendedLog.new

   
# When zone is accessed.
    log.visited_at      = visited_at

    # Accessed zone code.
    log.zone_code       = key

    # Access from api, page_provider.
    log.access_from     = access_from
    # Access type 404, api, page, redirect, poi...
    log.access_type     = access_type

    # Project, space and zone associations.
    log.project         = project
    log.space           = space
    log.zone            = zone

    # Who access to zone.
    log.identifier      = identifier
    log.identifier_data = data

    # Save data to database.
    log.save!
 
end
end

My question: Is it this approach correct or maybe i use different method for this type of task.

Thanks

Juraj

Mike Perham

unread,
Jan 26, 2017, 11:21:24 AM1/26/17
to sid...@googlegroups.com
Yes, some people do this.  If you use the Sidekiq native APIs, you can skip the fake class on the client side.

Sidekiq::Client.push(“class” => "Core::ZoneAccessLogWorker”, “args” => […])

Note the class is a String, it doesn’t have to exist in the client’s codebase.

--
You received this message because you are subscribed to the Google Groups "Sidekiq" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sidekiq+unsubscribe@googlegroups.com.
To post to this group, send email to sid...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sidekiq/836db79d-221e-480f-96e4-a474a0d53a16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Mike Perham – CEO, Contributed Systems
Smart, effective open source infrastructure for your apps.

Juraj Chlebec

unread,
Jan 26, 2017, 11:51:40 AM1/26/17
to Sidekiq
Thanks, 

Juraj

Dňa štvrtok, 26. januára 2017 17:21:24 UTC+1 Mike Perham napísal(-a):
To unsubscribe from this group and stop receiving emails from it, send an email to sidekiq+u...@googlegroups.com.

To post to this group, send email to sid...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sidekiq/836db79d-221e-480f-96e4-a474a0d53a16%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages