Module: ArMailerRevised::Helpers::CommandLine::ClassMethods

Defined in:
lib/ar_mailer_revised/helpers/command_line.rb

Instance Method Summary collapse

Instance Method Details

#display_mail_queue(what) ⇒ Object

Prints a list of unsent emails and the last delivery attempt, if any. Only emails which are ready to deliver are displayed



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ar_mailer_revised/helpers/command_line.rb', line 46

def display_mail_queue(what)
    emails = case what
               when 'all' then
                 puts 'Showing all emails in the system'
                 ArMailerRevised.email_class.all
               when 'deliverable' then
                 puts 'Showing emails ready to deliver'
                 ArMailerRevised.email_class.ready_to_deliver
               when 'delayed' then
                 puts 'Showing delayed emails'
                 ArMailerRevised.email_class.delayed
               else
                 []
             end
  puts 'Mail queue is empty' and return if emails.empty?
  puts Hirb::Helpers::AutoTable.render emails, :fields => [:from, :to, :delivery_time, :last_send_attempt, :updated_at]
end

#load_rails_environment(base_path) ⇒ Object

Loads the complete rails environment



176
177
178
179
180
181
182
183
184
# File 'lib/ar_mailer_revised/helpers/command_line.rb', line 176

def load_rails_environment(base_path)
  Dir.chdir(base_path) do
    require File.join(base_path, 'config/environment')
    require 'action_mailer/ar_mailer'
  end
rescue LoadError => e
  puts e
  raise RailsEnvironmentFailed
end

#process_args(args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ar_mailer_revised/helpers/command_line.rb', line 64

def process_args(args)
  name = File.basename $0

  options             = {}
  options[:chdir]     = '.'
  options[:max_age]   = 86400 * 7
  options[:rails_env] = ENV['RAILS_ENV'] || 'production'
  options[:log_level] = 'info'
  options[:verbose]   = false

  opts = OptionParser.new do |opts|
    opts.banner = "Usage: #{name} [options]"
    opts.separator ''

    opts.separator "#{name} scans the email table for new messages and sends them to the"
    opts.separator "website's configured SMTP host."
    opts.separator ''
    opts.separator "#{name} must be run from a Rails application's root."

    opts.separator ''
    opts.separator 'Sendmail options:'

    opts.on("-b", "--batch-size BATCH_SIZE",
            "Maximum number of emails to send per delay",
            "Default: Deliver all available emails", Integer) do |batch_size|
      options[:batch_size] = batch_size
    end

    opts.on("--max-age MAX_AGE",
            "Maxmimum age for an email. After this",
            "it will be removed from the queue.",
            "Set to 0 to disable queue cleanup.",
            "Default: #{options[:max_age]} seconds", Integer) do |max_age|
      options[:max_age] = max_age
    end

    opts.on('--mailq [all|deliverable|delayed]',
            'Display a list of emails waiting to be sent',
            'Default: all') do |mailq|
      options[:display_queue] = mailq || 'all'
    end

    opts.separator ''
    opts.separator 'Generic Options:'

    opts.on('-l', '--log-file PATH',
            'Custom log file location at PATH. May also be "stdout" or "stderr" for console output',
            'Default: log/environment.log') do |path|
      dir = File.dirname(path)
      usage opts, "#{dir} is not an existing directory" unless File.exists?(dir) && File.directory?(dir)
      usage opts, "#{path} is a directory" if File.directory?(path)
      options[:log_file] = path
    end

    opts.on('--log-level LEVEL',
            "Set the mailer's log LEVEL",
            "Default: #{options[:log_level]}") do |level|
      usage opts, "Invalid log-level: #{level}" unless %w[debug info warn error fatal].include?(level.to_s.downcase)
      options[:log_level] = level
    end

    opts.on("-c", "--chdir PATH",
            "Use PATH for the application path",
            "Default: #{options[:chdir]}") do |path|
      usage opts, "#{path} is not a directory" unless File.directory? path
      usage opts, "#{path} is not readable" unless File.readable? path
      options[:chdir] = path
    end

    opts.on("-e", "--environment RAILS_ENV",
            "Set the RAILS_ENV constant",
            "Default: #{options[:rails_env]}") do |env|
      options[:rails_env] = env
    end

    opts.on("-v", "--[no-]verbose",
            "Be verbose",
            "Default: #{options[:verbose]}") do |verbose|
      options[:verbose] = verbose
    end

    opts.on("-h", "--help",
            "You're looking at it") do
      usage opts
    end

    opts.on("--version", "Version of ARMailer") do
      usage "ar_mailer_revised #{VERSION}"
    end

    opts.separator ''
  end

  opts.parse! args

  ENV['RAILS_ENV'] = options[:rails_env]

  begin
    load_rails_environment(options[:chdir])
  rescue RailsEnvironmentFailed
    usage opts, <<-EOF
#{name} must be run from a Rails application's root to deliver email.
#{Dir.pwd} does not appear to be a Rails application root.
    EOF
  end

  options
end

#run(args = ARGV) ⇒ Object

Processes args and runs as appropriate



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ar_mailer_revised/helpers/command_line.rb', line 22

def run(args = ARGV)
  options = process_args(args)

  if options[:display_queue]
    display_mail_queue(options[:display_queue])
    exit
  end

  new(options).run

rescue SystemExit
  raise
rescue SignalException
  exit
rescue Exception => e
  $stderr.puts "Unhandled exception #{e.message}(#{e.class}):"
  $stderr.puts "\t#{e.backtrace.join "\n\t"}"
  exit -2
end

#usage(opts, message = nil) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/ar_mailer_revised/helpers/command_line.rb', line 186

def usage(opts, message = nil)
  if message then
    $stderr.puts message
    $stderr.puts
  end

  $stderr.puts opts
  exit 1
end