medularis-daemons_common 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ ## (12-07-2011) Claudio Alvarez (claudio at medularis.com)
2
+ * redis >= 2.2.0 is now allowed.
3
+
4
+ ## (23-05-2011) Claudio Alvarez (claudio at medularis.com)
5
+ * Version 0.0.4.
6
+ * Added functions for facilitating Ruby 1.9/1.8 compatibility.
7
+ * Added functions for facilitating debugging.
8
+
9
+ ## (17-05-2011) Claudio Alvarez (claudio at medularis.com)
10
+ * Changed required version of resque to 1.16.1.
11
+ * Corrected gem dependency errors.
12
+ * Increased version number to 0.0.3.
13
+ * Changed required version of json to 1.5.1.
14
+
15
+ ## (02-05-2011) Claudio Alvarez (claudio at medularis.com)
16
+ * Updated dependencies in gemspec.
17
+
18
+ ## (02-05-2011) Claudio Alvarez (claudio at medularis.com)
19
+ * Added constant `MedularisDaemonsCommon::ESL_EVENT_VARIABLE_PREFIX`.
20
+ * Added ThreadPool class to the `MedularisDaemonsCommon` module.
21
+
22
+ ## (27-04-2011) Claudio Alvarez (claudio at medularis.com)
23
+ * Adding `HISTORY.md` file to the project.
24
+ * Adding `MedularisDaemonsCommon::FSConnection`, which encapsulates connections to FreeSWITCH's ESL.
Binary file
@@ -0,0 +1,6 @@
1
+ #!/bin/bash
2
+
3
+ rm *.gem
4
+ gem build medularis-daemons_common.gemspec
5
+ cp *.gem ../medularis-core-deployment/server/common/ruby-install/localgems
6
+
@@ -0,0 +1,27 @@
1
+ # SCM_ID: $Id$
2
+ # encoding: UTF-8
3
+
4
+ def this_method
5
+ method = caller[0]
6
+ pieces = method.split('/')
7
+
8
+ if (pieces.length >= 1)
9
+ method = pieces[pieces.length - 1]
10
+ end
11
+
12
+ return "[#{method}]"
13
+ end
14
+
15
+ def this_method_w_timestamp
16
+ method = caller[0]
17
+ pieces = method.split('/')
18
+
19
+ if (pieces.length >= 1)
20
+ method = pieces[pieces.length - 1]
21
+ end
22
+
23
+ time = Time.now
24
+ strtime = time.strftime("%H:%M:%S")
25
+
26
+ return "[#{strtime} #{method}]"
27
+ end
@@ -0,0 +1,54 @@
1
+ # SCM_ID: $Id$
2
+ # encoding: UTF-8
3
+ module MedularisDaemonsCommon
4
+ require('ruby_compat')
5
+ require(File.join(File.expand_path('../../bin/freeSWITCH', __FILE__),'ESL'))
6
+
7
+ # Singleton for FS event socket access
8
+ class FSConnection
9
+
10
+ def self.instance(host = 'localhost', port = '8021', password = 'ClueCon')
11
+
12
+ @@singleton__mutex__.synchronize {
13
+ if @@singleton__instance__
14
+ return @@singleton__instance__
15
+ end
16
+
17
+ @@singleton__instance__ = FSConnection.new(host, port, password)
18
+ }
19
+
20
+ return @@singleton__instance__
21
+ end
22
+
23
+ def connection
24
+ if (@connection.nil?)
25
+ ruby_18 { @connection = ESL::ESLconnection.new(@host, @port, @password) } ||
26
+ ruby_19 { @connection = ESL::ESLconnection.new(@host.force_encoding("US-ASCII"), @port.force_encoding("US-ASCII"), @password.force_encoding("US-ASCII")) }
27
+
28
+ return @connection
29
+ else
30
+ return @connection
31
+ end
32
+ end
33
+
34
+ private
35
+ # private_class_method :new
36
+
37
+ def initialize(host, port, password)
38
+ @host = host
39
+ @port = port
40
+ @password = password
41
+ end
42
+
43
+ @host
44
+ @port
45
+ @password
46
+ @connection = nil
47
+
48
+ @@singleton__instance__ = nil
49
+ @@singleton__mutex__ = Mutex.new
50
+ end
51
+
52
+ FSCONNECTION = FSConnection.instance.connection
53
+ end
54
+
@@ -0,0 +1,9 @@
1
+ # SCM_ID: $Id:$
2
+ require('debug_utils')
3
+ require('fs_connection')
4
+ require('threadpool')
5
+ require('ruby_compat')
6
+
7
+ module MedularisDaemonsCommon
8
+ ESL_EVENT_VARIABLE_PREFIX = 'variable_'
9
+ end
@@ -0,0 +1,5 @@
1
+ # SCM_ID: $Id: $
2
+ # encoding: UTF-8
3
+ module MedularisDaemonsCommon
4
+ Version = '0.0.4'
5
+ end
@@ -0,0 +1,20 @@
1
+ # SCM_ID: $Id$
2
+ # encoding: UTF-8
3
+
4
+ if RUBY_VERSION < "1.9"
5
+ def ruby_18
6
+ yield
7
+ end
8
+
9
+ def ruby_19
10
+ false
11
+ end
12
+ else
13
+ def ruby_18
14
+ false
15
+ end
16
+
17
+ def ruby_19
18
+ yield
19
+ end
20
+ end
@@ -0,0 +1,123 @@
1
+ # SCM_ID: $Id$
2
+ module MedularisDaemonsCommon
3
+ require 'thread'
4
+
5
+ # Work item to be executed in thread pool
6
+ class ThreadPoolJob
7
+ attr_accessor :handler
8
+ attr_accessor :params
9
+
10
+ def initialize(*params, &block)
11
+ @params = params
12
+ @handler = block
13
+ end
14
+ end
15
+
16
+
17
+ # Launches a specified number of threads on instantiation,
18
+ # assigning work to them as it arrives
19
+ class ThreadPool
20
+
21
+ # Encapsulate each thread pool thread in object
22
+ class ThreadPoolJobRunner
23
+ attr_accessor :time_started
24
+
25
+ def initialize(thread_pool)
26
+ @thread_pool = thread_pool
27
+ @timeout_lock = Mutex.new
28
+ @thread_lock = Mutex.new
29
+ end
30
+
31
+ def run
32
+ @thread_lock.synchronize {
33
+ @thread = Thread.new {
34
+ until @thread_pool.terminate
35
+ @timeout_lock.synchronize { @time_started = nil }
36
+ work = @thread_pool.next_job
37
+ @timeout_lock.synchronize { @time_started = Time.now }
38
+ work.handler.call *work.params unless work.nil?
39
+ end
40
+ }
41
+ }
42
+ end
43
+
44
+ def check_timeout(timeout)
45
+ @timeout_lock.synchronize {
46
+ if !@time_started.nil? && Time.now - @time_started > timeout
47
+ stop
48
+ run
49
+ end
50
+ }
51
+ end
52
+
53
+ def stop
54
+ @thread_lock.synchronize {
55
+ if @thread.alive?
56
+ @thread.kill
57
+ @thread.join
58
+ end
59
+ }
60
+ end
61
+ end
62
+
63
+ # Create a thread pool with a specified number of threads
64
+ def initialize(num_threads, args = {})
65
+ @num_threads = num_threads
66
+ @timeout = args[:timeout]
67
+ @job_runners = []
68
+ @job_runners_lock = Mutex.new
69
+ @terminate = false
70
+ @terminate_lock = Mutex.new
71
+
72
+ @work_queue = Queue.new
73
+
74
+ 0.upto(@num_threads) { |i|
75
+ runner = ThreadPoolJobRunner.new(self)
76
+ @job_runners << runner
77
+ runner.run
78
+ }
79
+
80
+ # optional timeout thread
81
+ unless @timeout.nil?
82
+ @timeout_thread = Thread.new {
83
+ until terminate
84
+ sleep @timeout
85
+ @job_runners_lock.synchronize {
86
+ @job_runners.each { |jr|
87
+ jr.check_timeout(@timeout)
88
+ }
89
+ }
90
+ end
91
+ }
92
+ end
93
+ end
94
+
95
+ # terminate reader
96
+ def terminate
97
+ @terminate_lock.synchronize { @terminate }
98
+ end
99
+
100
+ # terminate setter
101
+ def terminate=(val)
102
+ @terminate_lock.synchronize { @terminate = val }
103
+ end
104
+
105
+ # Add work to the pool
106
+ def <<(work)
107
+ @work_queue.push work
108
+ end
109
+
110
+ # Return the next job queued up
111
+ def next_job
112
+ @work_queue.pop
113
+ end
114
+
115
+ # Terminate the thread pool
116
+ def stop
117
+ terminate = true
118
+ @timeout_thread.join unless @timout_thread.nil?
119
+ @work_queue.clear
120
+ @job_runners_lock.synchronize { @job_runners.each { |jr| jr.stop } }
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ # SCM_ID: $Id: $
3
+ require File.expand_path("../lib/medularis-daemons_common/version", __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = 'medularis-daemons_common'
7
+ gem.version = MedularisDaemonsCommon::Version
8
+ gem.date = Date.today.to_s
9
+
10
+ gem.summary = "Common utilities and functionality required by Medularis Click-To-Call Daemons."
11
+ gem.description = "Library contaning common features shared by the Medularis Click-To-Call Daemons, supporting the ClickFono service (http://www.clickfono.com)."
12
+
13
+ gem.authors = ['Medularis']
14
+ gem.email = 'info@medularis.com'
15
+ gem.homepage = 'http://www.medularis.com'
16
+
17
+ gem.add_dependency('rubygems-update', '= 1.4.1')
18
+ gem.add_dependency('rake','>= 0.8.7')
19
+ gem.add_dependency('json', '>= 1.5.1')
20
+ gem.add_dependency('log4r', '>= 1.1.9')
21
+ gem.add_dependency('mysql', '>= 2.8.1')
22
+ gem.add_dependency('redis', '>= 2.2.0')
23
+ gem.add_dependency('resque', '>= 1.16.1')
24
+ gem.add_dependency('resque-scheduler', '= 2.0.0.d')
25
+ gem.add_dependency('rest-open-uri', '>= 1.0.0')
26
+ gem.add_dependency('url', '>= 0.2.0')
27
+ gem.add_dependency('amqp', '>= 0.7.1')
28
+ gem.add_development_dependency('rspec', [">= 2.0.0"])
29
+
30
+ # ensure the gem is built out of versioned files1
31
+ # gem.files = Dir['{bin,lib,man,test,spec}/**/*', 'README*', 'LICENSE*'] & `git ls-files -z`.split("\0")
32
+ gem.files = `git ls-files -z`.split("\0")
33
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: medularis-daemons_common
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.4
6
+ platform: ruby
7
+ authors:
8
+ - Medularis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-07-12 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rubygems-update
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 1.4.1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.8.7
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: json
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: 1.5.1
47
+ type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: log4r
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 1.1.9
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: mysql
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 2.8.1
69
+ type: :runtime
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: redis
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: 2.2.0
80
+ type: :runtime
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: resque
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: 1.16.1
91
+ type: :runtime
92
+ version_requirements: *id007
93
+ - !ruby/object:Gem::Dependency
94
+ name: resque-scheduler
95
+ prerelease: false
96
+ requirement: &id008 !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - "="
100
+ - !ruby/object:Gem::Version
101
+ version: 2.0.0.d
102
+ type: :runtime
103
+ version_requirements: *id008
104
+ - !ruby/object:Gem::Dependency
105
+ name: rest-open-uri
106
+ prerelease: false
107
+ requirement: &id009 !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.0.0
113
+ type: :runtime
114
+ version_requirements: *id009
115
+ - !ruby/object:Gem::Dependency
116
+ name: url
117
+ prerelease: false
118
+ requirement: &id010 !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: 0.2.0
124
+ type: :runtime
125
+ version_requirements: *id010
126
+ - !ruby/object:Gem::Dependency
127
+ name: amqp
128
+ prerelease: false
129
+ requirement: &id011 !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: 0.7.1
135
+ type: :runtime
136
+ version_requirements: *id011
137
+ - !ruby/object:Gem::Dependency
138
+ name: rspec
139
+ prerelease: false
140
+ requirement: &id012 !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: 2.0.0
146
+ type: :development
147
+ version_requirements: *id012
148
+ description: Library contaning common features shared by the Medularis Click-To-Call Daemons, supporting the ClickFono service (http://www.clickfono.com).
149
+ email: info@medularis.com
150
+ executables: []
151
+
152
+ extensions: []
153
+
154
+ extra_rdoc_files: []
155
+
156
+ files:
157
+ - HISTORY.md
158
+ - bin/freeSWITCH/ESL.so
159
+ - build.sh
160
+ - lib/debug_utils.rb
161
+ - lib/fs_connection.rb
162
+ - lib/medularis-daemons_common.rb
163
+ - lib/medularis-daemons_common/version.rb
164
+ - lib/ruby_compat.rb
165
+ - lib/threadpool.rb
166
+ - medularis-daemons_common.gemspec
167
+ has_rdoc: true
168
+ homepage: http://www.medularis.com
169
+ licenses: []
170
+
171
+ post_install_message:
172
+ rdoc_options: []
173
+
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ none: false
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: "0"
182
+ required_rubygems_version: !ruby/object:Gem::Requirement
183
+ none: false
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: "0"
188
+ requirements: []
189
+
190
+ rubyforge_project:
191
+ rubygems_version: 1.4.2
192
+ signing_key:
193
+ specification_version: 3
194
+ summary: Common utilities and functionality required by Medularis Click-To-Call Daemons.
195
+ test_files: []
196
+