I have a rails app (v4.2) I'm developing and when a user registers for the site, I'd like to sign them up to my Mailchimp mailing list. I've implemented this process as a background job with sucker_punch and the gibbon gem. Read more here (Rails Mailchimp Background Job Not Implemented Error)
I've got it working using an after_commit call in the user.rb file.
Only issue I'm running into now is it seems ActiveJob will successfully perform this method over and over again until Mailchimp responds with a 400 error that the email already was added.
How can I get ActiveJob or this method to only hit the Mailchimp API once?
Here's my background job:
class AddUserToRegistrationListJob < ActiveJob::Base
queue_as :default
require 'gibbon'
def perform(user_id)
user = User.find(user_id)
email = user.email
first_name = user.first_name
last_name = user.last_name
mailchimp_list_id = "my list id here"
Gibbon::Request.api_key = "my api key here"
Gibbon::Request.timeout = 15
gibbon = Gibbon::Request.new
gibbon.lists("#{mailchimp_list_id}").members.create(
body:
{
email_address: email,
status: "subscribed",
merge_fields: {FNAME: first_name, LNAME: last_name
}})
end
end
My user.rb file:
after_create :add_user_to_registration_list
def add_user_to_registration_list
AddUserToRegistrationListJob.perform_later(self)
end