calloutd 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/calloutd +4 -0
- data/lib/calloutd.rb +230 -0
- metadata +109 -0
data/bin/calloutd
ADDED
data/lib/calloutd.rb
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# calloutd
|
2
|
+
# Aysnchronized call implementation
|
3
|
+
# Author: Jackie.ju@gmail.com
|
4
|
+
#
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'launch_job'
|
8
|
+
require 'rubyutility'
|
9
|
+
require 'json'
|
10
|
+
=begin
|
11
|
+
class Callouts
|
12
|
+
def add(obj, method, delay=0, p=nil)
|
13
|
+
@s = "" if !@s
|
14
|
+
@s+= gen_callout_string(obj, method, delay, p)+"\n"
|
15
|
+
end
|
16
|
+
|
17
|
+
def save
|
18
|
+
dir = "#{g_FILEROOT}/globaldata/q"
|
19
|
+
fname="#{dir}/#{Time.now.to_f}.callout"
|
20
|
+
save_to_file(@s, fname) if @s && @s != ""
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
=end
|
26
|
+
def err(m)
|
27
|
+
p "err!:#{m}"
|
28
|
+
print m.backtrace.join("\n")
|
29
|
+
end
|
30
|
+
class CallOut
|
31
|
+
@@callout_fs_root = "."
|
32
|
+
class << self
|
33
|
+
def fs_root
|
34
|
+
return @@callout_fs_root if @@callout_fs_root
|
35
|
+
return "."
|
36
|
+
end
|
37
|
+
def set_fs_root(dir)
|
38
|
+
@@callout_fs_root = dir
|
39
|
+
end
|
40
|
+
def gen_callout_string(obj, method, delay=0, p=nil)
|
41
|
+
oid = ""
|
42
|
+
if obj !=nil
|
43
|
+
if obj.class == String
|
44
|
+
oid = obj
|
45
|
+
else
|
46
|
+
oid = obj.obj_id
|
47
|
+
end
|
48
|
+
end
|
49
|
+
s = "#{oid}|:|#{method.to_s}|:|#{delay}|:|#{p.to_json}"
|
50
|
+
p s
|
51
|
+
return s
|
52
|
+
end
|
53
|
+
|
54
|
+
def callout(obj, method, delay=0, *p)
|
55
|
+
s = gen_callout_string(obj, method, delay, p)
|
56
|
+
fname="#{fs_root}/#{Time.now.to_f}.callout"
|
57
|
+
save_to_file(s, fname)
|
58
|
+
dir = get_dir(fname)
|
59
|
+
|
60
|
+
p "save callout to #{fname}"
|
61
|
+
end
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
def pick_callouts(&block)
|
66
|
+
# p "===>pick_callouts"
|
67
|
+
dir = "#{fs_root}/*.callout"
|
68
|
+
files = Dir[dir].sort_by{|c|
|
69
|
+
File.stat(c).ctime #sort by change time
|
70
|
+
}
|
71
|
+
|
72
|
+
return if !files or files.size < 1
|
73
|
+
|
74
|
+
files.each{|fname|
|
75
|
+
# fname = nil
|
76
|
+
# if files.size > 0
|
77
|
+
# fname = files.first
|
78
|
+
# end
|
79
|
+
# p "===>pick_callouts1:#{fname}"
|
80
|
+
# p files.first
|
81
|
+
# p File.stat(files.first).ctime
|
82
|
+
#p files.last # newset
|
83
|
+
#File.stat(files.last).ctime
|
84
|
+
|
85
|
+
return if !fname
|
86
|
+
pure_fname = fname.split("/").last
|
87
|
+
f_tm = pure_fname.gsub(".callout", "").to_f
|
88
|
+
p "tm:#{f_tm}"
|
89
|
+
begin
|
90
|
+
p "fname:#{fname}"
|
91
|
+
if FileTest::exists?(fname)
|
92
|
+
|
93
|
+
data = nil
|
94
|
+
open(fname, "r+") {|f|
|
95
|
+
str = f.read
|
96
|
+
if str
|
97
|
+
ar = str.split("|:|")
|
98
|
+
|
99
|
+
data = {
|
100
|
+
:tm=>f_tm,
|
101
|
+
:oid=>ar[0],
|
102
|
+
:method=>ar[1],
|
103
|
+
:delay=>ar[2].to_i,
|
104
|
+
|
105
|
+
}
|
106
|
+
if ar[3]
|
107
|
+
data[:p] = JSON.parse(ar[3])
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# TODO maybe need reserve error callouts
|
112
|
+
if data ==nil || data[:method] == nil
|
113
|
+
p "delete callout file #{fname}"
|
114
|
+
File.delete(fname)
|
115
|
+
next
|
116
|
+
end
|
117
|
+
|
118
|
+
if data
|
119
|
+
p "done21"
|
120
|
+
r = yield(data)
|
121
|
+
p "done22"
|
122
|
+
if r== true
|
123
|
+
p "delete callout file #{fname}"
|
124
|
+
|
125
|
+
File.delete(fname)
|
126
|
+
end
|
127
|
+
else
|
128
|
+
p "delete callout file #{fname}"
|
129
|
+
|
130
|
+
File.delete(fname)
|
131
|
+
end
|
132
|
+
|
133
|
+
# File.delete(fname)
|
134
|
+
break
|
135
|
+
|
136
|
+
|
137
|
+
}
|
138
|
+
end
|
139
|
+
rescue Exception=>e
|
140
|
+
err(e)
|
141
|
+
p e.inspect
|
142
|
+
File.delete(fname)
|
143
|
+
|
144
|
+
end
|
145
|
+
}
|
146
|
+
end
|
147
|
+
|
148
|
+
|
149
|
+
|
150
|
+
# to be overrite
|
151
|
+
def get_obj(oid)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
def calloutd
|
157
|
+
CallOut.pick_callouts(){|data|
|
158
|
+
p data.inspect
|
159
|
+
if data[:method]
|
160
|
+
if data[:delay] && Time.now.to_f - data[:tm] < data[:delay]
|
161
|
+
next false
|
162
|
+
end
|
163
|
+
# p "oid=>#{data[:oid]}"
|
164
|
+
if (data[:oid] && data[:oid] !="")
|
165
|
+
n = CallOut.get_obj(data[:oid])
|
166
|
+
if n && n.respond_to?(data[:method])
|
167
|
+
begin
|
168
|
+
if p
|
169
|
+
n.send(data[:method])
|
170
|
+
else
|
171
|
+
n.send(data[:method], *data[:p])
|
172
|
+
|
173
|
+
end
|
174
|
+
rescue Exception=>e
|
175
|
+
err(e)
|
176
|
+
end
|
177
|
+
next true
|
178
|
+
end
|
179
|
+
else
|
180
|
+
begin
|
181
|
+
if p
|
182
|
+
Object.send(data[:method])
|
183
|
+
else
|
184
|
+
Object.send(data[:method], *data[:p])
|
185
|
+
|
186
|
+
end
|
187
|
+
|
188
|
+
rescue Exception=>e
|
189
|
+
err(e)
|
190
|
+
end
|
191
|
+
next true
|
192
|
+
end
|
193
|
+
end
|
194
|
+
next true
|
195
|
+
}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
end
|
200
|
+
def launch_calloutd
|
201
|
+
launch_job("calloutd", 0.01)
|
202
|
+
end
|
203
|
+
|
204
|
+
=begin
|
205
|
+
# test
|
206
|
+
class CallOut
|
207
|
+
def self.get_obj(oid)
|
208
|
+
p "get_obj"
|
209
|
+
return nil
|
210
|
+
end
|
211
|
+
end
|
212
|
+
def testcallout1()
|
213
|
+
p "===>testcallout1"
|
214
|
+
end
|
215
|
+
def testcallout(p1, p2)
|
216
|
+
p "===>#{p1}===>#{p2}"
|
217
|
+
end
|
218
|
+
# launch_calloutd
|
219
|
+
# sleep(3)
|
220
|
+
CallOut.callout(nil, :testcallout1, 3)
|
221
|
+
# CallOut.callout(nil, :testcallout, 0, 1111, 2222)
|
222
|
+
calloutd
|
223
|
+
sleep(1)
|
224
|
+
calloutd
|
225
|
+
sleep(1)
|
226
|
+
calloutd
|
227
|
+
sleep(1)
|
228
|
+
calloutd
|
229
|
+
sleep(1)
|
230
|
+
=end
|
metadata
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: calloutd
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Ju Weihua (Jackie Ju)
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2016-01-05 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: launch_job
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: rubyutility
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: json
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
type: :runtime
|
62
|
+
version_requirements: *id003
|
63
|
+
description: Aynchronized call !
|
64
|
+
email: jackie.ju@gmail.com
|
65
|
+
executables:
|
66
|
+
- calloutd
|
67
|
+
extensions: []
|
68
|
+
|
69
|
+
extra_rdoc_files: []
|
70
|
+
|
71
|
+
files:
|
72
|
+
- lib/calloutd.rb
|
73
|
+
- bin/calloutd
|
74
|
+
has_rdoc: true
|
75
|
+
homepage: http://rubygems.org/gems/calloutd
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
version: "0"
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
100
|
+
version: "0"
|
101
|
+
requirements: []
|
102
|
+
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.3.7
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Aynchronized call !
|
108
|
+
test_files: []
|
109
|
+
|