push-c2dm 1.0.0 → 1.0.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.
@@ -1,3 +1,3 @@
1
1
  module PushC2dm
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,10 +1,13 @@
1
1
  module Push
2
2
  module Daemon
3
3
  module C2dmSupport
4
+ class ConnectionError < StandardError; end
5
+
4
6
  class ConnectionC2dm
5
7
  attr_reader :response, :name, :provider
6
8
  AUTH_URL = "https://www.google.com/accounts/ClientLogin"
7
9
  PUSH_URL = "https://android.apis.google.com/c2dm/send"
10
+ IDLE_PERIOD = 5.minutes
8
11
 
9
12
  def initialize(provider, i)
10
13
  @provider = provider
@@ -74,15 +77,48 @@ module Push
74
77
  "Content-type" => "application/x-www-form-urlencoded",
75
78
  "Content-length" => "#{data.length}" }
76
79
  uri = URI.parse(PUSH_URL)
80
+ post(uri, data, headers)
81
+ end
77
82
 
78
- # Timeout on the http connection is 5 minutes, reconnect after 5 minutes
79
- if @last_use + 5.minutes < Time.now
80
- @connection.finish
81
- @connection.start
83
+ def post(uri, data, headers)
84
+ reconnect_idle if idle_period_exceeded?
85
+
86
+ retry_count = 0
87
+
88
+ begin
89
+ response = @connection.post(uri.path, data, headers)
90
+ @last_use = Time.now
91
+ rescue EOFError, Errno::ECONNRESET, Timeout::Error => e
92
+ retry_count += 1
93
+
94
+ Push::Daemon.logger.error("[#{@name}] Lost connection to #{PUSH_URL} (#{e.class.name}), reconnecting ##{retry_count}...")
95
+
96
+ if retry_count <= 3
97
+ reconnect
98
+ sleep 1
99
+ retry
100
+ else
101
+ raise ConnectionError, "#{@name} tried #{retry_count-1} times to reconnect but failed (#{e.class.name})."
102
+ end
82
103
  end
83
- @last_use = Time.now
84
104
 
85
- @connection.post(uri.path, data, headers)
105
+ response
106
+ end
107
+
108
+ def idle_period_exceeded?
109
+ # Timeout on the http connection is 5 minutes, reconnect after 5 minutes
110
+ @last_use + IDLE_PERIOD < Time.now
111
+ end
112
+
113
+ def reconnect_idle
114
+ Push::Daemon.logger.info("[#{@name}] Idle period exceeded, reconnecting...")
115
+ reconnect
116
+ end
117
+
118
+ def reconnect
119
+ @connection.finish
120
+ @last_use = Time.now
121
+ @connection.start
86
122
  end
87
123
  end
88
124
  end
@@ -44,15 +44,16 @@ module Push
44
44
  # not be used anymore
45
45
  if ["InvalidRegistration", "NotRegistered"].index(error_type)
46
46
  with_database_reconnect_and_retry(connection.name) do
47
- Push::FeedbackC2dm.create!(:app => connection.provider.configuration[:name], :failed_at => Time.now, :device => device, :follow_up => 'delete')
47
+ Push::FeedbackC2dm.create!(:app => connection.provider.configuration[:name], :failed_at => Time.now,
48
+ :device => device, :follow_up => 'delete')
48
49
  end
49
- else
50
- Push::Daemon.logger.error("[#{connection.name}] Error received.")
51
- raise Push::DeliveryError.new(response.code, id, error_type, "C2DM")
52
50
  end
51
+
52
+ Push::Daemon.logger.error("[#{connection.name}] Error received.")
53
+ raise Push::DeliveryError.new(response.code, id, error_type, "C2DM")
53
54
  elsif !response.code.eql? "200"
54
55
  Push::Daemon.logger.error("[#{connection.name}] Error received.")
55
- raise Push::DeliveryError.new(response.code, id, response.description, "C2DM")
56
+ raise Push::DeliveryError.new(response.code, id, response.message, "C2DM")
56
57
  end
57
58
  end
58
59
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: push-c2dm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-19 00:00:00.000000000 Z
12
+ date: 2012-09-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: multi_json
16
- requirement: &70324549334240 !ruby/object:Gem::Requirement
16
+ requirement: &70146043588440 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '1.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70324549334240
24
+ version_requirements: *70146043588440
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: push-core
27
- requirement: &70324549333160 !ruby/object:Gem::Requirement
27
+ requirement: &70146043587200 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70324549333160
35
+ version_requirements: *70146043587200
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: sqlite3
38
- requirement: &70324549332620 !ruby/object:Gem::Requirement
38
+ requirement: &70146043586440 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70324549332620
46
+ version_requirements: *70146043586440
47
47
  description: C2DM support for the modular push daemon.
48
48
  email:
49
49
  - tom@tnux.net