Rails Delayed Job
Posted by dyn in ruby on rails on March 25th, 2010
When building a web application, you always try to make it fast and responsive. Sometimes this task can be difficult because of some long running requests like: file uploads, sending email, updating sphinx indexes, running reports, etc.
Enter Delayed Job.
First, we must install the plugin and migrate the delayed_job table that handles the work:
1 | script/plugin install git://github.com/collectiveidea/delayed_job.git |
Then we create the table:
1 2 | script/generate delayed_job rake db:migrate |
Now we are ready to enqueue jobs. Let’s say we want to send an email with delayed_job:
1 2 3 | Emailer.deliver_contact(@comments) #old way Emailer.send_later(:deliver_contact, @comments) #delayed_job way |
Now you check your email and you can’t find anything. That’s because we need to run the delayed_job daemon.
1 | script/delayed_job start |
Now you can see how the daemon sends the email. Instead of the send_later method, you can “mark” a method to always run in background:
1 2 3 4 5 6 7 8 9 | class Photo def generate_thumbnails #generate all the thumbnails end handle_asynchronously :generate_thumbnails end @photo = Photo.new(params[:photo]) @photo.generate_thumbnails #this will run throught delayed_job |
Now you need to run delayed_job in your production environment, with capistrano you should have something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | namespace :delayed_job do desc "Start delayed_job process" task :start, :roles => :app do run "cd #{current_path}; RAILS_ENV=production script/delayed_job start" end desc "Stop delayed_job process" task :stop, :roles => :app do run "cd #{current_path}; RAILS_ENV=production script/delayed_job stop" end desc "Restart delayed_job process" task :restart, :roles => :app do run "cd #{current_path}; RAILS_ENV=production script/delayed_job restart" end end after "deploy:start", "delayed_job:start" after "deploy:stop", "delayed_job:stop" after "deploy:restart", "delayed_job:restart" |
If there’s an error when launching the daemon in production, that might be because you need the daemons gem. I had problems with the daemons 1.0.11 so I went for the ghazel-daemons and everything worked fine.
We highly recommend to use monit to monitor and restart the delayed_job process in case something goes wrong. You can check other options like custom jobs and configuration parameters in the docs.
GrailsCertification.com is now online!
Our new site: www.grailscertification.com is now online!
In this site, users can test their knowledge of the Grails Web framework.
There are 5 types of certifications: rookie, junior groovy, groovy, guru and holy grail, each of them with different levels.
Users can also submit questions to collaborate with our question database. Our aim is to have the biggest Grails question database online!
What are you waiting for? Test your skills right now: www.grailscertification.com
Grails and EJB2.1 integration
If you are working on a Grails project and you want to use existing business logic from EJB2.1 inside your application, you can easily achive this using Spring.
The first step is to include the Beans you want to use, inside your conf/spring/resources.groovy file like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | beans = { ejbJndi(org.springframework.jndi.JndiTemplate){ environment = [ "java.naming.factory.initial" : "org.jnp.interfaces.NamingContextFactory", "java.naming.provider.url" : "jnp://localhost:1099"] } businessBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){ jndiName = "EJBBusinessBean" businessInterface = "com.my.ejb.EJBBusinessBean" jndiTemplate = ref("ejbJndi") } personBean(org.springframework.ejb.access.SimpleRemoteStatelessSessionProxyFactoryBean){ jndiName = "EJBPerson" businessInterface = "com.my.ejb.EJBPerson" jndiTemplate = ref("ejbJndi") } } |
Then you can use any of the defined Beans inside your services or controllers like this:
1 2 3 4 5 6 7 | class MyService { def personBean //spring injects the bean automatically def myMethod(){ personBean.list() } } |
That’s it!
Just remember to include the required JAR files (the EJB jars and the jars required to connect to the container, for instance, JBoss)
SEO Friendly URLs
Let’s say you have a site which has movie reviews. To display a unique review, you would have a PHP page, for example review.php, which obtains the id of the review and shows the title and description of that review. So, if you want to see the review with id 45, you need to access the following URL: http://yoursite.com/review.php?id=45
Do you see a problem with this? No? Are you sure? Ok, then I’ll have to explain it. With that type of URL, you are not able to know anything about the review. It could be a review for “Casablanca” just as easily as it could a review for “Ace Ventura: Pet Detective”. And that’s not the biggest problem; it is also a bad SEO URL!
How can you fix this? Use SEO Friendly URLs.
Requirements: Apache (with mod_rewrite module) installed on your server (most hosting services already have this).
Step 1: Insert the following code on you PHP script
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function StrToSearchFriendlyURL($s) { if(!$s) return 'page'; $s = strtolower(htmlentities($s, ENT_QUOTES, $GLOBALS['CHARSET'])); $s = preg_replace('/&(.)(?:acute|cedil|circ|ring|tilde|uml|grave|elig|slash);/', '\\1', $s); $s = preg_replace('/\W+/', '_', html_entity_decode($s)); $s = preg_replace('/_{2,}/','_',$s); $s = trim($s,'_'); if(!$s) $s = 'page'; return $s; } |
Step 2: Apply the above function to your regular URLs to make them SEO Friendy.
Step 3: When inserting the new URLs on your page, add a dash (-), the review id and html extension at the end.
Example:
1 | <a href="{SEO_friendly_URL}-{id}.html">{$review_title}</a> |
Step 4: Create a .htaccess file with the following content.
1 2 3 4 | Options +FollowSymLinks RewriteEngine On RewriteRule ^[^-]+-([0-9]+)\.html$ review.php?id=$1 [L] |
Step 5: Place the .htaccess file on your site’s document root.
That’s it! The URLs on your site will now look something like this: http://yoursite.com/casablanca-45.html which will be interpreted by Apache like http://yoursite.com/review.php?id=45
More about mod_rewrite: http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
More about .htaccess files: http://httpd.apache.org/docs/1.3/howto/htaccess.html
Grails Tips Part I – Domain Classes
I have been working a lot with Grails lately and I have to say that I love it!
It has allowed me to create some great web applications in a very fast and reliable way. While developing these applications, I learned a lot of useful things that I would like to share to you. Today I’ll give you some tips regarding Domain Classes.
1) Date created & Last Updated
If you want Grails to handle the date in which an object is created and the last time it was updated, all you have to do is add the following attributes to your domain class:
1 2 | Date dateCreated Date lastUpdated |
As easy as that! Grails will automatically set the value for this two attributes when an object is created and every time it is updated.
2) Field order
If you want Grails to generate the fields for the create, edit & show views in a particular order, just define that order on the constraints. If the attribute doesn’t require a specific constraint (such as maxSize, unique, nullable, etc), just put the attribute on the constraints and leave the constraint empty.
1 2 3 4 5 6 7 8 | static constraints = { name() address() city(nullable:true, blank:true) phone(nullable:true, blank:true) mobile(nullable:true, blank:true) email(email:true) } |
3) Unique by group
If you want an attribute on a domain class to be unique, but only grouped by another attribute, you can do it. In the following example, the company name will be unique only for companies from the same client:
1 2 3 4 5 6 7 8 9 | class Company { Client client String name String address static constraints = { name(size:3..50, blank:false, unique:'client') address() } } |
4) Change database mapping for String attribute
Grails will map the String attributes on your domain class to varchar(255) by default. If you want a String attribute mapped to text, here’s how you do it:
1 2 3 4 5 | static mapping = { columns{ attribute_name type:"text" } } |
Stay tuned for more Grails tips!