Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9004223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:51:19+00:00 2026-06-16T00:51:19+00:00

I have a method like this that goes through an array to find different

  • 0

I have a method like this that goes through an array to find different APIs and launch a delayed_job instance for every API found like this.

def refresh_users_list
  apis_array.each do |api| 
    api.myclass.new.delay.get_and_create_or_update_users
  end
end

I have an after_filter on users#index controller to trigger this method. This is creating many jobs to be triggered that will eventually cause too many connections problems on Heroku.

I’m wondering if there’s a way I can check for the presence of a Job in the database by each of the API that the array iterates. This would be very helpful so I can only trigger a particular refresh if that api wasn’t updated on a given time.

Any idea how to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:51:20+00:00Added an answer on June 16, 2026 at 12:51 am
    1. In config/application.rb, add the following

      config.autoload_paths += Dir["#{config.root}/app/jobs/**/"]
      
    2. Create a new directory at app/jobs/.

    3. Create a file at app/jobs/api_job.rb that looks like

      class ApiJob < Struct.new(:attr1, :attr2, :attr3)
      
        attr_accessor :token
      
        def initialize(*attrs)
      
          self.token = self.class.token(attr1, attr2, attr3)
        end
      
        def display_name
          self.class.token(attr1, attr2, attr3)
        end
      
        #
        # Class methods
        #
        def self.token(attr1, attr2, attr3)
          [name.parameterize, attr1.id, attr2.id, attr3.id].join("/")
        end
      
        def self.find_by_token(token)
          Delayed::Job.where("handler like ?", "%token: #{token}%")
        end
      end
      

      Note: You will replace attr1, attr2, and attr3 with whatever number of attributes you need (if any) to pass to the ApiJob to perform the queued task. More on how to call this in a moment

    4. For each of your API’s that you queue some get_and_create_or_update_users method for you’ll create another Job. For example, if I have some Facebook api model, I might have a class at app/jobs/facebook_api_job.rb that looks like

      class FacebookApiJob < ApiJob
        def perform
          FacebookApi.new.get_and_create_or_update_users(attr1, attr2, attr3)
        end
      end
      

      Note: In your Question you did not pass any attributes to get_and_create_or_update_users. I am just showing you where you would do this if you need the job to have attributes passed to it.

    Finally, wherever your refresh_users_list is defined, define something like this job_exists? method

    def job_exists?(tokens)
      tokens = [tokens] if !tokens.is_a?(Array) # allows a String or Array of tokens to be passed
    
      tokens.each do |token|
        return true unless ApiJob.find_by_token(token).empty?
      end
    
      false
    end
    

    Now, within your refresh_users_list and loop, you can build new tokens and call job_exists? to check if you have queued jobs for the API. For example

    # Build a token
    
    def refresh_users_list
      apis_array.each do |api| 
        token = ApiJob.token(attr1, attr2, attr3)
        next if job_exists?(token)
    
        api.myclass.new.delay.get_and_create_or_update_users
      end
    end
    

    Note: Again I want to point out, you won’t be able to just drop in the code above and have it work. You must tailor it to your application and the job’s you’re running.


    Why is this so complicated?

    From my research, there’s no way to “tag” or uniquely identify a queued job through what delayed_job provides. Sure, each job has a unique :id attribute. You could store the ID values for each created job in some hash somewhere

    {
      "FacebookApi": [1, 4, 12],
      "TwitterApi": [3, 193, 44],
      # ...
    }
    

    and then check corresponding hash key for an ID, but I find this limiting, and not always sufficient for the problem When you need to identify a specific job by multiple attributes like above, we must create a way to find these jobs (without loading every job into memory and looping over them to see if one matches our criteria).

    How is this working?

    The Struct that the ApiJob extends has a :token attribute. This token is based on the attributes passed (attr1, attr2, attr3) and is built when a new class extending ApiJob is instantiated.

    The find_by_token class method simply searches the string representation of the job in the delayed_job queue for a match based on a token built using the same token class method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that goes through loads of file types like so: case
I have a method that looks like this: public static String escape(String text) {
For example I have a method that looks like this [self performSelectorOnMainThread:@selector(someMethod) withObject:data waitUntilDone:YES];
Let's say that I have a method that looks like this: def raise_if_client_status_error(xml_resp) #
I have a method on an interface that looks like this and I want
I have a method in an assembly that looks like this: namespace MyNameSpace {
I have a method in a url rewriting module that looks like this public
I have a controller method that is a little something like this: def suggestions
I have a spring bean that I get like this within a method- repAppCaller
I have a method that looks like: T[] field; public Method(IList<T> argument) { this.field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.