Delayed Job in Rails

SHOULDN’T BE IMPOSSIBLE LIKE THE DOCUMENTATION TRIES TO MAKE IT…

Delayed Jobs (Asyncronous Jobs)… Doing two things at the same time… COMPU PU PU PUs are really stupid they (typically) do commands one after the other, even if it makes sense to do them at the same time. LIKE FOR INSTANCE… Would you boil water and watch it until the water boils, then once it boils, you’d start preparing the food? NO FUCK YOU’D DO IT ALL AT THE SAME TIME. UNFORTUNATELY FOR MANY COMPUTER LANGUAGES IT JUST DOESNT HAPPEN THAT WAY.

So they came up with this thing called delayed_job. It’s been around for 7 years. SINCE IT HAS BEEN AROUND FOR SO LONG, THEY DECIDED EVERYONE WHO MIGHT WANT TO USE IT WOULD JUST READ THE CODE IN THEIR REPO. SO THEY DON’T FUCKING HELP YOU AT ALL. THEIR DOCS BLOW ASS. Really shitty documentation. Complete shit. Mine probably won’t be any better for you either.

One of the biggest confusion points is rails has its own convention with Jobs, then you can assign an adapter to it like delayed_job… that’s what I do… The rails service is called active job.

# Gemfile
gem 'delayed_job_active_record'

# Generate delayed_job files
rails generate delayed_job:active_record
rake db:migrate

# Tell rails (ActiveJobs) that you are using delayed_job
# config/application.rb
config.active_job.queue_adapter = :delayed_job

# Creating a Job file
rails generate job future_building_upgrade

# app/jobs/add_future_building_upgrade_job.rb
class FutureBuildingUpgradeJob < ActiveJob::Base
  queue_as :default


  def perform(building_id)
    building = Building.find(building_id)
    if building.future_building_upgrade.active == true
      building.update(level: building.next_level)
      building.future_building_upgrade.update(active:false)
    end
  end
end


# utilizing the job ... this will perform the job in 12 days, 5 hours and 30 minutes from creating it.
FutureBuildingUpgradeJob.set(wait:12.days + 5.hours + 30.minutes).perform_later(building_id)

# utilizing the job immediately...
FutureBuildingUpgradeJob.perform_later(buildings)


# Run your delayed_job daemon (daemon also means worker, worker also means computer... basically a daemon is a way for computer science people to make you feel stupid. It's basically another server that only runs your jobs)

# Gemfile
gem 'daemons'

$ bin/delayed_job

Post Content