Ruby
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Methods and Blocks
-
Section 4: Object-Oriented Ruby
-
Section 5: Metaprogramming
-
Section 6: Working with Data and Files
-
Section 7: Ruby Frameworks Overview
-
Section 8: Ecosystem and Testing
-
Section 9: Practical Projects
-
Section 10: Interview Practice
-
Section 11: Data Structures and Algorithms in Ruby
-
Section 12: More Practice Exercises
-
Section 13: Enumerable and Functional Style
-
Section 14: More OOP Practice
-
Section 15: More Testing
-
Section 16: Enumerable and Comparable Modules In Depth
-
Section 17: Ruby Standard Library: Core Utilities
-
Section 18: Ruby Standard Library: Data and Security
-
Section 19: Ruby Standard Library: CLI and Text
-
Section 20: Ruby Networking
-
Section 21: Ruby on Rails Deep Dive
-
Section 22: Ruby Metaprogramming Deep Dive
-
Section 23: Ruby Design Patterns
-
Section 24: Ruby Concurrency
-
Section 25: Ruby Testing Deep Dive
-
Section 26: Ruby Gems and Packaging
-
Section 27: Ruby Performance
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Rails API Development
-
Section 31: Rails Authentication and Authorization
-
Section 32: Rails Testing Deep Dive
-
Section 33: Rails Performance
-
Section 34: Rails Deployment
-
Section 35: Sinatra and Lightweight Ruby Web Apps
-
Section 36: More Ruby Language Deep Dive
-
Section 37: Ruby 3.x Modern Features
-
Section 38: More Data Structures in Ruby
-
Section 39: More Practical Projects
-
Section 40: Ruby Ecosystem Tools
-
Section 41: More Practice and Review
-
Section 42: Final Practice and Mastery
-
Section 43: Ruby Interview Deep Dive
-
Section 44: Ruby Background Processing Deep Dive
-
Section 45: Ruby GraphQL
-
Section 46: Ruby Object Model Deep Dive
-
Section 47: Ruby Hanami Framework Overview
-
Section 48: Ruby gRPC and Protocol Buffers
-
Section 49: Ruby Data Processing
-
Section 50: Ruby Search Integration
-
Section 51: Ruby File Upload and Media
-
Section 52: Ruby Email and Notifications
-
Section 53: Ruby Admin Panels
-
Section 54: Ruby Feature Flags and Experimentation
-
Section 55: Ruby Monitoring and Observability
-
Section 56: Ruby Docker and Deployment Deep Dive
-
Section 57: Ruby Security Deep Dive
-
Section 58: More Advanced Metaprogramming
-
Section 59: More Final Projects
185: Scheduling Recurring Jobs with Whenever
At some point in every professional project, you'll need something to happen automatically on a schedule. Maybe it's clearing out a cache, sending a weekly digest email, or syncing data from a third-party API. When I first started out, my instinct was to just jump straight into the server's crontab. It feels like the "pure" way to do it, but in a team environment, it's a recipe for disaster.
The fragility of manual crontab entries
Imagine we're building a system that sends a "Weekly Activity Summary" to users every Monday at 8:00 AM. The naive approach is to SSH into your production server, run crontab -e, and drop in a line like this:
0 8 * * 1 /bin/bash -l -c 'cd /var/www/my_app/current && RAILS_ENV=production bundle exec rake summary:send_weekly'
On the surface, it works. But here is where the friction starts. First, cron syntax is notoriously cryptic—one misplaced asterisk and your job runs every minute instead of every Monday, potentially spamming your entire user base. Second, and more importantly, this configuration lives exclusively on the server. It isn't in Git. When a new developer joins the team and sets up their local environment, or when you spin up a new staging server, that schedule doesn't exist. You have to remember to manually copy-paste it, which is exactly how "it works in production but not in staging" bugs are born.
Treating your schedule as code
This is why I use the whenever gem. It doesn't actually replace cron; instead, it gives us a Ruby DSL (Domain Specific Language) to describe our schedule in a file that lives right inside our application repository. We stop writing bash strings and start writing Ruby.
Once you add gem 'whenever', require: false to your Gemfile and run the initializer, you get a config/schedule.rb file. That same weekly summary task now looks like this:
every :monday, at: '8am' do
rake "summary:send_weekly"
end
I love this because it's human-readable. I don't have to double-check a cron cheat sheet to remember if the fifth field is the day of the week or the month. More importantly, your schedule is now version-controlled. If we decide to move the email to Tuesday at 10:00 AM, I make a pull request, the team reviews it, and it gets deployed along with the rest of the code.
The trade-off in deployment
Now, there is a small catch: whenever doesn't magically "run" in the background. It is a translator. It reads your schedule.rb and writes the actual crontab entries for you. This means you have to integrate the update step into your deployment pipeline.
If you're using Capistrano, there's a built-in recipe for this. If you're using a simpler script or a CI/CD pipeline, you just need to run bundle exec whenever --update-crontab during the deploy process. It feels like an extra step, but it's a tiny price to pay for the peace of mind that comes with knowing exactly what is scheduled to run across all your environments without having to SSH into a box and pray you didn't typo a cron expression.
📋 Practical Task
Implementing a Daily Database Archive Task
You have been asked to implement a maintenance task that archives old logs every night at 3:00 AM. You already have a Rake task defined as logs:archive_old.
Your goal is to move the scheduling out of the manual system crontab and into the application code using the whenever gem. Perform the following steps:
- Create a
config/schedule.rbfile. - Define a recurring job that runs the
logs:archive_oldrake task every day at 3:00 AM. - Add a second job that runs a custom shell command
/usr/bin/backup_script.shevery Sunday at midnight. - Verify your work by running
bundle exec wheneverin your terminal to see the generated cron output without actually writing it to the system crontab.
There are no comments for now.