knjrbfw 0.0.86 → 0.0.87
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/knjrbfw.gemspec +2 -3
- data/lib/knj/datarow.rb +20 -5
- data/lib/knj/unix_proc.rb +16 -7
- data/lib/knj/webscripts/image.rhtml +11 -4
- metadata +3 -4
- data/lib/knj/threadpool.rb +0 -264
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.87
|
data/knjrbfw.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{knjrbfw}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.87"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Kasper Johansen"]
|
12
|
-
s.date = %q{2012-08-
|
12
|
+
s.date = %q{2012-08-19}
|
13
13
|
s.description = %q{Including stuff for HTTP, SSH and much more.}
|
14
14
|
s.email = %q{k@spernj.org}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -240,7 +240,6 @@ Gem::Specification.new do |s|
|
|
240
240
|
"lib/knj/thread.rb",
|
241
241
|
"lib/knj/thread2.rb",
|
242
242
|
"lib/knj/threadhandler.rb",
|
243
|
-
"lib/knj/threadpool.rb",
|
244
243
|
"lib/knj/translations.rb",
|
245
244
|
"lib/knj/unix_proc.rb",
|
246
245
|
"lib/knj/web.rb",
|
data/lib/knj/datarow.rb
CHANGED
@@ -760,17 +760,32 @@ class Knj::Datarow
|
|
760
760
|
#Various methods to define methods based on the columns for the datarow.
|
761
761
|
def self.define_translation_methods(args)
|
762
762
|
define_method("#{args[:val_dc]}=") do |newtransval|
|
763
|
-
|
764
|
-
|
765
|
-
|
763
|
+
begin
|
764
|
+
_hb.trans_set(self, {
|
765
|
+
args[:val] => newtransval
|
766
|
+
})
|
767
|
+
rescue NameError
|
768
|
+
_kas.trans_set(self, {
|
769
|
+
args[:val] => newtransval
|
770
|
+
})
|
771
|
+
end
|
766
772
|
end
|
767
773
|
|
768
774
|
define_method("#{args[:val_dc]}") do
|
769
|
-
|
775
|
+
begin
|
776
|
+
return _hb.trans(self, args[:val])
|
777
|
+
rescue NameError
|
778
|
+
return _kas.trans(self, args[:val])
|
779
|
+
end
|
770
780
|
end
|
771
781
|
|
772
782
|
define_method("#{args[:val_dc]}_html") do
|
773
|
-
|
783
|
+
begin
|
784
|
+
str = _hb.trans(self, args[:val])
|
785
|
+
rescue NameError
|
786
|
+
str = _kas.trans(self, args[:val])
|
787
|
+
end
|
788
|
+
|
774
789
|
if str.to_s.strip.length <= 0
|
775
790
|
return "[no translation for #{args[:val]}]"
|
776
791
|
end
|
data/lib/knj/unix_proc.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
#Require the 'wref'-gem if it hasnt already been.
|
2
2
|
require "wref" if !Kernel.const_defined?(:Wref)
|
3
3
|
|
4
4
|
#This class handels various stuff regarding Unix-processes.
|
@@ -35,7 +35,12 @@ class Knj::Unix_proc
|
|
35
35
|
|
36
36
|
MUTEX.synchronize do
|
37
37
|
ret = [] unless block_given?
|
38
|
-
|
38
|
+
|
39
|
+
if args["psaux_str"]
|
40
|
+
res = args["psaux_str"]
|
41
|
+
else
|
42
|
+
res = Knj::Os.shellcmd(cmdstr)
|
43
|
+
end
|
39
44
|
|
40
45
|
res.scan(/^(\S+)\s+([0-9]+)\s+([0-9.]+)\s+([0-9.]+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+ (.+)($|\n)/) do |match|
|
41
46
|
pid = match[1]
|
@@ -64,12 +69,16 @@ class Knj::Unix_proc
|
|
64
69
|
next if !found
|
65
70
|
end
|
66
71
|
|
67
|
-
|
68
|
-
|
69
|
-
if block_given?
|
70
|
-
yield(proc_obj)
|
72
|
+
if args["yield_data"]
|
73
|
+
yield(data)
|
71
74
|
else
|
72
|
-
|
75
|
+
proc_obj = Knj::Unix_proc.spawn(data)
|
76
|
+
|
77
|
+
if block_given?
|
78
|
+
yield(proc_obj)
|
79
|
+
else
|
80
|
+
ret << proc_obj
|
81
|
+
end
|
73
82
|
end
|
74
83
|
end
|
75
84
|
|
@@ -1,4 +1,11 @@
|
|
1
1
|
<%
|
2
|
+
#Register appserver.
|
3
|
+
begin
|
4
|
+
appsrv = _hb
|
5
|
+
rescue NameError
|
6
|
+
appsrv = _kas
|
7
|
+
end
|
8
|
+
|
2
9
|
#Support for the PHP-version... Comes handy when converting PHP to Ruby...
|
3
10
|
if !_get["path"]
|
4
11
|
trans = {
|
@@ -171,12 +178,12 @@
|
|
171
178
|
end
|
172
179
|
|
173
180
|
if tmp_use and !force
|
174
|
-
|
175
|
-
|
181
|
+
appsrv.header("Last-Modified", "#{time_orig.httpdate} GMT") if time_orig
|
182
|
+
appsrv.header("Content-Type", "image/png")
|
176
183
|
_httpsession.force_content(File.read(tmp_path))
|
177
184
|
else
|
178
|
-
|
179
|
-
|
185
|
+
appsrv.header("Last-Modified", "#{time_orig.httpdate} GMT") if time_orig
|
186
|
+
appsrv.header("Content-Type", "image/png")
|
180
187
|
_httpsession.force_content(blob_cont)
|
181
188
|
end
|
182
189
|
%>
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: knjrbfw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.87
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Kasper Johansen
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-08-
|
13
|
+
date: 2012-08-19 00:00:00 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -356,7 +356,6 @@ files:
|
|
356
356
|
- lib/knj/thread.rb
|
357
357
|
- lib/knj/thread2.rb
|
358
358
|
- lib/knj/threadhandler.rb
|
359
|
-
- lib/knj/threadpool.rb
|
360
359
|
- lib/knj/translations.rb
|
361
360
|
- lib/knj/unix_proc.rb
|
362
361
|
- lib/knj/web.rb
|
@@ -395,7 +394,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
395
394
|
requirements:
|
396
395
|
- - ">="
|
397
396
|
- !ruby/object:Gem::Version
|
398
|
-
hash:
|
397
|
+
hash: -398379275512532928
|
399
398
|
segments:
|
400
399
|
- 0
|
401
400
|
version: "0"
|
data/lib/knj/threadpool.rb
DELETED
@@ -1,264 +0,0 @@
|
|
1
|
-
#A small threadpool framework.
|
2
|
-
class Knj::Threadpool
|
3
|
-
def self.worker_data
|
4
|
-
raise "This thread is not running via the threadpool." if !Thread.current[:knj_threadpool]
|
5
|
-
return Thread.current[:knj_threadpool]
|
6
|
-
end
|
7
|
-
|
8
|
-
attr_reader :workers, :blocks, :mutex, :args, :events
|
9
|
-
|
10
|
-
#Constructor.
|
11
|
-
#===Examples
|
12
|
-
# tp = Knj::Threadpool.new(:threads => 5)
|
13
|
-
def initialize(args = {})
|
14
|
-
@args = args
|
15
|
-
@args[:sleep] = 0.2 if !@args.key?(:sleep)
|
16
|
-
|
17
|
-
raise "Invalid number of threads: '#{@args[:threads]}'." if !@args[:threads] or @args[:threads].to_i <= 0
|
18
|
-
|
19
|
-
@workers = []
|
20
|
-
@blocks = []
|
21
|
-
@mutex = Mutex.new
|
22
|
-
@events = Knj::Event_handler.new
|
23
|
-
@events.add_event(:name => :on_error)
|
24
|
-
|
25
|
-
self.start
|
26
|
-
end
|
27
|
-
|
28
|
-
#Starts the threadpool. This is automatically called from the constructor.
|
29
|
-
def start
|
30
|
-
@mutex.synchronize do
|
31
|
-
if !@running
|
32
|
-
@workers.length.upto(@args[:threads]) do |count|
|
33
|
-
@workers << Knj::Threadpool::Worker.new(:threadpool => self, :id => count)
|
34
|
-
end
|
35
|
-
|
36
|
-
@running = true
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
#Stops the threadpool.
|
42
|
-
def stop
|
43
|
-
if @running
|
44
|
-
@workers.each do |worker|
|
45
|
-
if !worker.running
|
46
|
-
worker.kill
|
47
|
-
@workers.delete(worker)
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
@running = false
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
#Runs the given block, waits for the result and returns the result.
|
56
|
-
def run(*args, &block)
|
57
|
-
raise "No block given." if !block_given?
|
58
|
-
blockdata = {:block => block, :result => nil, :running => false, :runned => false, :args => args}
|
59
|
-
@blocks << blockdata
|
60
|
-
|
61
|
-
loop do
|
62
|
-
sleep @args[:sleep]
|
63
|
-
|
64
|
-
if blockdata[:runned]
|
65
|
-
begin
|
66
|
-
res = blockdata[:result]
|
67
|
-
raise blockdata[:error] if blockdata.key?(:error)
|
68
|
-
ensure
|
69
|
-
@mutex.synchronize do
|
70
|
-
blockdata.clear
|
71
|
-
@blocks.delete(blockdata)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
|
75
|
-
return res
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
#Runs the given block in the threadpool asynced. Returns a 'Knj::Threadpool::Asynced'-object that can be used to get the result and more.
|
81
|
-
def run_async(*args, &block)
|
82
|
-
raise "No block given." if !block_given?
|
83
|
-
|
84
|
-
@mutex.synchronize do
|
85
|
-
blockdata = {:block => block, :running => false, :runned => false, :args => args}
|
86
|
-
@blocks << blockdata
|
87
|
-
return Knj::Threadpool::Asynced.new(blockdata)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
#Returns a new block to be runned if there is one. Otherwise false.
|
92
|
-
def get_block
|
93
|
-
return false if !@running
|
94
|
-
|
95
|
-
@mutex.synchronize do
|
96
|
-
@blocks.each do |blockdata|
|
97
|
-
if blockdata and !blockdata[:running] and !blockdata[:runned]
|
98
|
-
blockdata[:running] = true
|
99
|
-
return blockdata
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
return false
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
#This is the threadpool worker-object. No need to spawn this manually.
|
109
|
-
class Knj::Threadpool::Worker
|
110
|
-
attr_reader :running
|
111
|
-
|
112
|
-
#Constructor. Should not be called manually.
|
113
|
-
def initialize(args)
|
114
|
-
@args = args
|
115
|
-
@tp = @args[:threadpool]
|
116
|
-
@mutex_tp = @tp.mutex
|
117
|
-
@sleep = @tp.args[:sleep]
|
118
|
-
@running = false
|
119
|
-
self.spawn_thread
|
120
|
-
end
|
121
|
-
|
122
|
-
#Starts the workers thread.
|
123
|
-
def spawn_thread
|
124
|
-
@thread = Knj::Thread.new do
|
125
|
-
loop do
|
126
|
-
break if !@sleep or !@tp
|
127
|
-
|
128
|
-
if !@blockdata
|
129
|
-
sleep @sleep
|
130
|
-
@blockdata = @tp.get_block if !@blockdata
|
131
|
-
end
|
132
|
-
|
133
|
-
next if !@blockdata
|
134
|
-
|
135
|
-
res = nil
|
136
|
-
raise "No block in blockdata?" if !@blockdata[:block]
|
137
|
-
@blockdata[:worker] = self
|
138
|
-
Thread.current[:knj_threadpool] = {
|
139
|
-
:worker => self,
|
140
|
-
:blockdata => @blockdata
|
141
|
-
}
|
142
|
-
|
143
|
-
begin
|
144
|
-
@running = true
|
145
|
-
res = @blockdata[:block].call(*@blockdata[:args])
|
146
|
-
rescue => e
|
147
|
-
@mutex_tp.synchronize do
|
148
|
-
@blockdata[:error] = e
|
149
|
-
end
|
150
|
-
ensure
|
151
|
-
#Reset thread.
|
152
|
-
Thread.current[:knj_threadpool] = nil
|
153
|
-
|
154
|
-
#Set running-status on worker.
|
155
|
-
@running = false
|
156
|
-
|
157
|
-
#Update block-data.
|
158
|
-
@mutex_tp.synchronize do
|
159
|
-
@blockdata[:result] = res if res
|
160
|
-
@blockdata[:runned] = true
|
161
|
-
@blockdata[:running] = false
|
162
|
-
end
|
163
|
-
|
164
|
-
#Try to avoid slowdown of sleep by checking if there is a new block right away.
|
165
|
-
@blockdata = @tp.get_block
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
#Returns true if the worker is currently working with a block.
|
172
|
-
def busy?
|
173
|
-
return true if @blockdata
|
174
|
-
end
|
175
|
-
|
176
|
-
#Returns the ID of the worker.
|
177
|
-
def id
|
178
|
-
return @args[:id]
|
179
|
-
end
|
180
|
-
|
181
|
-
#Kills the current thread and restarts the worker.
|
182
|
-
def restart
|
183
|
-
@mutex_tp.synchronize do
|
184
|
-
@thread.kill
|
185
|
-
|
186
|
-
if @blockdata
|
187
|
-
@blockdata[:runned] = true
|
188
|
-
@blockdata[:running] = false
|
189
|
-
|
190
|
-
begin
|
191
|
-
sleep 0.1
|
192
|
-
raise "The worker was stopped during execution of the block."
|
193
|
-
rescue => e
|
194
|
-
@blockdata[:error] = e
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
#Dont run the job again - remove it from the queue.
|
199
|
-
@tp.blocks.delete(@blockdata)
|
200
|
-
@blockdata = nil
|
201
|
-
@running = false
|
202
|
-
|
203
|
-
#Spawn a new thread - we killed the previous.
|
204
|
-
self.spawn_thread
|
205
|
-
end
|
206
|
-
end
|
207
|
-
|
208
|
-
#Kills the thread.
|
209
|
-
def kill
|
210
|
-
@mutex_tp.synchronize do
|
211
|
-
@thread.kill
|
212
|
-
end
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
#An object of this class will be returned when calling 'run_async'.
|
217
|
-
class Knj::Threadpool::Asynced
|
218
|
-
#Constructor. Should not be called manually.
|
219
|
-
def initialize(args)
|
220
|
-
@args = args
|
221
|
-
end
|
222
|
-
|
223
|
-
#Returns true if the asynced job is still running.
|
224
|
-
def running?
|
225
|
-
return true if @args[:running]
|
226
|
-
return false
|
227
|
-
end
|
228
|
-
|
229
|
-
#Returns true if the asynced job is done running.
|
230
|
-
def done?
|
231
|
-
return true if @args[:runned] or @args.empty? or @args[:error]
|
232
|
-
return false
|
233
|
-
end
|
234
|
-
|
235
|
-
#Returns true if the asynced job is still waiting to run.
|
236
|
-
def waiting?
|
237
|
-
return true if !@args.empty? and !@args[:running] and !@args[:runned]
|
238
|
-
return false
|
239
|
-
end
|
240
|
-
|
241
|
-
#Raises error if one has happened in the asynced job.
|
242
|
-
def error!
|
243
|
-
raise @args[:error] if @args.key?(:error)
|
244
|
-
end
|
245
|
-
|
246
|
-
#Sleeps until the asynced job is done. If an error occurred in the job, that error will be raised when calling the method.
|
247
|
-
def join
|
248
|
-
loop do
|
249
|
-
self.error!
|
250
|
-
break if self.done?
|
251
|
-
sleep 0.1
|
252
|
-
end
|
253
|
-
|
254
|
-
self.error!
|
255
|
-
end
|
256
|
-
|
257
|
-
#Returns the result of the job. If an error occurred in the job, that error will be raised when calling the method.
|
258
|
-
def result(args = nil)
|
259
|
-
self.join if args and args[:wait]
|
260
|
-
raise "Not done yet." unless self.done?
|
261
|
-
self.error!
|
262
|
-
return @args[:result]
|
263
|
-
end
|
264
|
-
end
|