Bird
Raised Fist0
Ruby on Railsframework~20 mins

Job creation and queuing in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Job Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a job is enqueued with perform_later?

Consider a Rails Active Job class SendEmailJob. When you call SendEmailJob.perform_later(user_id), what is the immediate behavior?

Ruby on Rails
class SendEmailJob < ApplicationJob
  queue_as :default

  def perform(user_id)
    # send email logic
  end
end

SendEmailJob.perform_later(42)
AThe job runs immediately and blocks the current thread until done.
BThe job runs synchronously but in a new thread.
CThe job is discarded because perform_later is not a valid method.
DThe job is added to the queue and will run asynchronously later.
Attempts:
2 left
💡 Hint

Think about how perform_later differs from perform_now.

📝 Syntax
intermediate
1:30remaining
Identify the correct syntax to set a custom queue in a job class

Which of the following correctly sets the queue name to :mailers in a Rails Active Job class?

Ruby on Rails
class NotificationJob < ApplicationJob
  # set queue here
  queue_as :mailers

  def perform
    # job code
  end
end
Aqueue_as :mailers
Bself.queue = :mailers
Cset_queue :mailers
Dqueue_name :mailers
Attempts:
2 left
💡 Hint

Look for the official method to assign queue names in Active Job.

🔧 Debug
advanced
2:30remaining
Why does this job never run after enqueuing?

Given the following job and code, why does the job never execute?

Ruby on Rails
class CleanupJob < ApplicationJob
  queue_as :default

  def perform
    puts "Cleaning up..."
  end
end

CleanupJob.perform_later
AThe job is enqueued but no queue adapter is configured to process jobs.
BThe job class is missing the <code>perform</code> method.
CThe <code>perform_later</code> method requires an argument.
DThe job runs immediately and finishes before output can be seen.
Attempts:
2 left
💡 Hint

Check if the background job processor is running or configured.

❓ state_output
advanced
1:30remaining
What is the output after running this job with arguments?

What will be printed when the following job is enqueued and performed?

Ruby on Rails
class GreetJob < ApplicationJob
  def perform(name)
    puts "Hello, #{name}!"
  end
end

GreetJob.perform_now("Alice")
AError: wrong number of arguments
BHello, Alice!
CHello, !
DNo output
Attempts:
2 left
💡 Hint

Consider how perform_now works with arguments.

🧠 Conceptual
expert
3:00remaining
Which queue adapter supports job retries natively in Rails?

Among the default Rails queue adapters, which one supports automatic job retries without extra configuration?

AInline adapter
BAsync adapter
CSidekiq adapter
DTest adapter
Attempts:
2 left
💡 Hint

Think about which adapter is designed for production background processing with retry features.

Practice

(1/5)
1. What is the main purpose of creating a job in Rails using ApplicationJob?
easy
A. To run tasks in the background without blocking the main app flow
B. To create database tables automatically
C. To handle user authentication
D. To style the user interface

Solution

  1. Step 1: Understand the role of jobs in Rails

    Jobs are designed to run tasks asynchronously, so they don't slow down the main app.
  2. Step 2: Identify the correct purpose

    Running tasks in the background matches the job's purpose, unlike database creation or UI styling.
  3. Final Answer:

    To run tasks in the background without blocking the main app flow -> Option A
  4. Quick Check:

    Jobs = background tasks [OK]
Hint: Jobs run tasks later to keep app fast [OK]
Common Mistakes:
  • Thinking jobs create database tables
  • Confusing jobs with UI styling
  • Assuming jobs handle user login
2. Which of the following is the correct way to enqueue a job named SendEmailJob to run later?
easy
A. SendEmailJob.perform_later
B. SendEmailJob.run_later
C. SendEmailJob.enqueue
D. SendEmailJob.perform_now

Solution

  1. Step 1: Recall the method to queue jobs in Rails

    The method perform_later adds the job to the queue for later execution.
  2. Step 2: Compare options

    perform_now runs immediately, enqueue and run_later are not valid Rails job methods.
  3. Final Answer:

    SendEmailJob.perform_later -> Option A
  4. Quick Check:

    Use perform_later to queue jobs [OK]
Hint: Use perform_later to queue jobs [OK]
Common Mistakes:
  • Using perform_now instead of perform_later
  • Using non-existent methods like enqueue or run_later
  • Confusing immediate execution with queuing
3. Given this job definition:
class CleanupJob < ApplicationJob
  def perform
    puts 'Cleaning up!'
  end
end

What will happen when you call CleanupJob.perform_later?
medium
A. Prints 'Cleaning up!' immediately
B. Adds the job to the queue and prints 'Cleaning up!' later
C. Raises an error because perform_later is undefined
D. Does nothing because perform is empty

Solution

  1. Step 1: Understand perform_later behavior

    Calling perform_later queues the job to run asynchronously later, not immediately.
  2. Step 2: Check the perform method

    The perform method prints 'Cleaning up!' when the job runs, so this will happen later after queuing.
  3. Final Answer:

    Adds the job to the queue and prints 'Cleaning up!' later -> Option B
  4. Quick Check:

    perform_later queues job, runs perform later [OK]
Hint: perform_later queues job, runs perform method later [OK]
Common Mistakes:
  • Thinking perform_later runs perform immediately
  • Assuming perform_later is undefined
  • Believing perform method is empty and does nothing
4. Identify the error in this job code:
class NotifyUserJob < ApplicationJob
  def perform(user_id)
    User.find(user_id).notify
  end
end

NotifyUserJob.perform_later
medium
A. Cannot inherit from ApplicationJob
B. Wrong method name; should be perform_now
C. User model does not exist
D. Missing argument when calling perform_later

Solution

  1. Step 1: Check perform method parameters

    The perform method requires one argument: user_id.
  2. Step 2: Check perform_later call

    Calling perform_later without arguments causes an error because user_id is missing.
  3. Final Answer:

    Missing argument when calling perform_later -> Option D
  4. Quick Check:

    perform_later needs all perform arguments [OK]
Hint: Always pass perform arguments to perform_later [OK]
Common Mistakes:
  • Calling perform_later without required arguments
  • Confusing perform_later with perform_now
  • Assuming inheritance from ApplicationJob is invalid
5. You want to create a job that sends a welcome email only if the user has verified their email. Which is the best way to implement this in perform?
hard
A. Raise an error if user is not verified inside perform
B. Send email unconditionally in perform and handle verification elsewhere
C. Check user.verified? inside perform and send email only if true
D. Call perform_now instead of perform_later

Solution

  1. Step 1: Understand conditional logic in jobs

    Jobs can include logic to decide whether to perform actions, like checking if user is verified.
  2. Step 2: Evaluate options

    Check user.verified? inside perform and send email only if true correctly checks verification inside perform before sending email, ensuring only verified users get emails.
  3. Final Answer:

    Check user.verified? inside perform and send email only if true -> Option C
  4. Quick Check:

    Use condition inside perform for selective job actions [OK]
Hint: Put conditions inside perform to control job actions [OK]
Common Mistakes:
  • Sending emails without checking verification
  • Raising errors unnecessarily in perform
  • Confusing perform_now with perform_later
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard