ruby-on-railsmailchimprails-activejobgibbonsucker-punch

Rails ActiveJob Background Job Keeps Pinging Mailchimp Repeatedly


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

Solution

  • The Gibbon interface has been updated again for v3 of the MailChimp API, and there is a new capability to 'upsert' in a one liner. From the RubyDoc:

    If you want to upsert instead, you would do the following:
    
     gibbon.lists(list_id).members(lower_case_md5_hashed_email_address).upsert(body: {email_address: "foo@bar.com", status: "subscribed", merge_fields: {FNAME: "First Name", LNAME: "Last Name"}})
    

    To acquire the hash, this code should work:

    require 'digest'
    lower_case_md5_hashed_email_address = Digest::MD5.new.update(email.downcase).hexdigest
    

    This should resolve your error by ensuring the upsert command is a no-op if the member is already subscribed to the list in question.