# Jobs scheduling

**URL:** <https://www.ruby-forum.com/t/jobs-scheduling/139443>\
**Category:** Rails\
**Created:** [June 3, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443 "2008-06-03T23:27:30Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 3, 2008, 11:27pm UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/1 "2008-06-03T23:27:30Z")

</div>

I know, that my question is easy for you, but not for me, because I’m  
beginner with RoR.  
So…  
My application sends a lot of emails - not only for confirm  
registration, change password, but others. I take time if I do this by  
request.  
My application also creates many files - it takes a time too…  
Is there a good way for sending email in background?  
Is there a good way for scheduling jobs?

Thanks for help?

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 3, 2008, 11:32pm UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/2 "2008-06-03T23:32:57Z")

</div>

Two best I have personally found are backgroundrb and spawn:

[http://spawn.rubyforge.org/svn/spawn/](http://spawn.rubyforge.org/svn/spawn/)  
[http://backgroundrb.rubyforge.org/](http://backgroundrb.rubyforge.org/)

With spawn, scheduling a background (deferred) task is about as simple  
as can be:

spawn do  
# do whatever you want in here (rails environment is loaded)  
end

If you want recurring job or more finetuned control, you can use  
backgroundrb which is as simple as

1. generate a worker
2. code the worker to do a task
3. Invoke the worker task with the syntax (roughly):

MiddleMan(:worker\_name).task\_name(args)

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 4, 2008, 12:56am UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/3 "2008-06-04T00:56:40Z")

</div>

Thanks.  
I found this:  
[railspikes.com&nbsp;-&nbsp;This website is for sale!&nbsp;-&nbsp;railspikes Resources and Information.](http://railspikes.com/2008/6/3/asynchronous-railsconf-2008),  
It’s interesting, but not everything is clear for me…  
Maybe someone know more about this solutions?

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 4, 2008, 1:49am UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/4 "2008-06-04T01:49:09Z")

</div>

Nathan - spawn works perfect 😉  
Thanks a lot!  
Do you know something about cornedit?

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 5, 2008, 7:04am UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/5 "2008-06-05T07:04:59Z")

</div>

Anka,

Emails are really sent in the background since they will be queued by  
sendmail or similar. If you plan on sending a ton of emails in one  
request, I would suggest setting something up with crontab to execute at  
timed intervals.

You can create a model and put your logic in there and then add a  
crontab entry

## RAILS\_ROOT/lib/my\_script\_model.rb

class MyScriptModel \< ActiveRecord::Base  
def self.some\_method  
Generate all the emails here  
end  
end

## Crontab

RAILS\_ENV=production  
0 \* \* \* \* some\_user RAILS\_ROOT/script/runner MyScriptModel.some\_method

Something along these lines should get you started.

There are certainly other solutions, but I think this would be fairly  
easy to get going.

-Fredrik

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 5, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/6 "2008-06-05T14:14:57Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

On Thursday 05 June 2008 09:06:22 Anka Gr wrote:

> Generally the idea is clear, but tell me, how my some\_method invoked  
> in crontab knows which email should be send?  
> Should I use e.g. table jobs, where I put information which email and  
> to whom it should me send? And some\_method should checking this jobs?

That’s usually what I do when cron is sufficient, yes. I like  
the “queued\_” prefix for table names for this kind of thing,  
e.g. “queued\_emails”.

Ciao,  
Sheldon.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQFIR9hvpGJX8XSgas0RAm3vAJ99xDdoFplsGdzZf44LRhXXRO0v0QCgok8y  
niUZR8OwRDVM6XrhBcIm0sw=  
=O1aW  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![anka](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/5f9b8f/32.png) [@anka](https://www.ruby-forum.com/u/anka)\
**Post date:** [June 5, 2008, 9:06am UTC](https://www.ruby-forum.com/t/jobs-scheduling/139443/7 "2008-06-05T09:06:22Z")

</div>

Fredrik,

I said, that emails are sent in the background so why request is longer  
than when I use spawn? It can be other reason?

and question about crontab:  
Generally the idea is clear, but tell me, how my some\_method invoked in  
crontab knows which email should be send?  
Should I use e.g. table jobs, where I put information which email and to  
whom it should me send? And some\_method should checking this jobs?

It’s hard to explain and asking in English 😉
