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

  • SEARCH
  • Home
  • 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 8548213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:26:32+00:00 2026-06-11T13:26:32+00:00

I have a background operation I would like to occur every 20 seconds in

  • 0

I have a background operation I would like to occur every 20 seconds in Rails given that some condition is true. It kicked off when a certain controller route is hit, and it looks like this

def startProcess
    argId = self.id
    t = Thread.new do
        while (Argument.isRunning(argId)) do
            Argument.update(argId)
            Argument.markVotes(argId)
            puts "Thread ran"
            sleep 20
        end
    end
end

However, this code does absolutely nothing to my database unless I call "t.join" in which case my whole server is blocked for a long time (but it works).

Why can’t the read commit ActiveRecords without being joined to the main thread?
The thread calls methods that look something like

def sample
  model = Model.new()
  model.save()
end

but the models are not saved to the DB unless the thread is joined to the main thread. Why is this? I have been banging my head about this for hours.

EDIT:

The answer marked correct is technically correct, however this edit is to outline the solution I eventually used. The issues is that Ruby does not have true threading, so even once I got my DB connection working the Thread couldn’t get processor time unless there was little traffic to the server.

Solution: start a new Heroku worker instance, point it at the same database, and make it execute a rake task that has the same functionality as the thread. Now everything works great.

  • 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-11T13:26:34+00:00Added an answer on June 11, 2026 at 1:26 pm

    You need to re-establish the database connection:

    ActiveRecord::Base.establish_connection Rails.env
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text field entry in my view that I would like to
I have a long operation running in the background, like uploading stuff, converting images,
Quick background I have a concurrent map I used to cache some values that
I would like to write a .NET application that runs in the background (stays
I have to do a background process which will perform some operations.. while this
I have background music for some songs available in both .MID and .KAR formats,
I have this background image that is 175x175 but I am trying to make
I have a background image that is 1024 x 1024 pixels; the reason for
I have a background worker thread that is constantly syncing data to/from a remote
I would like to perform the operation stated in the title. Code works absolutely

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.