# How can I schedule an action?

**URL:** <https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618>\
**Category:** Rails\
**Created:** [March 21, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618 "2008-03-21T23:22:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 21, 2008, 11:22pm UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618/1 "2008-03-21T23:22:49Z")

</div>

I have a background code with backgroundfu:

# Simple, non-monitored worker.

class ReportWorker

def add()  
Report.generate\_rep()  
end

end

I want this code only to run on Fridays and only to run one time every  
Friday. How can I do that?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 22, 2008, 12:25am UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618/2 "2008-03-22T00:25:16Z")

</div>

John S. wrote:

> I have a background code with backgroundfu:
> 
> # Simple, non-monitored worker.
> 
> class ReportWorker
> 
> def add()  
> Report.generate\_rep()  
> end
> 
> end
> 
> I want this code only to run on Fridays and only to run one time every  
> Friday. How can I do that?

Sounds like a job for cron and script/runner. Or even cron and a rake  
task. Doesn’t sound like you need any kind of “background”  
functionality. That would seem like overkill.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 22, 2008, 11:50am UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618/3 "2008-03-22T11:50:36Z")

</div>

How about some kind of loop? Why using cron? Is it the best option? I  
have backgroundfu working.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 22, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618/4 "2008-03-22T19:57:52Z")

</div>

Nathan E. wrote:

> I don’t think backgroundFu is for scheduled tasks. If you use  
> backgroundrb, though, you can just do this:
> 
> class HelloWorker \< BackgrounDRb::MetaWorker  
> set\_worker\_name :hello\_worker
> 
> def create(args = nil)  
> # time argument is in seconds  
> add\_periodic\_timer(604800) { expire\_sessions }  
> end
> 
> def expire\_sessions  
> # expire user sessions  
> end  
> end
> 
> or if you want a more precise scheduling, use can use cron or the unix  
> scheduler through backgroundrb
> 
> [http://backgroundrb.rubyforge.org/scheduling/index.html](http://backgroundrb.rubyforge.org/scheduling/index.html)

For now, what I have done is some kink of loop with Backgroundfu. It  
seems not to use a lot of CPU but I have to test it for some time.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [March 22, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/how-can-i-schedule-an-action/132618/5 "2008-03-22T18:43:31Z")

</div>

I don’t think backgroundFu is for scheduled tasks. If you use  
backgroundrb, though, you can just do this:

class HelloWorker \< BackgrounDRb::MetaWorker  
set\_worker\_name :hello\_worker

def create(args = nil)  
# time argument is in seconds  
add\_periodic\_timer(604800) { expire\_sessions }  
end

def expire\_sessions  
# expire user sessions  
end  
end

or if you want a more precise scheduling, use can use cron or the unix  
scheduler through backgroundrb

[http://backgroundrb.rubyforge.org/scheduling/index.html](http://backgroundrb.rubyforge.org/scheduling/index.html)
