online_manager 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +13 -11
- data/lib/online_manager.rb +6 -17
- data/online_manager.gemspec +1 -1
- metadata +2 -3
- data/lib/online_manager/dsl.rb +0 -23
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 24f8a91946ab2eb1b0585331886f4e70c253e70e
|
4
|
+
data.tar.gz: 793ac0de4e25815d5d2425ed462ea6e1538f480b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4da8e86883f70f5170a725e36dea4ae991d8b228bf26c238731f61fd9c0590aab6af8e6070801523dd2e18892e480aa238f5f97d30a8b60943caec7717673282
|
7
|
+
data.tar.gz: 7271777e28da50652f36a64f507ae0fb85f69548c56cadf88799197287457558d90e719c71811743b83ac1e087b98e33cf36b6c2172d3aea10f12c364f5c1ea3
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -30,31 +30,35 @@ require 'online_manager'
|
|
30
30
|
# You should set all the users offline when it first runs,
|
31
31
|
# since otherwise you'll be left with users marked online from when it last quit.
|
32
32
|
|
33
|
-
|
34
|
-
OnlineManager.run do |c|
|
33
|
+
class OnlineConfig
|
35
34
|
|
36
35
|
# How long since a heartbeat was last received until the user is considered offline? (in seconds)
|
37
|
-
|
36
|
+
def timeout
|
37
|
+
5
|
38
|
+
end
|
38
39
|
|
39
40
|
# All the blocks below will be called inside an eventmachine reactor.
|
40
41
|
|
41
|
-
|
42
|
+
def setup
|
42
43
|
# Set up any websocket server/connection to pubsub server, etc
|
43
44
|
# This must use eventmachine, otherwise it will block the reactor.
|
44
|
-
# Then
|
45
|
+
# Then yield(user_id) whenever you receive a heartbeat from a user.
|
45
46
|
end
|
46
47
|
|
47
|
-
|
48
|
+
def online(user_id)
|
48
49
|
puts "Booster #{user_id} online"
|
49
50
|
# Update the user to online in the database or whatever
|
50
51
|
# This must use asynchronous i/o, otherwise it will block the reactor.
|
51
52
|
end
|
52
53
|
|
53
|
-
|
54
|
+
def offline(user_id)
|
54
55
|
puts "Booster #{user_id} offline"
|
55
56
|
# Called when a user transitions from online -> offline
|
56
57
|
end
|
57
58
|
end
|
59
|
+
|
60
|
+
# This runs an eventmachine reactor, so the call to .run blocks.
|
61
|
+
OnlineManager.run(OnlineConfig.new)
|
58
62
|
```
|
59
63
|
|
60
64
|
If you already are in the context of an eventmachine reactor, or you want to run multiple of these in the same thread,
|
@@ -62,15 +66,13 @@ you can use `.setup`.
|
|
62
66
|
|
63
67
|
```ruby
|
64
68
|
EM.run do
|
65
|
-
OnlineManager.setup
|
66
|
-
# Configure it
|
67
|
-
end
|
69
|
+
OnlineManager.setup(OnlineConfig.new)
|
68
70
|
end
|
69
71
|
```
|
70
72
|
|
71
73
|
## TODO
|
72
74
|
|
73
|
-
* Write tests for this. Its pretty simple (~ 50 lines of code
|
75
|
+
* Write tests for this. Its pretty simple (~ 50 lines of code) and I have it running in production, so it should be fine.
|
74
76
|
Check out [this][1] for testing eventmachine.
|
75
77
|
* Consider setting remaining users offline in an ensure block, so it always leaves the statuses in a consistent state.
|
76
78
|
[Could this replace setting users offline on initialization?][2]
|
data/lib/online_manager.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
1
|
require 'eventmachine'
|
2
2
|
|
3
|
-
require 'online_manager/dsl'
|
4
|
-
|
5
3
|
class OnlineManager
|
6
|
-
attr_writer :online_callback, :offline_callback, :setup_callback, :timeout
|
7
|
-
|
8
4
|
def self.run(*args, &block)
|
9
5
|
new(*args, &block).run
|
10
6
|
end
|
@@ -13,16 +9,9 @@ class OnlineManager
|
|
13
9
|
new(*args, &block).setup
|
14
10
|
end
|
15
11
|
|
16
|
-
def initialize
|
17
|
-
@
|
12
|
+
def initialize(config)
|
13
|
+
@config = config
|
18
14
|
@online_users = {}
|
19
|
-
@online_callback = -> (_) { }
|
20
|
-
@offline_callback = -> (_) { }
|
21
|
-
@setup_callback = ->(_) { raise 'You must specify a setup callback' }
|
22
|
-
|
23
|
-
yield DSL.new(self) if block_given?
|
24
|
-
|
25
|
-
raise 'Timeout must be specified' unless @timeout
|
26
15
|
end
|
27
16
|
|
28
17
|
def run
|
@@ -30,18 +19,18 @@ class OnlineManager
|
|
30
19
|
end
|
31
20
|
|
32
21
|
def setup
|
33
|
-
@
|
22
|
+
@config.setup(&method(:seen))
|
34
23
|
end
|
35
24
|
|
36
25
|
private
|
37
26
|
|
38
27
|
def seen(id)
|
39
|
-
@
|
28
|
+
@config.online(id) unless online?(id)
|
40
29
|
|
41
30
|
time = Time.now
|
42
31
|
@online_users[id] = time
|
43
32
|
|
44
|
-
EM.add_timer(@timeout) do
|
33
|
+
EM.add_timer(@config.timeout) do
|
45
34
|
set_offline(id, time)
|
46
35
|
end
|
47
36
|
end
|
@@ -49,7 +38,7 @@ private
|
|
49
38
|
def set_offline(id, time)
|
50
39
|
if @online_users[id] == time
|
51
40
|
@online_users.delete(id)
|
52
|
-
@
|
41
|
+
@config.offline(id)
|
53
42
|
end
|
54
43
|
end
|
55
44
|
|
data/online_manager.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = 'online_manager'
|
7
|
-
spec.version = '0.0.
|
7
|
+
spec.version = '0.0.3'
|
8
8
|
spec.authors = ['Cameron Martin']
|
9
9
|
spec.email = ['cameronmartin123@gmail.com']
|
10
10
|
spec.summary = %q{Manage user's online status.}
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: online_manager
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Martin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -80,7 +80,6 @@ files:
|
|
80
80
|
- README.md
|
81
81
|
- Rakefile
|
82
82
|
- lib/online_manager.rb
|
83
|
-
- lib/online_manager/dsl.rb
|
84
83
|
- online_manager.gemspec
|
85
84
|
homepage: https://github.com/cameron-martin/online_manager
|
86
85
|
licenses:
|
data/lib/online_manager/dsl.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
class OnlineManager
|
2
|
-
class DSL
|
3
|
-
def initialize(target)
|
4
|
-
@target = target
|
5
|
-
end
|
6
|
-
|
7
|
-
def timeout(timeout)
|
8
|
-
@target.timeout = timeout
|
9
|
-
end
|
10
|
-
|
11
|
-
def online(&block)
|
12
|
-
@target.online_callback = block
|
13
|
-
end
|
14
|
-
|
15
|
-
def offline(&block)
|
16
|
-
@target.offline_callback = block
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup(&block)
|
20
|
-
@target.setup_callback = block
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|