smailer 0.2.16 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -26,12 +26,16 @@ class CreateSmailerTables < ActiveRecord::Migration
26
26
  t.integer "retries", :default => 0, :null => false
27
27
  t.datetime "last_retry_at"
28
28
  t.string "last_error"
29
+ t.boolean "locked", :default => false, :null => false
30
+ t.datetime "locked_at"
29
31
  t.datetime "created_at"
30
32
  t.datetime "updated_at"
31
33
  t.string "key"
32
34
  end
33
35
  add_index "queued_mails", ["mail_campaign_id", "to"], :name => "index_queued_mails_on_mail_campain_id_and_to", :unique => true
34
- add_index "queued_mails", ["retries"], :name => "index_queued_mails_on_retries"
36
+ add_index "queued_mails", ["retries", "locked"], :name => "index_queued_mails_on_retries_and_locked"
37
+ add_index "queued_mails", ["locked", "retries", "id"], :name => "index_queued_mails_on_locked_retries_and_id"
38
+ add_index "queued_mails", ["locked", "locked_at"], :name => "index_queued_mails_on_locked_and_locked_at"
35
39
 
36
40
  create_table "finished_mails", :force => true do |t|
37
41
  t.integer "mail_campaign_id"
@@ -26,12 +26,16 @@ class CreateSmailerTables < ActiveRecord::Migration
26
26
  t.integer "retries", :default => 0, :null => false
27
27
  t.datetime "last_retry_at"
28
28
  t.string "last_error"
29
+ t.boolean "locked", :default => false, :null => false
30
+ t.datetime "locked_at"
29
31
  t.datetime "created_at"
30
32
  t.datetime "updated_at"
31
33
  t.string "key"
32
34
  end
33
35
  add_index "queued_mails", ["mail_campaign_id", "to"], :name => "index_queued_mails_on_mail_campain_id_and_to", :unique => true
34
- add_index "queued_mails", ["retries"], :name => "index_queued_mails_on_retries"
36
+ add_index "queued_mails", ["retries", "locked"], :name => "index_queued_mails_on_retries_and_locked"
37
+ add_index "queued_mails", ["locked", "retries", "id"], :name => "index_queued_mails_on_locked_retries_and_id"
38
+ add_index "queued_mails", ["locked", "locked_at"], :name => "index_queued_mails_on_locked_and_locked_at"
35
39
 
36
40
  create_table "finished_mails", :force => true do |t|
37
41
  t.integer "mail_campaign_id"
@@ -3,7 +3,14 @@ require 'mail'
3
3
  module Smailer
4
4
  module Tasks
5
5
  class Send
6
- def self.execute
6
+ def self.execute(options = {})
7
+ options.reverse_merge! :verp => !options[:return_path_domain].blank?
8
+
9
+ # validate options
10
+ if options[:verp] && options[:return_path_domain].blank?
11
+ raise "VERP is enabled, but a :return_path_domain option has not been specified or is blank."
12
+ end
13
+
7
14
  rails_delivery_method = if Compatibility.rails_3?
8
15
  Rails.configuration.action_mailer.delivery_method
9
16
  else
@@ -20,12 +27,19 @@ module Smailer
20
27
 
21
28
  results = []
22
29
 
30
+ # clean up any old locked items
31
+ Smailer::Models::QueuedMail.update_all({:locked => false}, ['locked = ? AND locked_at <= ?', true, 1.hour.ago.utc])
32
+
33
+ # load the queue items to process
23
34
  items_to_process = if Compatibility.rails_3?
24
- Smailer::Models::QueuedMail.order(:retries.asc, :id.asc).limit(batch_size)
35
+ Smailer::Models::QueuedMail.where(:locked => false).order(:retries.asc, :id.asc).limit(batch_size)
25
36
  else
26
- Smailer::Models::QueuedMail.all(:order => 'retries ASC, id ASC', :limit => batch_size)
37
+ Smailer::Models::QueuedMail.all(:conditions => {:locked => false}, :order => 'retries ASC, id ASC', :limit => batch_size)
27
38
  end
28
39
 
40
+ # lock the queue items
41
+ Smailer::Models::QueuedMail.update_all({:locked => true, :locked_at => Time.now.utc}, {:id => items_to_process.map(&:id)})
42
+
29
43
  items_to_process.each do |queue_item|
30
44
  # try to send the email
31
45
  mail = Mail.new do
@@ -38,8 +52,24 @@ module Smailer
38
52
  end
39
53
  mail.raise_delivery_errors = true
40
54
 
55
+ # compute the VERP'd return_path if requested
56
+ # or fall-back to a global return-path if not
57
+ item_return_path = if options[:verp]
58
+ "bounces-#{queue_item.key}@#{options[:return_path_domain]}"
59
+ else
60
+ options[:return_path]
61
+ end
62
+
63
+ # set the return-path, if any
64
+ if item_return_path
65
+ mail.return_path = item_return_path
66
+ mail['Errors-To'] = item_return_path
67
+ mail['Bounces-To'] = item_return_path
68
+ end
69
+
41
70
  queue_item.last_retry_at = Time.now
42
- queue_item.retries += 1
71
+ queue_item.retries += 1
72
+ queue_item.locked = false # unlock this email
43
73
 
44
74
  begin
45
75
  # commense delivery
@@ -1,3 +1,3 @@
1
1
  module Smailer
2
- VERSION = "0.2.16"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smailer
3
3
  version: !ruby/object:Gem::Version
4
- hash: 55
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 16
10
- version: 0.2.16
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dimitar Dimitrov
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-16 00:00:00 +03:00
18
+ date: 2011-09-21 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency