# Execute Action at a Specified Time

**URL:** <https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532>\
**Category:** Rails\
**Created:** [October 6, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532 "2007-10-06T23:31:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 6, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532/1 "2007-10-06T23:31:42Z")

</div>

Hey all.  
I’m creating an app that that involves sending emails at a specified  
date-time. I only have one model and it has a “sendat” (:datetime)  
column. I need to send an email to an email address (also in the  
model) at the “sendat” time. I’m not sure how to do this.

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 6, 2007, 11:39pm UTC](https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532/2 "2007-10-06T23:39:17Z")

</div>

On Oct 6, 2007, at 16:31 , David wrote:

> I need to send an email to an email address (also in the  
> model) at the “sendat” time.

Run a script/runner via cron job that checks for emails that need to  
be sent and sends them. I’m thinking something along the lines of  
“send emails with sendat \< now that haven’t been sent”.

Michael G.  
grzm seespotcode net

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 11, 2007, 4:17pm UTC](https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532/3 "2007-10-11T16:17:26Z")

</div>

Heres my method in my Item model:

* * *

def self.send\_expired  
items = Item.find(:all, :conditions =\> [“time \<= ?”, Time.now])

for item in items  
email = item.email  
content = item.content

```
time = item.time.strftime("%b %d %I:%M %p")

begin
  Notifier.deliver_item_email(email, content, time)
rescue Exception => e
  logger.error("Error: e.message")
else
  Item.find(item.id).destroy
end

```

end

end  
\*\*

Here’s my Notifier class:  
\*\*  
class Notifier \< ActionMailer::Base  
def item\_email(email,content,time)  
recipients email  
from “removed\_email\_address@domain.invalid”  
subject “You are being notified!”  
body :content =\> content, :time =\> time  
end  
end  
\*\*

Basically, the self.send\_expired method grabs all the Item objects  
that have a time column before the current time, sends the item via  
email, and deletes it from the db.

The first time I ran “script/runner -e development  
‘Item.send\_expired’” it worked fine and it sent all the items that had  
expired. I then created another item object that had a time 5 minutes  
after I saved it to the db. When that time passed (in other words,  
when the item “expired”), no email was sent and the item remained in  
the db. I guess script/runner only called the ‘send\_expired’ method  
when I started it.

I guess I don’t really understand how script/runner works. I thought  
it continuously calls the function, so as soon as an item expires, it  
is sent via email and destroyed. Could someone please explain?

~ David

---

<div class="post-metadata">

**Author:** ![david](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/aeb1de/32.png) [@david](https://www.ruby-forum.com/u/david)\
**Post date:** [October 11, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/execute-action-at-a-specified-time/116532/4 "2007-10-11T18:47:15Z")

</div>

On 10/11/07, David [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I guess I don’t really understand how script/runner works. I thought  
> it continuously calls the function, so as soon as an item expires, it  
> is sent via email and destroyed. Could someone please explain?

No, script/runner just runs your code once and then exits. It doesn’t  
do anything magic.

Michael suggested you run script/runner periodically from cron (every  
5 minutes, or whatever frequency you need). I think that’s the best  
solution.

You could also make your runner script check for mails, then sleep for  
a while, then check again, in an endless loop.
