0
0
Ruby on Railsframework~20 mins

Scheduled jobs in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Scheduled Jobs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a scheduled job is enqueued with Active Job and Sidekiq?

Consider a Rails app using Active Job with Sidekiq as the backend. If you schedule a job with MyJob.set(wait: 5.minutes).perform_later, what will happen?

Ruby on Rails
class MyJob < ApplicationJob
  queue_as :default

  def perform
    puts "Job executed"
  end
end

MyJob.set(wait: 5.minutes).perform_later
AThe job is scheduled to be pushed to Sidekiq after 5 minutes, then executed.
BThe job is scheduled in Sidekiq but will never execute because Sidekiq does not support delayed jobs.
CThe job is immediately pushed to Sidekiq and executed right away.
DThe job is executed immediately in the current process without Sidekiq involvement.
Attempts:
2 left
💡 Hint

Think about how Active Job and Sidekiq handle delayed execution.

📝 Syntax
intermediate
2:00remaining
Which syntax correctly schedules a recurring job with the 'whenever' gem?

You want to run a rake task every day at 2am using the 'whenever' gem. Which syntax in schedule.rb is correct?

A
every :day, at: '2:00 am' do
  rake 'my:task'
end
B
every 1.day, at: '02:00' do
  rake 'my:task'
end
C
every 1.day, at: '2:00 am' do
  rake 'my:task'
end
D
every :day, at: '02:00 am' do
  rake 'my:task'
end
Attempts:
2 left
💡 Hint

Check the symbol and time format used by 'whenever' for scheduling.

🔧 Debug
advanced
2:00remaining
Why does this scheduled job never run in production?

Given this Sidekiq scheduled job code, why might the job never execute in production?

Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :default

  def perform
    # cleanup logic
  end
end

# Scheduling the job
CleanupJob.set(wait_until: Time.now + 1.hour).perform_later
AThe job is scheduled with <code>wait_until</code> but this option is not supported by Active Job.
BThe job is scheduled but the perform method is empty, so Sidekiq skips execution.
CThe job is scheduled correctly but the queue name is wrong, so Sidekiq ignores it.
DSidekiq is not running in production, so scheduled jobs are never processed.
Attempts:
2 left
💡 Hint

Check if the background worker process is active in production.

state_output
advanced
2:00remaining
What is the state of the job after calling perform_later with a delay?

In a Rails app using Active Job with Sidekiq, what is the state of the job immediately after calling MyJob.set(wait: 10.minutes).perform_later?

Ruby on Rails
class MyJob < ApplicationJob
  def perform
    puts "Running job"
  end
end

MyJob.set(wait: 10.minutes).perform_later
AThe job is saved in the database but not enqueued until manually triggered.
BThe job is enqueued in Sidekiq's scheduled set and will run after 10 minutes.
CThe job is enqueued in Sidekiq's default queue and runs immediately.
DThe job is executed immediately in the current thread and then enqueued.
Attempts:
2 left
💡 Hint

Think about how Sidekiq handles delayed jobs internally.

🧠 Conceptual
expert
3:00remaining
Which approach ensures a scheduled job runs exactly once even if the server restarts?

You want to schedule a job to run once at a specific time, and ensure it does not run multiple times if the server restarts or the job is retried. Which approach is best?

ASchedule the job with Active Job's <code>set(wait_until: time)</code> and trust Sidekiq to handle duplicates.
BUse Sidekiq's <code>perform_in</code> method to schedule the job and rely on Sidekiq's retry mechanism.
CUse a database-backed scheduler like <code>sidekiq-scheduler</code> with unique job constraints to prevent duplicates.
DManually check in the job's perform method if it has run before by querying Redis keys.
Attempts:
2 left
💡 Hint

Consider how to prevent duplicate jobs in distributed systems.