Class: Employer::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/employer/cli.rb

Instance Method Summary collapse

Instance Method Details

#clearObject



35
36
37
38
39
40
41
42
# File 'lib/employer/cli.rb', line 35

def clear
  unless File.exists?(options[:config])
    STDERR.puts "#{options[:config]} does not exist."
    exit 1
  end

  Employer::Workshop.pipeline(options[:config]).clear
end

#configObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/employer/cli.rb', line 46

def config
  if File.exists?(options[:config])
    STDERR.puts "#{options[:config]} already exists."
    exit 1
  end

  FileUtils.mkdir("config") unless File.directory?("config")

  File.open(options[:config], "w") do |file|
    file.write <<CONFIG
# If you're using Rails the below line requires config/environment to setup the
# Rails environment. If you're not using Rails you'll want to require something
# here that sets up Employer's environment appropriately (making available the
# classes that your jobs need to do their work, providing the connection to
# your database, etc.)
# require "./config/environment"

require "employer-mongoid"

# Setup the backend for the pipeline, this is where the boss gets the jobs to
# process. See the documentation for details on writing your own pipeline
# backend.
pipeline_backend Employer::Mongoid::Pipeline.new

# Add loggers to log. Logged output will go to all of the loggers defined here.
log_to ::Logger.new("./log/employer.log")

# Use employees that fork subprocesses to perform jobs. You cannot use these
# with JRuby, because JRuby doesn't support Process#fork.
forking_employees 4

# Use employees that run their jobs in threads, you can use these when using
# JRuby. While threaded employees also work with MRI they are limited by the
# GIL (this may or may not be a problem depending on the type of work your jobs
# need to do).
# threading_employees 4
CONFIG
  end
end

#workObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/employer/cli.rb', line 11

def work
  unless File.exists?(options[:config])
    STDERR.puts "#{options[:config]} does not exist."
    exit 1
  end

  int_count = 0
  workshop = Employer::Workshop.new(File.read(options[:config]))
  workshop.log_to(::Logger.new(STDOUT))

  Signal.trap("INT") do
    int_count += 1
    if int_count == 1
      workshop.stop
    else
      workshop.stop_now
    end
  end

  workshop.run
end