deferrer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MWYxNDY1MDIxNTJkNTJiMmFkYWQyMWFmZWQ5Y2ZkZjMwMmE1MTkwZQ==
5
+ data.tar.gz: !binary |-
6
+ MjE0OWFiYjA1NjlkMmE0NjgwMzViMDY4ZjVmYTdhODQzNzdjYjJmMg==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ NjMzNmU0ZmVjOWRiYWVhODY1YzU4MmY5OTRmNzNhYTdiNTMwMmUzMTdlOTE3
10
+ NWIxNjI4MGI1ODkxNDVhODFhYjJjMDkyMzNlYTg1MDVkODU2NzY1YTMyMzEx
11
+ NGUxNzNjMjY5MWRjZDBhNDMwYzQ2YjZhMjkzN2EwMTg1ZDNmZjc=
12
+ data.tar.gz: !binary |-
13
+ YWEzOTI3NWVjNjk3OTVjMDdjOGM4NGU1ZTA2ZDFmOWE3OGE2NDM0NTJiZjkx
14
+ YTY5Y2Q1NmUzOTgzNjE0ZjZjNjY3YjYzZDlmNDAxNWJjY2Y2ZjZlNmM1Zjdm
15
+ NGNjM2E5ZmU5NTEwY2NjZGE3YjE3NzliNDI5YjBjZjZmYTNlN2Y=
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Dalibor Nasevic
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.
@@ -0,0 +1,63 @@
1
+ # Deferrer
2
+
3
+ Defer executions and run only the last update at the scheduled time
4
+
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'deferrer'
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install deferrer
20
+
21
+
22
+
23
+ ## Usage
24
+
25
+ Configure redis
26
+
27
+ Deferrer.redis_config = { :host => "localhost", :port => 6379 }
28
+
29
+
30
+ Define deferrer class (must have perform class method)
31
+
32
+ class NameDeferrer
33
+ def self.perform(first_name, last_name)
34
+ puts "#{first_name} #{last_name}".upcase
35
+ end
36
+ end
37
+
38
+
39
+ Start a worker process. It needs to have redis configured and access to deferrer classes.
40
+
41
+ Deferrer.run
42
+
43
+
44
+ Defer some executions
45
+
46
+ Deferrer.defer_in(5, 'identifier', NameDeferrer, 'User', '1')
47
+ Deferrer.defer_in(6, 'identifier', NameDeferrer, 'User', '2')
48
+ Deferrer.defer_in(9, 'identifier', NameDeferrer, 'User', '3')
49
+
50
+
51
+ It will stack all defered executions per identifier until first timeout expires (5 seconds) and then it will only execute the last update for the expired identifier:
52
+
53
+ NameDeferrer.perform('User', '3') => USER 3
54
+
55
+
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deferrer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "deferrer"
8
+ spec.version = Deferrer::VERSION
9
+ spec.authors = ["Dalibor Nasevic"]
10
+ spec.email = ["dalibor.nasevic@gmail.com"]
11
+ spec.description = %q{Defer executions and run only the last update}
12
+ spec.summary = %q{Defer executions and run only the last update at the scheduled time}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "redis"
22
+ spec.add_dependency "multi_json"
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,9 @@
1
+ # Example
2
+
3
+ 1. Open first terminal and run:
4
+
5
+ bundle exec ruby runner.rb
6
+
7
+ 2. Open second terminal and run:
8
+
9
+ bundle exec ruby client.rb
@@ -0,0 +1,8 @@
1
+ require 'deferrer'
2
+ require_relative './name_deferrer'
3
+
4
+ Deferrer.redis_config = { :host => "localhost", :port => 6379 }
5
+
6
+ Deferrer.defer_in(5, 'identifier', NameDeferrer, 'User', '1')
7
+ Deferrer.defer_in(6, 'identifier', NameDeferrer, 'User', '2')
8
+ Deferrer.defer_in(9, 'identifier', NameDeferrer, 'User', '3')
@@ -0,0 +1,9 @@
1
+ # setup redis
2
+ Deferrer.redis_config = { :host => "localhost", :port => 6379 }
3
+
4
+ # define deferrer class (must have perform class method)
5
+ class NameDeferrer
6
+ def self.perform(first_name, last_name)
7
+ puts "#{first_name} #{last_name}".upcase
8
+ end
9
+ end
@@ -0,0 +1,21 @@
1
+ require 'deferrer'
2
+ require_relative './name_deferrer'
3
+
4
+ Deferrer.redis_config = { :host => "localhost", :port => 6379 }
5
+
6
+ class Logger
7
+ def self.info(message)
8
+ puts "INFO: #{message}"
9
+ end
10
+
11
+ def self.error(message)
12
+ puts "ERROR: #{message}"
13
+ end
14
+ end
15
+
16
+ puts 'Runner started'
17
+
18
+ Deferrer.run({
19
+ :loop_frequency => 0.5,
20
+ :logger => Logger
21
+ })
@@ -0,0 +1,13 @@
1
+ require 'redis'
2
+ require "deferrer/version"
3
+
4
+ module Deferrer
5
+
6
+ autoload :Configuration, 'deferrer/configuration'
7
+ autoload :JsonEncoding, 'deferrer/json_encoding'
8
+ autoload :Deferral, 'deferrer/deferral'
9
+
10
+ extend Configuration
11
+ extend JsonEncoding
12
+ extend Deferral
13
+ end
@@ -0,0 +1,14 @@
1
+ module Deferrer
2
+ module Configuration
3
+
4
+ # Deferrer.redis_config = { :host => "localhost", :port => 6379 }
5
+ def redis_config=(config)
6
+ @redis = Redis.new(config)
7
+ end
8
+
9
+ # Returns the configured Redis instance
10
+ def redis
11
+ @redis
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,90 @@
1
+ module Deferrer
2
+ module Deferral
3
+
4
+ LIST_KEY = :deferred_list
5
+
6
+ def run(options = {})
7
+ loop_frequency = options[:loop_frequency] || 0.1
8
+ logger = options[:logger] || nil
9
+
10
+ loop do
11
+ while item = next_item
12
+ begin
13
+ klass = constantize(item['class'])
14
+ args = item['args']
15
+
16
+ logger.info("Executing: #{item['key']}") if logger
17
+
18
+ klass.send(:perform, *args)
19
+ rescue Exception => e
20
+ logger.error("Error: #{e.class}: #{e.detail}") if logger
21
+ end
22
+ end
23
+
24
+ sleep loop_frequency
25
+ end
26
+ end
27
+
28
+ def next_item
29
+ item = nil
30
+ decoded_item = nil
31
+ score = calculate_score(Time.now)
32
+
33
+ key = redis.zrangebyscore(LIST_KEY, '-inf', score, :limit => [0, 1]).first
34
+
35
+ if key
36
+ _, item = redis.brpop(key, 0)
37
+ decoded_item = decode(item) if item
38
+ decoded_item['key'] = key
39
+
40
+ remove(key)
41
+ end
42
+
43
+ decoded_item
44
+ end
45
+
46
+ def constantize(klass_string)
47
+ klass_string.split('::').inject(Object) {|memo,name| memo = memo.const_get(name); memo}
48
+ end
49
+
50
+ def defer_in(number_of_seconds_from_now, identifier, klass, *args)
51
+ timestamp = Time.now + number_of_seconds_from_now
52
+ defer_at(timestamp, identifier, klass, *args)
53
+ end
54
+
55
+ def defer_at(timestamp, identifier, klass, *args)
56
+ item = build_item(klass, args)
57
+ key = item_key(identifier)
58
+ score = calculate_score(timestamp)
59
+
60
+ count = redis.rpush(key, encode(item))
61
+
62
+ # set score only on first update
63
+ if count == 1
64
+ redis.zadd(LIST_KEY, score, key)
65
+ end
66
+ end
67
+
68
+ def item_key(identifier)
69
+ "deferred:#{identifier}"
70
+ end
71
+
72
+ private
73
+ def build_item(klass, args)
74
+ {'class' => klass.to_s, 'args' => args}
75
+ end
76
+
77
+ def calculate_score(timestamp)
78
+ timestamp.to_f
79
+ end
80
+
81
+ def remove(key)
82
+ redis.watch(key)
83
+ redis.multi do
84
+ redis.del(key)
85
+ redis.zrem(LIST_KEY, key)
86
+ end
87
+ redis.unwatch
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,20 @@
1
+ require 'multi_json'
2
+
3
+ module Deferrer
4
+ module JsonEncoding
5
+
6
+ class DecodeException < StandardError; end
7
+
8
+ def encode(item)
9
+ MultiJson.dump(item)
10
+ end
11
+
12
+ def decode(item)
13
+ begin
14
+ MultiJson.load(item)
15
+ rescue ::MultiJson::DecodeError => e
16
+ raise DecodeException, e.message, e.backtrace
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module Deferrer
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+
3
+ class CarDeferrer
4
+ def self.perform(car)
5
+ car.upcase
6
+ end
7
+ end
8
+
9
+ describe Deferrer::Deferral do
10
+ let(:car) { 'car' }
11
+ let(:car2) { 'car2' }
12
+ let(:identifier) { 'car1' }
13
+ let(:redis) { Deferrer.redis }
14
+ let(:list_key) { Deferrer::Deferral::LIST_KEY }
15
+
16
+ before :each do
17
+ redis.flushall
18
+ end
19
+
20
+ describe ".defer_at" do
21
+ it "deferrs at given time" do
22
+ Deferrer.defer_at(Time.now, identifier, CarDeferrer, car)
23
+
24
+ redis.zrangebyscore(list_key, '-inf', Time.now.to_f, :limit => [0, 1]).first.should_not be_nil
25
+ redis.exists(Deferrer.item_key(identifier)).should be_true
26
+ end
27
+
28
+ it "deferrs in given interval" do
29
+ Deferrer.defer_in(1, identifier, CarDeferrer, car)
30
+
31
+ redis.zrangebyscore(list_key, '-inf', (Time.now + 1).to_f, :limit => [0, 1]).first.should_not be_nil
32
+ redis.exists(Deferrer.item_key(identifier)).should be_true
33
+ end
34
+ end
35
+
36
+ describe ".next_item" do
37
+ it "returns the next item" do
38
+ Deferrer.defer_at(Time.now, identifier, CarDeferrer, car)
39
+
40
+ item = Deferrer.next_item
41
+
42
+ item['class'].should == CarDeferrer.to_s
43
+ item['args'].should == [car]
44
+ end
45
+
46
+ it "returns last update of an item" do
47
+ Deferrer.defer_at(Time.now - 3, identifier, CarDeferrer, car)
48
+ Deferrer.defer_at(Time.now - 2, identifier, CarDeferrer, car2)
49
+
50
+ item = Deferrer.next_item
51
+
52
+ item['class'].should == CarDeferrer.to_s
53
+ item['args'].should == [car2]
54
+ end
55
+
56
+ it "keep the old score value" do
57
+ Deferrer.defer_at(Time.now - 3, identifier, CarDeferrer, car)
58
+ Deferrer.defer_at(Time.now + 1, identifier, CarDeferrer, car2)
59
+
60
+ Deferrer.next_item.should_not be_nil
61
+ end
62
+
63
+ it "returns nil when no next item" do
64
+ Deferrer.next_item.should be_nil
65
+ end
66
+
67
+ it "removes values from redis" do
68
+ Deferrer.defer_at(Time.now, identifier, CarDeferrer, car)
69
+
70
+ item = Deferrer.next_item
71
+
72
+ redis.zrangebyscore(list_key, '-inf', Time.now.to_f, :limit => [0, 1]).first.should be_nil
73
+ redis.exists(Deferrer.item_key(identifier)).should be_false
74
+ Deferrer.next_item.should be_nil
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ require 'deferrer'
2
+
3
+ Deferrer.redis_config = { :host => "localhost", :port => 6379 }
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deferrer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dalibor Nasevic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: multi_json
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Defer executions and run only the last update
70
+ email:
71
+ - dalibor.nasevic@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - deferrer.gemspec
83
+ - example/README.md
84
+ - example/client.rb
85
+ - example/name_deferrer.rb
86
+ - example/runner.rb
87
+ - lib/deferrer.rb
88
+ - lib/deferrer/configuration.rb
89
+ - lib/deferrer/deferral.rb
90
+ - lib/deferrer/json_encoding.rb
91
+ - lib/deferrer/version.rb
92
+ - spec/deferrer/deferral_spec.rb
93
+ - spec/spec_helper.rb
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.0.0
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Defer executions and run only the last update at the scheduled time
118
+ test_files:
119
+ - spec/deferrer/deferral_spec.rb
120
+ - spec/spec_helper.rb