Sha256: 126cceddeea09669c0a26c8b8cfe39cc82bef6a5d1d911a565818176e6bed300

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'thor'

class Localjob
  class CLI < Thor
    option :queue,     aliases: ["-q"], type: :string, default: "0x10CA110B"
    option :require,   aliases: ["-r"], type: :string, default: "."
    option :pid_file,  aliases: ["-p"], type: :string
    desc "work", "Start worker to process jobs"
    def work
      load_environment options[:require]
      Localjob::Worker.new(options[:queue].to_i(16), *options.slice(:pid_file)).work
    end

    private
    def load_environment(file)
      if rails?(file)
        require 'rails'
        require File.expand_path("#{file}/config/environment.rb")
        ::Rails.application.eager_load!
      elsif File.file?(file)
        require File.expand_path(file)
      else
        puts <<EOS
To run a localjob worker, you must supply the --require flag. This is the file
the worker requires before working off jobs.

Otherwise localjob will complain about undefined classes, because they are not
loaded in the context of the worker.
EOS
        exit!
      end
    end

    def rails?(file)
      File.exists?(File.expand_path("#{file}/config/environment.rb"))
    end

    def msg_max
      @msg_max ||= File.read("/proc/sys/fs/mqueue/msg_max")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
localjob-0.4.1 lib/localjob/cli.rb