Follow Me Icons

 

Follow @KendrickColeman on TwitterConnect on LinkedInWatch My Videos on YouTubeFollow me on FacebookCheck Out My Projects on GitHubStay Up To Date with RSS

Search

BSA 728x90 Center Banner

Creating a worker process on Cloud Foundry with Clockwork

I just got finished writing a Rails application that requires a 'cron'-like job to run every 30 seconds. This job will scrape a website for data every 30 seconds and update the database and notify me via SMS about any changes. I was able to get this all working on Heroku after a while with the assistance of some Procfiles. but I wanted to try this on Cloud Foundry as well.

 

The problem existed because of lots of out-of-date documentation on the part of Cloud Foundry. The most confusing part was not understanding this requires 2 containers/applications. One for the app, and another for running the clockwork task. In Heroku, this requires 2 Dynos as well because 1 is for 'web' and the other is for 'clock', however, this is all masked behind Heroku's magic.

 

So how do you create a worker process? I was going back and forth creating rake tasks, then tried putting logic into my controller, and having everything ultimately fail. After some trial and error, I found out the best & easiest way is to put all the logic into the Modal. Once the logic is in your modal, you can easily call it by doing Modal.method.

 

Second, add the clockwork gem to your Gemfile, then create the file "lib/clock.rb". Follow the gist below to configure your clock.rb file. Specify the Modal.method that you wish to call.

 

Great! now we just need to build our manifest.yml file for Cloud Foundry. The important thing to remember is that we are essentially going to spin up 2 applications. The first container will be the web front-end of our rails app, and the second container is just the clockwork reoccurring job. If your reoccurring process will need to make changes to the database (which it most certainly will) make sure you append the correct services. In this case, both of my containers will be using the same database.

 

 

NOTE: If you are running your rails app for the first time, don't forget to change the command to "command: bundle exec rake db:create db:migrate db:seed && bundle exec rails s -p $PORT". This will create and seed your database if necessary.

 

Files are ready to go... all that you have to do now is is "cf push". DO NOT do "cf push application-name" because it will not run the secondary container. If you have changed your manifest.yml file in anyway do "cf push --reset".

 

During the "cf push" process, do not assign a domain to the clockwork process. This would create an unnecessary route and the application may fail because Cloud Foundry can't reach it.

 

**UPDATE 12/7/14**

I updated to the new CF CLI and removed the CF Gem and now I can no longer push multiple applications from a manifest file. To push the cronjob dyno you must specify the app, no-route, and no-manifest such as this:

cf push mycron --no-route --no-manifest

 

View the Gist here: https://gist.github.com/kacole2/5d79a0519b5f2d0c6155

 

 

now you should have two apps running in Cloud Foundry 

 

Related Items