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 9061389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:28:21+00:00 2026-06-16T15:28:21+00:00

I have a lib class (Updater) that do some long tasks, this tasks can

  • 0

I have a lib class (“Updater”) that do some long tasks, this tasks can be launched manually (in the browser) or every 2 hours (I have implemented whenever gem, that does a schedule every 2 hours of this task).

Seen that this tasks make a lot of work on the DB, I think that some concurrence errors can occurs (if for example calling when is yet working).
Have I right?

I have thought a solution with mutex, having a pseudo code for my Updater class like this:

module Updater
  def start
    #do some job
  end
end

A correct solution, I think, is something like this

module Updater
  def start
    mutex.lock
       #do some job
    mutex.unlock
  end
end

Is my solution correct?

Can please provide some more informations about concurrence (for example how to use correctly mutex in Rails, what I must require, etc..)?
I have searched but found nothing with good explanation.

  • 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-16T15:28:22+00:00Added an answer on June 16, 2026 at 3:28 pm

    I think the correct way to use a Ruby Mutex is obtain a lock, run a code block, and then release the lock again, i.e. to call lock in combination with unlock…

    @mutex = Mutex.new
    ..
    
    def start
      @mutex.lock
      begin
        ..
      ensure
        @mutex.unlock rescue nil
      end
    end
    

    or to use the synchronize method:

    @mutex = Mutex.new
    ..
    
    def start
      @mutex.synchronize do
        # do something
      end
    end
    

    An example can be found in the Rack Middleware class Rack::Lock. But I am not sure if it helps in your case, even if you use class variables like @@mutex, because the mutex/semaphore may not be preserved between different tasks and distinct processes (I assume that the “tasks” are different processes started by “rake tasks”). The Mutex class is useful to achieve thread safety, since it implements a simple semaphore that can be used to coordinate access to shared data from multiple concurrent threads. However, there is also a nice RailsCast about Thread-Safety (unfortunately only behind a paywall).

    In your case it may help to create a global lock flag in the database, or to create a global lock file in the filesystem with touch lock.txt and to remove it again with rm lock.txt when the process is over. It is possible to execute these shell commands in Ruby with Kernel.system or %x. If the file exists File.exists?("lock.txt") then the update can be suspended.

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

Sidebar

Related Questions

Lets say I have this class in foobar-shared.lib: class FooBar { std::string m_helloWorld; }
Hey we have a library class (lib/Mixpanel) that calls delayed job as follows: class
So I have this code inside my lib/ folder: class GlobalConfig::SetHelper def self.yes_no_input(configuration) value
I have a simple class that defines some constants, e.g.: module Foo class Bar
I have a model that looks like this: class Base(models.Model): pass class Mixin1(Base): active
It seems interesting, that nobody asked this before. I have some basic-questions about the
I have a class that looks like this: class SearchService include Mongoid::Document key :name,
I have a eclipse workspace with 3 subprojects myMp3TagBusineslogic (lib that can be used
Suppose I have three projects in my sln. (1) xyz.a{Class Lib}{no reference added} (2)
I have a class Test which is in lib folder and I want to

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.