How does Delayed Job work in Ruby on Rails ?

前端 未结 2 1061
灰色年华
灰色年华 2021-02-06 06:22

I am new to this and is little confused about how Delayed Job works ?

I know it creates a table and puts the jobs in the table and then I need to run

r         


        
相关标签:
2条回答
  • 2021-02-06 06:36
    1. Does DJ script checks the table every minute and when the time matches job_at time, it runs that job ?

    When you run rake jobs:work DelayedJob will poll the delayed_jobs table, performing jobs matching the job_at column value if it's been set. This part you're correct about.

    1. How it is different than cron (whenever gem) if the script is just checking the table every min ?

    whenever is a gem that helps you configure a crontab. It has nothing directly to do with performing tasks on your server on a periodic basis.

    You could setup a cron to run whatever tasks exist in the queue every minute, but leaving a delayed_job daemon running has multiple benefits.

    • Even if the cron ran every minute, delayed_job's daemon will see and perform any jobs queued within that 1-minute window between cron runs
    • Every time the cron would run, it will rebuild a new Rails environment in which to perform the jobs. This is a waste of time and resources when the daemon can just sit there immediately ready to perform a newly queued job.

    If you want to configure delayed_job through a cron every minute you can add something like this to your crontab

    * * * * * RAILS_ENV=production script/delayed_job start --exit-on-complete
    

    Every minute, delayed_job will spin up, perform whatever jobs are ready for it or which it must retry from a previously failed run, and then quit. I don't recommend this though. Setting up a delayed_job as a daemon is the right way to go.

    0 讨论(0)
  • 2021-02-06 06:45

    Does DJ script checks the table every minute and when the time matches job_at time, it runs that job ?

    yes. It checks the database every 5 seconds.

    How it is different than cron (whenever gem) if the script is just checking the table every min ?

    In the context of background jobs, they are not that different. Their main difference is how they usually run the jobs.

              DJ                  |            Crontab
     uses additional database     | you should either set up a rake task
     table but that's it. easier  | or a runner which can be called on the
     to code compared to crontab  | crontab
    ------------------------------|------------------------------------------
     requires you to run a worker | requires you to setup your cron which
     that will poll the database  | you can easily do using the whenever gem
    ------------------------------|------------------------------------------
     since this uses a table, it  | you have to setup some sort of logging so
     is easier to debug errors    | that you have an idea what caused the error
     when they happen             |
    ------------------------------|------------------------------------------
     the worker should always be  | as long as your crontab is set up properly,
     running to perform the job   | you should have no issues
    ------------------------------|------------------------------------------
     harder to setup recurring    | easy to setup recurring tasks
     tasks                        |
    ------------------------------|------------------------------------------
    
    0 讨论(0)
提交回复
热议问题