later 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ redis.pid
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in later.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Erol Fornoles
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,80 @@
1
+ # Later
2
+
3
+ [**Later**](erol.github.com/later) is a Redis-backed event scheduling library for Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'later'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install later
18
+
19
+ ## Usage
20
+
21
+ If you're not using Bundler to require your gems, you will need to require Later in your application:
22
+
23
+ require 'later'
24
+
25
+ Later allows you to manage multiple schedule sets:
26
+
27
+ Later[:reservations]
28
+ Later[:appointments]
29
+
30
+ The schedule sets are referenced based on the default Redis instance. If you need a schedule set which resides on a different Redis instance, you can pass a [Nest](github.com/soveran/nest) object.
31
+
32
+ redis = Redis.new host: host, port: port
33
+ key = Nest.new :reservations, redis
34
+
35
+ Later[key]
36
+
37
+ Setting an event schedule is simple:
38
+
39
+ Later[key].set 'my-unique-event', Time.now + 60
40
+
41
+ And can be reset with equal simplicity:
42
+
43
+ Later[key].set 'my-unique-event', Time.now + 120
44
+
45
+ An event schedule can also be unset:
46
+
47
+ Later[key].unset 'my-unique-event'
48
+
49
+ Note that event names should be unique in the scope of the schedule set.
50
+
51
+ ### Workers
52
+
53
+ Workers are Ruby processes that run forever. They allow you to process event schedules in the background:
54
+
55
+ require 'later'
56
+
57
+ Later[:reservations].each do |event|
58
+ # Do something with the event.
59
+ end
60
+
61
+ # This line is never reached.
62
+
63
+ If for some reason, a worker has to stop itself from running:
64
+
65
+ Later[:reservations].each do |event|
66
+ # Do something with the event.
67
+
68
+ Later[:reservations].stop if stop?
69
+ end
70
+
71
+ # This line is reached if stop? is true.
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it
76
+ 2. Create your feature branch ( `git checkout -b my-new-feature` )
77
+ 3. Create tests and make them pass ( `rake test` )
78
+ 4. Commit your changes ( `git commit -am 'Added some feature'` )
79
+ 5. Push to the branch ( `git push origin my-new-feature` )
80
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/lib/*.rb']
9
+ t.verbose = true
10
+ end
data/later.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/later/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Erol Fornoles']
6
+ gem.email = ['erol.fornoles@gmail.com']
7
+ gem.description = %q{Later is a Redis-backed event scheduling library for Ruby}
8
+ gem.summary = %q{Later is a Redis-backed event scheduling library for Ruby}
9
+ gem.homepage = 'http://erol.github.com/later'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'later'
15
+ gem.require_paths = ['lib']
16
+ gem.version = Later::VERSION
17
+
18
+ gem.add_dependency 'redis'
19
+ gem.add_dependency 'nest'
20
+
21
+ gem.add_development_dependency 'minitest'
22
+ end
data/lib/later.rb ADDED
@@ -0,0 +1,80 @@
1
+ require 'later/version'
2
+ require 'redis'
3
+ require 'nest'
4
+
5
+ module Later
6
+ class Schedule
7
+ def key
8
+ @key
9
+ end
10
+
11
+ def initialize(key)
12
+ if key.is_a?(Nest)
13
+ @key = key
14
+ else
15
+ @key = Later.key[key]
16
+ end
17
+ end
18
+
19
+ def [](event)
20
+ Time.at key[:schedule].zscore(event) rescue nil
21
+ end
22
+
23
+ def count
24
+ key[:schedule].zcard
25
+ end
26
+
27
+ def set(event, time)
28
+ key[:schedule].zadd time.to_i, event
29
+ end
30
+
31
+ def unset(event)
32
+ key[:schedule].zrem event
33
+ end
34
+
35
+ def stop
36
+ @stop = true
37
+ end
38
+
39
+ def each(&block)
40
+ @stop = false
41
+
42
+ loop do
43
+ break if @stop
44
+
45
+ time = Time.now.to_i
46
+
47
+ key[:schedule].redis.multi
48
+ key[:schedule].zrangebyscore '-inf', time
49
+ key[:schedule].zremrangebyscore '-inf', time
50
+ ids = key[:schedule].redis.exec.first
51
+
52
+ key.redis.pipelined do
53
+ ids.each do |id|
54
+ key[:queue].lpush id
55
+ end
56
+ end
57
+
58
+ event = key[:queue].brpoplpush(key[:now], 1)
59
+
60
+ next unless event
61
+
62
+ block.call event
63
+ end
64
+ end
65
+ end
66
+
67
+ @schedules = {}
68
+
69
+ def self.key
70
+ Nest.new('Later')
71
+ end
72
+
73
+ def self.[](schedule)
74
+ if @schedules[schedule.to_sym]
75
+ @schedules[schedule.to_sym]
76
+ else
77
+ @schedules[schedule.to_sym] = Schedule.new schedule
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,3 @@
1
+ module Later
2
+ VERSION = '0.1.0'
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'bundler/setup'
2
+ require 'later'
3
+ require 'minitest/autorun'
4
+
5
+ class MiniTest::Unit::TestCase
6
+ def setup
7
+ system 'redis-server', File.join(File.dirname(__FILE__), 'redis.conf')
8
+ until File.exist? 'redis.pid'
9
+ sleep 1
10
+ end
11
+ end
12
+
13
+ def teardown
14
+ system "kill `cat redis.pid`"
15
+ sleep 1
16
+ end
17
+ end
data/test/lib/later.rb ADDED
@@ -0,0 +1,74 @@
1
+ require_relative '../helper.rb'
2
+
3
+ class LaterTest < MiniTest::Unit::TestCase
4
+ def setup
5
+ super
6
+
7
+ @redis = Redis.new host: '127.0.0.1', port: 6666
8
+ @key = Nest.new 'Events', @redis
9
+ end
10
+
11
+ def test_set_a_unique_event_schedule
12
+ time = Time.now
13
+
14
+ Later[@key].set '1', time
15
+
16
+ assert_equal 1, Later[@key].count
17
+ assert_in_delta time, Later[@key]['1'], 1
18
+ end
19
+
20
+ def test_unset_a_unique_event_schedule
21
+ time = Time.now
22
+
23
+ Later[@key].set '1', time
24
+ Later[@key].unset '1'
25
+
26
+ assert_equal 0, Later[@key].count
27
+ assert_equal nil, Later[@key]['1']
28
+ end
29
+
30
+ def test_count_set_and_unset_event_schedules
31
+ time = Time.now
32
+
33
+ 1.upto(10) do |i|
34
+ Later[@key].set i.to_s, time
35
+ assert_equal i, Later[@key].count
36
+ end
37
+
38
+ 1.upto(10) do |i|
39
+ Later[@key].unset i.to_s
40
+ assert_equal 10 - i, Later[@key].count
41
+ end
42
+ end
43
+
44
+ def test_process_many_event_schedules
45
+ start = Time.now + 20
46
+
47
+ times = []
48
+ schedules = Hash.new { |h,k| h[k] = [] }
49
+
50
+ 1.upto(1000) do |i|
51
+ time = start + i / 100
52
+ times.unshift time
53
+ schedules[time].unshift event: i.to_s, time: time
54
+ end
55
+
56
+ times.map{ |time| schedules[time] }.flatten.shuffle.each do |schedule|
57
+ Later[@key].set schedule[:event], schedule[:time]
58
+ end
59
+
60
+ Thread.new do
61
+ sleep 30
62
+ Later[@key].stop
63
+ end
64
+
65
+ Later[@key].each do |event|
66
+ time = times.pop
67
+
68
+ assert_in_delta time, Time.now, 1.2
69
+ assert_includes schedules[time], {event: event, time: time}
70
+ end
71
+
72
+ assert_equal 0, Later[@key].count
73
+ end
74
+ end
data/test/redis.conf ADDED
@@ -0,0 +1,3 @@
1
+ daemonize yes
2
+ port 6666
3
+ pidfile ./redis.pid
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: later
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Erol Fornoles
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: redis
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: nest
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Later is a Redis-backed event scheduling library for Ruby
63
+ email:
64
+ - erol.fornoles@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - later.gemspec
75
+ - lib/later.rb
76
+ - lib/later/version.rb
77
+ - test/helper.rb
78
+ - test/lib/later.rb
79
+ - test/redis.conf
80
+ homepage: http://erol.github.com/later
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Later is a Redis-backed event scheduling library for Ruby
104
+ test_files:
105
+ - test/helper.rb
106
+ - test/lib/later.rb
107
+ - test/redis.conf