Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

adding a Mailman.config.poll_count to limit the number of times to poll

  • Loading branch information...
commit 2623dd5eadb31cf129b8832a5e6f8e82003125b9 1 parent ca7e636
authored April 27, 2011
9  USER_GUIDE.md
Source Rendered
@@ -220,6 +220,15 @@ then exit.
220 220
 **Default**: `60`
221 221
 
222 222
 
  223
+### Polling Count
  224
+
  225
+`Mailman.config.poll_count` is the number of times to check for new messages
  226
+on the server. It is currently only used by the POP3 reciever. If it is set 
  227
+to `nil`, Mailman will poll forever.
  228
+
  229
+**Default**: `nil`
  230
+
  231
+
223 232
 ### Maildir
224 233
 
225 234
 `Mailman.config.maildir` is the location of a Maildir folder to watch. If it
3  lib/mailman/application.rb
@@ -57,7 +57,8 @@ def run
57 57
           connection.connect
58 58
           connection.get_messages
59 59
           connection.disconnect
60  
-          break if !polling
  60
+          Mailman.config.poll_count -= 1 unless Mailman.config.poll_count.nil?
  61
+          break if !polling || Mailman.config.poll_count == 0
61 62
           sleep Mailman.config.poll_interval
62 63
         end
63 64
 
8  lib/mailman/configuration.rb
@@ -10,6 +10,10 @@ class Configuration
10 10
     # @return [Fixnum] the poll interval for POP3 or IMAP. Setting this to 0
11 11
     #   disables polling
12 12
     attr_accessor :poll_interval
  13
+    
  14
+    # @return [Fixnum] the number of times to poll. Setting this to 0 
  15
+    #   causes piolling to continue forever
  16
+    attr_accessor :poll_count
13 17
 
14 18
     # @return [String] the path to the maildir
15 19
     attr_accessor :maildir
@@ -29,6 +33,10 @@ def logger
29 33
     def poll_interval
30 34
       @poll_interval ||= 60
31 35
     end
  36
+    
  37
+    def poll_count
  38
+      @poll_count ||= nil
  39
+    end
32 40
 
33 41
     def rails_root
34 42
       @rails_root ||= '.'
21  spec/functional/application_spec.rb
@@ -88,7 +88,7 @@ def send_example
88 88
     config.pop3 = { :server => 'example.com',
89 89
                     :username => 'chunky',
90 90
                     :password => 'bacon' }
91  
-    config.poll_interval = 0 # just poll once
  91
+    config.poll_count = 1
92 92
 
93 93
     mailman_app {
94 94
       from 'chunky@bacon.com' do
@@ -100,6 +100,25 @@ def send_example
100 100
     @app.run
101 101
     @app.router.instance_variable_get('@count').should == 2
102 102
   end
  103
+  
  104
+  it 'should poll a POP3 server a specified number of times' do
  105
+    config.pop3 = { :server => 'example.com',
  106
+                    :username => 'chunky',
  107
+                    :password => 'bacon' }
  108
+    config.poll_interval = 1
  109
+    config.poll_count = 2
  110
+
  111
+    mailman_app {
  112
+      from 'chunky@bacon.com' do
  113
+        @count ||= 0
  114
+        @count += 1
  115
+        MockPOP3.add_after_processing(3)
  116
+      end
  117
+    }
  118
+
  119
+    @app.run
  120
+    @app.router.instance_variable_get('@count').should == 5
  121
+  end
103 122
 
104 123
   it 'should watch a maildir folder for messages' do
105 124
     setup_maildir # creates the maildir with a queued message
9  spec/mailman/configuration_spec.rb
@@ -33,6 +33,15 @@
33 33
     config.poll_interval.should == 20
34 34
   end
35 35
 
  36
+  it 'should have a default poll count' do
  37
+    config.poll_count.should be_nil
  38
+  end
  39
+  
  40
+  it 'should store the poll count' do
  41
+    config.poll_interval = 40
  42
+    config.poll_interval.should == 40
  43
+  end
  44
+
36 45
   it 'should store the maildir location' do
37 46
     config.maildir = '../maildir-test'
38 47
     config.maildir.should == '../maildir-test'
16  spec/pop3_mock.rb
@@ -25,15 +25,27 @@ class MockPOP3
25 25
   def initialize
26 26
     @@popmails = []
27 27
     2.times do |i|
28  
-      @@popmails << MockPopMail.new("To: test@example.com\r\nFrom: chunky@bacon.com\r\nSubject: Hello!\r\n\r\nemail message\r\ntest#{i.to_s}", i)
  28
+      @@popmails << MockPOP3.create_message(i)
29 29
     end
30 30
   end
31 31
 
32 32
   def self.popmails
33 33
     @@popmails.clone
34 34
   end
35  
-
  35
+  
  36
+  def self.create_message(index)
  37
+    MockPopMail.new("To: test@example.com\r\nFrom: chunky@bacon.com\r\nSubject: Hello!\r\n\r\nemail message\r\ntest#{index.to_s}", index)
  38
+  end
  39
+  
  40
+  def self.add_after_processing(count)
  41
+    @@next_batch = []
  42
+    count.times do |i|
  43
+      @@next_batch << MockPOP3.create_message(i)
  44
+    end
  45
+  end
  46
+  
36 47
   def each_mail(*args)
  48
+    @@popmails = @@next_batch if @@popmails.length == 0 && !@@next_batch.nil?
37 49
     @@popmails.each do |popmail|
38 50
       yield popmail
39 51
     end

0 notes on commit 2623dd5

Please sign in to comment.
Something went wrong with that request. Please try again.