concurrent-wrapper 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ ROOT = File.expand_path(File.dirname(__FILE__))
2
+
3
+ require File.join(ROOT, 'concurrent', 'client')
4
+ require File.join(ROOT, 'concurrent', 'ticket')
5
+ require File.join(ROOT, 'concurrent', 'redis_key')
@@ -0,0 +1,57 @@
1
+ require 'redis'
2
+ require File.join(ROOT, 'concurrent', 'ticket')
3
+
4
+ class Concurrent
5
+ class << self
6
+ attr_accessor :redis_path, :redis_port
7
+ end
8
+
9
+ def self.config
10
+ yield self
11
+ end
12
+
13
+ def self.sync(uniq_tag)
14
+ ticket = Ticket.new(uniq_tag)
15
+
16
+ ticket.dispatch
17
+
18
+ pull_ticket(uniq_tag)
19
+
20
+ yield
21
+
22
+ ticket.notify
23
+ end
24
+
25
+ def self.pull_ticket(uniq_tag)
26
+ redis.brpop "client:#{uniq_tag}:list", 0
27
+ end
28
+
29
+ def self.redis(redis_host = '127.0.0.1', redis_port = 6379)
30
+ @@redis ||= Redis.new :host => redis_host, :port => redis_port
31
+ end
32
+ end
33
+
34
+
35
+ #require 'active_record'
36
+ #require 'mysql2'
37
+
38
+ #ActiveRecord::Base.establish_connection(
39
+ #:adapter => 'mysql2',
40
+ #:host => 'localhost',
41
+ #:suername => 'root',
42
+ #:password => '',
43
+ #:database => 'flames_develop',
44
+ #:pool => 150
45
+ #)
46
+
47
+ #class Profile < ActiveRecord::Base
48
+ #end
49
+
50
+ #uniq_tag = 'user:1'
51
+ #1000.times do |i|
52
+ #Concurrent.sync(uniq_tag) do
53
+ #puts "sync: #{i + 1}"
54
+ #Profile.first.increment! :gems, 1
55
+ #puts '>>>>>executing....'
56
+ #end
57
+ #end
@@ -0,0 +1,13 @@
1
+ module RedisKey
2
+ def blocking_key
3
+ "blocking:#{@uniq_tag}:list"
4
+ end
5
+
6
+ def executing_key
7
+ "executing:#{@uniq_tag}:list"
8
+ end
9
+
10
+ def client_key
11
+ "client:#{@uniq_tag}:list"
12
+ end
13
+ end
@@ -0,0 +1,51 @@
1
+ #require File.expand_path './redis_key'
2
+ require File.join(ROOT, 'concurrent', 'redis_key')
3
+
4
+ class Ticket
5
+ include RedisKey
6
+
7
+ #uniq_tag desc:
8
+ # The concurrent objects who point to same reference,
9
+ # or represent same row in database must use same uniq_tag.
10
+ #
11
+ # NOTICE:
12
+ # We have to thing the different databases use the same table name and have the same row id,
13
+ # so if we use the in multi database for control concurrence we have to specify database's uniq tag.
14
+ #
15
+
16
+ def initialize(uniq_tag)
17
+ @uniq_tag = uniq_tag
18
+ end
19
+
20
+ def dispatch
21
+ executing ? push_to_client : blocking
22
+ end
23
+
24
+ def blocking
25
+ redis.lpush blocking_key, @uniq_tag
26
+ end
27
+
28
+ def pop_blocking
29
+ redis.lpop blocking_key
30
+ end
31
+
32
+ def executing
33
+ redis.sadd executing_key, @uniq_tag
34
+ end
35
+
36
+ def rem_executing
37
+ redis.srem executing_key, @uniq_tag
38
+ end
39
+
40
+ def push_to_client
41
+ redis.lpush client_key, @uniq_tag
42
+ end
43
+
44
+ def notify
45
+ pop_blocking ? push_to_client : rem_executing
46
+ end
47
+
48
+ def redis
49
+ Concurrent.redis
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: concurrent-wrapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Savin Max
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-21 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: concurrent-wrapper make concurrence oriented programming easy. With the
15
+ wrapper you even don't need to think about concurrence, just live the code in the
16
+ wraper
17
+ email: mafei.198@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/concurrent.rb
23
+ - lib/concurrent/client.rb
24
+ - lib/concurrent/ticket.rb
25
+ - lib/concurrent/redis_key.rb
26
+ homepage: http://rubygems.org/gems/concurrent-wrapper
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.24
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: concurrence oriented programming wrapper
50
+ test_files: []