├── Gemfile ├── Gemfile.lock ├── Procfile ├── Readme.md ├── config.ru ├── cron.d └── example.rb ├── public └── index.html └── worker.rb /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | ruby "2.1.5" 4 | 5 | gem "rufus-scheduler" 6 | gem "rack" 7 | 8 | group :development do 9 | gem "heroku" 10 | gem "foreman" 11 | gem "pry-byebug" 12 | end 13 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | GEM 2 | remote: https://rubygems.org/ 3 | specs: 4 | addressable (2.3.7) 5 | byebug (3.5.1) 6 | columnize (~> 0.8) 7 | debugger-linecache (~> 1.2) 8 | slop (~> 3.6) 9 | coderay (1.1.0) 10 | columnize (0.9.0) 11 | debugger-linecache (1.2.0) 12 | dotenv (1.0.2) 13 | excon (0.44.3) 14 | foreman (0.77.0) 15 | dotenv (~> 1.0.2) 16 | thor (~> 0.19.1) 17 | heroku (3.27.2) 18 | heroku-api (~> 0.3.19) 19 | launchy (>= 0.3.2) 20 | multi_json (~> 1.10.1) 21 | netrc (>= 0.10.0) 22 | rest-client (= 1.6.7) 23 | rubyzip (= 0.9.9) 24 | heroku-api (0.3.22) 25 | excon (~> 0.38) 26 | multi_json (~> 1.8) 27 | launchy (2.4.3) 28 | addressable (~> 2.3) 29 | method_source (0.8.2) 30 | mime-types (2.4.3) 31 | multi_json (1.10.1) 32 | netrc (0.10.3) 33 | pry (0.10.1) 34 | coderay (~> 1.1.0) 35 | method_source (~> 0.8.1) 36 | slop (~> 3.4) 37 | pry-byebug (3.0.1) 38 | byebug (~> 3.4) 39 | pry (~> 0.10) 40 | rack (1.6.0) 41 | rest-client (1.6.7) 42 | mime-types (>= 1.16) 43 | rubyzip (0.9.9) 44 | rufus-scheduler (3.0.9) 45 | tzinfo 46 | slop (3.6.0) 47 | thor (0.19.1) 48 | thread_safe (0.3.4) 49 | tzinfo (1.2.2) 50 | thread_safe (~> 0.1) 51 | 52 | PLATFORMS 53 | ruby 54 | 55 | DEPENDENCIES 56 | foreman 57 | heroku 58 | pry-byebug 59 | rack 60 | rufus-scheduler 61 | -------------------------------------------------------------------------------- /Procfile: -------------------------------------------------------------------------------- 1 | web: bundle exec ruby worker.rb 2 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | CRONY 2 | ===== 3 | 4 | Crony is a template project to implement a cron like scheduler. [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler) is used for scheduling and [foreman](https://github.com/ddollar/foreman) is used for running the scheduler. This makes it perfect for a deployment on [heroku](http://heroku.com)'s [Caledon Cedar](http://devcenter.heroku.com/articles/cedar) stack. 5 | 6 | 7 | Usage 8 | ----- 9 | 10 | Fork/clone this project, install the gems needed using bundler: 11 | 12 | ```bash 13 | bundle install 14 | ``` 15 | 16 | and start the scheduler using foreman: 17 | 18 | ```bash 19 | bundle exec foreman start 20 | ``` 21 | 22 | Your crony won't do much yet (except for printing it's heartbeat), so add some cron jobs now. And better delete that example job before you use it seriously. 23 | 24 | 25 | Adding Cron Jobs 26 | ---------------- 27 | 28 | Each (non hidden) file in the `cron.d` directory will be evaluated and can be used to schedule jobs. Refer to [rufus-scheduler](https://github.com/jmettraux/rufus-scheduler)'s documentation on how to schedule jobs. Be aware that your `cron.d` files will be evaluated in the context of the scheduler instance, so you can access all methods the scheduler has. Here's an example: 29 | 30 | ```ruby 31 | every "5m" do 32 | # do something every five minutes 33 | end 34 | ``` 35 | 36 | You can have as many jobs as you wish within one file. Separating them in several files is just a way to organize and group your jobs for better overview. 37 | 38 | Within your `cron.d` files, you can do anything: Requiring other files/gems (remember to put them into your `Gemfile` first), defining classes/methods etc. This way, you can do anything from sending emails to pinging other sites of yours (eg using the fabulous [httpi](https://github.com/rubiii/httpi)) to start a task there or to keep them alive when the hoster shuts them down after a certain time of inactivity. 39 | 40 | 41 | Heroku Deployment 42 | ----------------- 43 | 44 | After you've added your jobs and helpers, you're ready to deploy your jobs to heroku: 45 | 46 | ```bash 47 | # create an application on the Caledon Cedar stack 48 | heroku create 49 | 50 | # tell heroku not to install the gems you only need for your local development/testing 51 | heroku config:add BUNDLE_WITHOUT=development 52 | 53 | # deploy your code 54 | git push heroku master 55 | ``` 56 | 57 | After this, your private crony instance will happily be running and executing your jobs. 58 | 59 | If you want to pause the scheduler, just scale your worker down to 0: 60 | 61 | ```bash 62 | heroku ps:scale web=0 63 | ``` 64 | 65 | To restart, scale it back up to 1 again. 66 | 67 | Be aware that heroku cycles your processes every 24h meaning that your crony instance will be restarted once daily. There shouldn't be any problems, though - unless this collides exactly with a job you've scheduled. 68 | 69 | 70 | Future Ideas 71 | ------------ 72 | 73 | * Implement database backed (instead of file based) job configuration that would allow for management of jobs via the command line (eg rake tasks) without the need to redeploy the app. 74 | 75 | 76 | Contributing 77 | ------------ 78 | 79 | If you want to contribute changes you've made, helper scripts or anything else, please fork this project and send a pull request with your changes. 80 | 81 | Preferably, you keep the master branch in your fork clean and create another branch to add your own jobs and push that to heroku. This way, you can keep the master branch clean from personal files and make those pull requests easier for both of us. Just rebase your personal branch on top of master and use `-f` when pushing to heroku. 82 | 83 | 84 | License 85 | ------- 86 | 87 | [MIT](http://www.opensource.org/licenses/mit-license.php) 88 | 89 | Copyright (c) 2011 Thomas Jachmann 90 | 91 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 92 | 93 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 94 | 95 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 96 | -------------------------------------------------------------------------------- /config.ru: -------------------------------------------------------------------------------- 1 | use Rack::Static, :root => "public", :index => "index.html" 2 | run Rack::File.new("public") 3 | -------------------------------------------------------------------------------- /cron.d/example.rb: -------------------------------------------------------------------------------- 1 | every "1s" do 2 | puts "<3" 3 | end 4 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 |read more at github.com/thomasjachmann/crony
28 | 29 |