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.