mail_manager 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZTZlYzBlZDYwOTEyZmE1OTMxZmQ5NWRiZmM3M2MyMjc4YmIxMTc2Mw==
4
+ NjliMjdlY2MyOGQ4ODI0ZjJmOGE4YzRhNGMwZjA4NjM0MmY3NDAyZA==
5
5
  data.tar.gz: !binary |-
6
- ZmRkZDEwN2JmNmEyNTVlMjdmNjNlZWFmMjBiM2QxY2MyZWM1NWRjZg==
6
+ OGY4MTA0NWE0NmM0NDllNzA1NWZlOTcxNjBlYWZiYzQzYmRjNmRjZg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MDUzMGFhYTE2ZGY4ODgyNmJiN2I1ZmM5YWI2YWVlYmMyMWIxZDAwOTgyNGFl
10
- ZmQzYTdhYjI5Y2I3MTk4YTMxNjUwYTJlMmI5OGM0M2VlMDA0OWMxYTJkZDRi
11
- ZDgxZGYyMjM3ZjYwZDQ0YjM0ZTE0YzcxNTBjMTc2NDFlMDJjYzU=
9
+ NWY3NjJkMDdhMTdjYzc1OTQ4OTE1ODRlYjMyNGM4NTlkNjY5YTg2NDY1ZjEy
10
+ YmFmZmQ4NTU3NDY1ZWY3NDBhN2IzZmI3ZjczZGE1NDQ5M2UwMTU1MmVmNWEw
11
+ YTE1ZWMwZTljMGViNDQ4NGQ3OWRlNzVmODliMmFhZjdkYTA2OTQ=
12
12
  data.tar.gz: !binary |-
13
- OGVhODBkZjhiMjRjNWVjZTk0ZDhlODgwN2M0YWU0YzgzMDMxZTI2ZDk4ODY1
14
- ZTJiNTI5MzM0MjRjNjJjODdmZGUzNTZkOTI3MzI0NDdjNDE3Y2RmMGM4NTMy
15
- YjhmN2U3MzNlMDE1NjlmZmFmYmI0M2RjYWNhOGMxODMzZDAxNjE=
13
+ ZTQ1YmVjODdjMWEyYmE0MmYzMzA2YjlkN2MxNTk3NWEyNjdjOWNkY2M4MGEx
14
+ ODNhNGI4MTQxNDgwMDY4ZjM4YWY2MDYyY2U4NjFkYjM1MmNjOWE5OTg0MmI3
15
+ NzNkNGYwMjY0NDJkN2NlMGIxZDVhOTZlOTEzMGVmMGMzNTUxNTg=
@@ -0,0 +1,14 @@
1
+ require File.join(MailManager::PLUGIN_ROOT,'lib','delayed','status')
2
+ require File.join(MailManager::PLUGIN_ROOT,'lib','delayed','repeating_job')
3
+ require File.join(MailManager::PLUGIN_ROOT,'lib','delayed','status_job')
4
+ require File.join(MailManager::PLUGIN_ROOT,'lib','delayed','persistent_job')
5
+ require File.join(MailManager::PLUGIN_ROOT,'lib','delayed','mailer')
6
+
7
+ # config/initializers/delayed_job_config.rb
8
+ Delayed::Worker.destroy_failed_jobs = false
9
+ Delayed::Worker.sleep_delay = 60
10
+ Delayed::Worker.max_attempts = 1
11
+ Delayed::Worker.max_run_time = 5.minutes
12
+ Delayed::Worker.read_ahead = 10
13
+ Delayed::Worker.delay_jobs = !Rails.env.test?
14
+ Delayed::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'delayed_job.log'))
@@ -0,0 +1,8 @@
1
+ class Delayed::Mailer < ActionMailer::Base
2
+ def exception_notification(job,error,emails)
3
+ recipients emails
4
+ from Conf.exception_notification['sender_address']
5
+ subject "* [JOB] #{job.name}(#{job.id}) failed on #{`hostname`} in #{Rails.root}"
6
+ body "* [JOB] #{job.name}(#{job.id}) failed with #{error.class.name}: #{error.message} - #{job.attempts} failed attempts\n #{error.backtrace.join("\n ")}"
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Delayed
2
+ class PersistentJob < Job
3
+ # Try to run job. Returns true/false (work done/work failed)
4
+ @@destroy_failed_jobs = false
5
+ @@destroy_successful_jobs = false
6
+ @@default_max_attempts = 1
7
+ end
8
+ end
@@ -0,0 +1,67 @@
1
+ module Delayed
2
+ class RepeatingJob < Job
3
+ def repeats_every
4
+ (@repeats_every ||= payload_object.repeats_every || 1.minutes) rescue 1.minutes
5
+ end
6
+
7
+ def repeats_every=(time_span)
8
+ payload_object.repeats_every = time_span if payload_object.respond_to?(:repeats_every)
9
+ @repeats_every = time_span
10
+ end
11
+
12
+ def total_runs
13
+ begin
14
+ @total_runs ||= payload_object.total_runs || 0
15
+ rescue => e
16
+ @total_runs ||= 0
17
+ end
18
+ end
19
+
20
+ def total_runs=(value)
21
+ @total_runs = (payload_object.total_runs = value) rescue value
22
+ end
23
+
24
+ # I always repeat!
25
+ # Try to run job. Returns true/false (work done/work failed)
26
+ def run(max_run_time=500)
27
+ runtime = Benchmark.realtime do
28
+ #FIXME: I don't like timeout ...
29
+ #Timeout.timeout(max_run_time.to_i) { invoke_job }
30
+ invoke_job
31
+ end
32
+ # TODO: warn if runtime > max_run_time ?
33
+ logger.info "* [JOB-#{id}] #{name} completed after %.4f" % runtime
34
+ return repeat
35
+ rescue Exception => e
36
+ begin
37
+ repeat e.message, e.backtrace
38
+ rescue => e2
39
+ logger.warn "Job[#{id}] could not repeat #{e2.message} #{e2.backtrace.join("\n")}"
40
+ self.update_attributes(:failed_at=>Time.now,:last_error => "Could not repeat #{e2.message} #{e2.backtrace.join("\n")}")
41
+ end
42
+ log_exception(e)
43
+ return false # work failed
44
+ end
45
+
46
+ # Repeat the job in the future.
47
+ def repeat(message="", backtrace = [])
48
+ unless message.blank?
49
+ self.last_error = %Q|#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}: #{message}\n#{backtrace.join("\n")}
50
+ "\n#{self.last_error}| if message
51
+ self.failed_at = Time.now
52
+ self.attempts += 1
53
+ else
54
+ self.failed_at = nil
55
+ self.attempts = 0
56
+ end
57
+ while(run_at <= Time.now) do
58
+ self.run_at += repeats_every
59
+ end
60
+ self.unlock
61
+ self.total_runs += 1
62
+ save!
63
+ reload
64
+ message.blank?
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,13 @@
1
+ module Delayed
2
+ class StatusException < Exception
3
+ end
4
+ class Status
5
+ def self.ok?(overdue=15.minutes)
6
+ job = Delayed::StatusJob.first || Delayed::StatusJob.enqueue(::StatusJob.new)
7
+ elapsed_time = (Time.now - job.updated_at).to_i
8
+ raise(::Delayed::StatusException, "Rails3 Status job has failed at #{job.failed_at} with message: #{job.last_error}") if job.failed?
9
+ raise(::Delayed::StatusException, "Rails3 Status job hasn't run for #{elapsed_time} seconds") if elapsed_time > overdue
10
+ true
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ # module Delayed
2
+ # class StatusJob < RepeatingJob
3
+ # def perform
4
+ # true
5
+ # end
6
+
7
+ # def repeats_every
8
+ # 1.minutes
9
+ # end
10
+
11
+ # # This is a good hook if you need to report job processing errors in additional or different ways
12
+ # def log_exception(error)
13
+ # #don't send mail for this currently.. we'll do something smart laters
14
+ # #Delayed::Mailer.deliver_exception_notification(self,error,notify_email) unless notify_email.blank?
15
+ # logger.error "* [JOB] #{name}(#{id}) failed with #{error.class.name}: #{error.message} - #{attempts} failed attempts"
16
+ # end
17
+ # end
18
+ # end
19
+ class Delayed::StatusJob < Delayed::Job
20
+ def perform
21
+ Delayed::StatusJob.enqueue ::StatusJob.new, run_at: 1.minute.from_now
22
+ end
23
+ end
@@ -39,5 +39,7 @@ module MailManager
39
39
  end
40
40
  MailManager::Engine.config.to_prepare do
41
41
  ApplicationController.helper(MailManager::SubscriptionsHelper)
42
+ load File.join(MailManager::PLUGIN_ROOT,'config','initializers','delayed_job.rb')
43
+ load File.join(MailManager::PLUGIN_ROOT,'lib','mail_manager','lock.rb')
42
44
  end
43
45
  require 'will_paginate'
@@ -1,11 +1,11 @@
1
- class Lock
2
- class LockException < Exception
1
+ class MailManager::Lock
2
+ class MailManager::LockException < Exception
3
3
  end
4
4
  def self.with_lock(name, timeout=5, max_attempts=1, &block)
5
5
  ActiveRecord::Base.connection_pool.with_connection do |connection|
6
6
  begin
7
7
  lock = get_lock(connection,name,timeout,max_attempts)
8
- raise LockException.new("Failed to obtain lock #{name} in #{timeout} secs") unless lock
8
+ raise MailManager::LockException.new("Failed to obtain lock #{name} in #{timeout} secs") unless lock
9
9
  yield lock
10
10
  ensure
11
11
  is_released = release_lock(connection,name)
@@ -1,3 +1,3 @@
1
1
  module MailManager
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  require 'rake'
2
2
  ENV["Rails.env"] ||= "development"
3
- require "#{Rails.root}/config/environment"
3
+ require File.join(Rails.root,'config','environment')
4
+ initializer = File.join(MailManager::PLUGIN_ROOT,'config','initializers','delayed_job.rb')
5
+ load initializer
4
6
 
5
7
  namespace :mail_manager do
6
8
  desc "Create mlm LSI Auth Menus"
@@ -19,7 +19,7 @@ module MailManager
19
19
  class BounceJob < Struct.new(:repeats_every)
20
20
  def perform
21
21
  found_messages = false
22
- with_lock('mail_manager_bounce_job') do
22
+ ::MailManager::Lock.with_lock('mail_manager_bounce_job') do
23
23
  Rails.logger.info "Bounce Job Connecting to #{MailManager.bounce['pop_server']} with #{MailManager.bounce['login']}:#{MailManager.bounce['password']}"
24
24
  Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) if MailManager.bounce['ssl']
25
25
  Net::POP3.start(MailManager.bounce['pop_server'],MailManager.bounce['port'],
@@ -19,7 +19,7 @@ module MailManager
19
19
  end
20
20
 
21
21
  def self.get_ready
22
- Lock.with_lock('mail_manager_mailing_job_ready') do |lock|
22
+ MailManager::Lock.with_lock('mail_manager_mailing_job_ready') do |lock|
23
23
  mailing = Mailing.ready.first
24
24
  return nil if mailing.nil?
25
25
  mailing.change_status('processing')
@@ -27,4 +27,4 @@ module MailManager
27
27
  end
28
28
  end
29
29
  end
30
- end
30
+ end
@@ -26,7 +26,7 @@ module MailManager
26
26
  end
27
27
 
28
28
  def self.get_ready
29
- Lock.with_lock('mail_manager_message_ready') do |lock|
29
+ MailManager::Lock.with_lock('mail_manager_message_ready') do |lock|
30
30
  Rails.logger.warn "Finding ready messages"
31
31
  message = Message.ready.first
32
32
  return nil if message.nil?
@@ -26,7 +26,7 @@ module MailManager
26
26
  end
27
27
 
28
28
  def self.get_ready
29
- Lock.with_lock('mail_manager_test_message_ready') do |lock|
29
+ MailManager::Lock.with_lock('mail_manager_test_message_ready') do |lock|
30
30
  test_message = TestMessage.ready.first
31
31
  return nil if test_message.nil?
32
32
  test_message.change_status('processing')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lone Star Internet
@@ -193,6 +193,7 @@ files:
193
193
  - app/views/mail_manager/subscriptions/unsubscribe.html.erb
194
194
  - app/views/mail_manager/subscriptions/unsubscribe_by_email_address.html.erb
195
195
  - config/daemons.yml
196
+ - config/initializers/delayed_job.rb
196
197
  - config/routes.rb
197
198
  - db/migrate/001_mail_mgr_initial.rb
198
199
  - db/migrate/002_mail_mgr_create_contact.rb
@@ -223,11 +224,16 @@ files:
223
224
  - features/support/paths.rb
224
225
  - lib/daemons/mail_manager.rb
225
226
  - lib/daemons/mail_manager_ctl
227
+ - lib/delayed/mailer.rb
228
+ - lib/delayed/persistent_job.rb
229
+ - lib/delayed/repeating_job.rb
230
+ - lib/delayed/status.rb
231
+ - lib/delayed/status_job.rb
226
232
  - lib/deleteable.rb
227
- - lib/lock.rb
228
233
  - lib/mail_manager.rb
229
234
  - lib/mail_manager/config.rb
230
235
  - lib/mail_manager/engine.rb
236
+ - lib/mail_manager/lock.rb
231
237
  - lib/mail_manager/version.rb
232
238
  - lib/tasks/mail_manager.rake
233
239
  - lib/tasks/mail_manager_tasks.rake