Skip to content

Instantly share code, notes, and snippets.

@anicholson
Created October 27, 2016 23:24
Show Gist options
  • Save anicholson/1ec2526edb5a573f9c20a10c54a82bdc to your computer and use it in GitHub Desktop.
Save anicholson/1ec2526edb5a573f9c20a10c54a82bdc to your computer and use it in GitHub Desktop.

Revisions

  1. anicholson created this gist Oct 27, 2016.
    38 changes: 38 additions & 0 deletions delayed_job
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    #!/usr/bin/env ruby
    # frozen_string_literal: true

    require 'bundler/setup'
    require_relative '../config/environment'

    require 'rack'

    # Take the whole script down if anything goes remotely wrong
    Thread.abort_on_exception = true

    MACCHIATO_PORT = 4000

    # Health Check: A rack app that listens on 4000
    # and returns 200 to indicate it's alive.

    _health_check_thread = Thread.new do
    headers = { 'Content-Type' => 'text/plain' }.freeze
    response = ['200', headers, 'Alive'].freeze

    app = proc { |_env| response }

    Rack::Handler::WEBrick.run app, Port: MACCHIATO_PORT
    end

    # Worker Job: A thread that initiates the DelayedJob worker queue.

    delayed_job_options = {
    quiet: false
    }.freeze

    worker_thread = Thread.new { Delayed::Worker.new(delayed_job_options).start }

    # Wait for the worker queue thread to finish before ending the script.
    # If it crashes, the script will end and take the health check thread
    # down as well.

    worker_thread.join