# Backgroundrb - workers firing twice?

**URL:** <https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309>\
**Category:** Rails\
**Created:** [November 4, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309 "2006-11-04T14:22:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [November 4, 2006, 2:22pm UTC](https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309/1 "2006-11-04T14:22:06Z")

</div>

I’ve got backgroundrb handling three periodic tasks. For some reason,  
each of them is firing twice on each run. From my backgroundrb.log:

Daily stats in production at Sat Nov 04 03:00:41 EST 2006  
Daily stats in production at Sat Nov 04 03:00:41 EST 2006  
11 expired sessions being deleted at Sat Nov 04 03:03:41 EST 2006  
0 expired sessions being deleted at Sat Nov 04 03:03:41 EST 2006  
Database backup in production at Sat Nov 04 03:33:41 EST 2006  
Database backup in production at Sat Nov 04 03:33:41 EST 2006

You get the idea.

Each of the workers has some variation of the following:

MINUTES\_TO\_KEEP = 15  
repeat\_every MINUTES\_TO\_KEEP.minutes  
first\_run Time.now

My backgroundrb.yml config file has:

autostart:  
1:  
job\_key: session\_cleanup1  
class: session\_cleanup\_worker  
2:  
job\_key: database\_backup1  
class: database\_backup\_worker  
3:  
job\_key: log\_statistics  
class: daily\_stats\_worker

Does anybody have an idea why this is happening, and what I can do to  
stop it?

–Al Evans

---

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [November 5, 2006, 1:56am UTC](https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309/2 "2006-11-05T01:56:09Z")

</div>

Al Evans wrote:

> Does anybody have an idea why this is happening, and what I can do to  
> stop it?

I didn’t figure out why this was happening, but here’s a workaround in  
case anybody else runs into the same problem:

LOCK\_NAME = “#{RAILS\_ROOT}/log/whatever.lock”

def lock\_file  
return false if File.exists?(LOCK\_NAME)  
f = File.open(LOCK\_NAME, “w”)  
f.close  
true  
end

def clear\_lock\_file  
File.delete(LOCK\_NAME)  
end

def do\_work(args)  
if lock\_file  
  
clear\_lock\_file  
end  
end

–Al Evans–

---

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [March 19, 2007, 12:09am UTC](https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309/3 "2007-03-19T00:09:44Z")

</div>

I have a very similar setup and I can confirm behavior. Not sure why the  
workers fire twice. Here’s a solution that doesn’t rely upon files:

@@running=false

def do\_work(args)  
if not @@running  
@@running=true  
  
@@running=false  
end  
end

---

<div class="post-metadata">

**Author:** ![al-evans](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/54ee81/32.png) [@al-evans](https://www.ruby-forum.com/u/al-evans)\
**Post date:** [August 29, 2008, 12:05pm UTC](https://www.ruby-forum.com/t/backgroundrb-workers-firing-twice/74309/4 "2008-08-29T12:05:38Z")

</div>

Chris S. wrote:

> I have a very similar setup and I can confirm behavior. Not sure why the  
> workers fire twice. Here’s a solution that doesn’t rely upon files:
> 
> @@running=false
> 
> def do\_work(args)  
> if not @@running  
> @@running=true  
>   
> @@running=false  
> end  
> end

Thanks Chris it’s really a very good solution.
