Class: Knjappserver::Threadding_timeout
- Inherits:
-
Object
- Object
- Knjappserver::Threadding_timeout
- Defined in:
- lib/include/class_knjappserver_threadding_timeout.rb
Instance Attribute Summary collapse
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns various data.
-
#alive? ⇒ Boolean
Returns true if the thread is alive or not.
-
#initialize(args) ⇒ Threadding_timeout
constructor
A new instance of Threadding_timeout.
-
#running? ⇒ Boolean
Returns true if the timeout is running or not.
-
#start ⇒ Object
Starts the timeout.
-
#stop ⇒ Object
Stops the timeout.
- #time ⇒ Object
- #time=(newtime) ⇒ Object
Constructor Details
#initialize(args) ⇒ Threadding_timeout
Returns a new instance of Threadding_timeout.
4 5 6 7 8 9 10 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 4 def initialize(args) @args = args @kas = @args[:kas] raise "No time given." if !@args.key?(:time) @mutex = Mutex.new @running = false end |
Instance Attribute Details
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
2 3 4 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 2 def timeout @timeout end |
Instance Method Details
#[](key) ⇒ Object
Returns various data.
86 87 88 89 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 86 def [](key) return @timeout if key == :knjappserver_timeout raise "No such key: '#{key}'." end |
#alive? ⇒ Boolean
Returns true if the thread is alive or not.
92 93 94 95 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 92 def alive? return @thread.alive? if @thread return false end |
#running? ⇒ Boolean
Returns true if the timeout is running or not.
98 99 100 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 98 def running? return @running end |
#start ⇒ Object
Starts the timeout.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 21 def start @run = true @thread = Thread.new do loop do begin if @args[:counting] @timeout = @args[:time] while @timeout > 0 @timeout += -1 break if @kas.should_restart or !@run sleep 1 end else sleep @args[:time] end break if @kas.should_restart or !@run @mutex.synchronize do @kas.threadpool.run do @kas.ob.db.get_and_register_thread if @kas.ob.db.opts[:threadsafe] @kas.db_handler.get_and_register_thread if @kas.db_handler.opts[:threadsafe] Thread.current[:knjappserver] = { :kas => @kas, :db => @kas.db_handler } begin @running = true if @args.key?(:timeout) Timeout.timeout(@args[:timeout]) do @args[:block].call(*@args[:args]) end else @args[:block].call(*@args[:args]) end ensure @running = false @kas.ob.db.free_thread if @kas.ob.db.opts[:threadsafe] @kas.db_handler.free_thread if @kas.db_handler.opts[:threadsafe] end end end rescue => e @kas.handle_error(e) end end end return self end |
#stop ⇒ Object
Stops the timeout.
77 78 79 80 81 82 83 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 77 def stop @run = false @mutex.synchronize do @thread.kill if @thread.alive? @thread = nil end end |
#time ⇒ Object
16 17 18 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 16 def time return @args[:time] end |
#time=(newtime) ⇒ Object
12 13 14 |
# File 'lib/include/class_knjappserver_threadding_timeout.rb', line 12 def time=(newtime) @args[:time] = newtime.to_s.to_i end |