Class: AlWorker::Timer
- Inherits:
-
Object
- Object
- AlWorker::Timer
- Defined in:
- lib/al_worker_timer.rb
Overview
タイマー
Instance Attribute Summary collapse
-
#interval ⇒ Numeric
タイマー間隔 (sec).
-
#is_singleshot ⇒ Boolean
readonly
シングルショットタイマーか?.
-
#is_start ⇒ Boolean
readonly
動作中か?.
-
#mode_sync ⇒ Symbol
タイムアップ時の動作 同期(:sync)/非同期(:async).
-
#timeup ⇒ Numeric, Time
タイマー開始 (sec).
Class Method Summary collapse
-
.periodic(interval = nil, timeup = nil) ⇒ AlWorker::Timer
繰り返しタイマーの生成.
-
.singleshot(timeup = nil) ⇒ AlWorker::Timer
シングルショットタイマーの生成.
Instance Method Summary collapse
-
#initialize(a1, a2, a3) ⇒ Timer
constructor
constructor ユーザプログラムからは直接使わない。.
-
#run(*arg) { ... } ⇒ Boolean
タイマー開始.
-
#stop ⇒ Object
タイマー停止.
Constructor Details
#initialize(a1, a2, a3) ⇒ Timer
Note:
constructor ユーザプログラムからは直接使わない。
38 39 40 41 42 43 44 45 |
# File 'lib/al_worker_timer.rb', line 38 def initialize( a1, a2, a3 ) @timeup = a1 @interval = a2 @mode_sync = a3 @is_singleshot = !@interval @is_start = false end |
Instance Attribute Details
#interval ⇒ Numeric
Returns タイマー間隔 (sec).
24 25 26 |
# File 'lib/al_worker_timer.rb', line 24 def interval @interval end |
#is_singleshot ⇒ Boolean (readonly)
Returns シングルショットタイマーか?.
18 19 20 |
# File 'lib/al_worker_timer.rb', line 18 def is_singleshot @is_singleshot end |
#is_start ⇒ Boolean (readonly)
Returns 動作中か?.
30 31 32 |
# File 'lib/al_worker_timer.rb', line 30 def is_start @is_start end |
#mode_sync ⇒ Symbol
Returns タイムアップ時の動作 同期(:sync)/非同期(:async).
27 28 29 |
# File 'lib/al_worker_timer.rb', line 27 def mode_sync @mode_sync end |
#timeup ⇒ Numeric, Time
Returns タイマー開始 (sec).
21 22 23 |
# File 'lib/al_worker_timer.rb', line 21 def timeup @timeup end |
Class Method Details
.periodic(interval = nil, timeup = nil) ⇒ AlWorker::Timer
繰り返しタイマーの生成
69 70 71 |
# File 'lib/al_worker_timer.rb', line 69 def self.periodic( interval = nil, timeup = nil ) return self.new( timeup, interval, :sync ) end |
.singleshot(timeup) ⇒ AlWorker::Timer .singleshot(timeup) ⇒ AlWorker::Timer
シングルショットタイマーの生成
57 58 59 |
# File 'lib/al_worker_timer.rb', line 57 def self.singleshot( timeup = nil ) return self.new( timeup, nil, :sync ) end |
Instance Method Details
#run(*arg) { ... } ⇒ Boolean
タイマー開始
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/al_worker_timer.rb', line 81 def run( *arg ) return false if @is_start @is_start = true @thread = Thread.start( arg ) {|arg| @timeup ||= @interval # sleep until first exec. case @timeup when Numeric sleep @timeup if @timeup > 0 when Time dt = @timeup.to_f - Time.now.to_f sleep dt if dt > 0 else raise self.to_s + ": sleep time must be Numeric or Time." end int_timeup = Time.now.to_f while true # fire! begin Thread.exit if Thread.current[:flag_stop] || ! block_given? if mode_sync == :sync AlWorker.mutex_sync.synchronize { yield( *arg ) } else yield( *arg ) end rescue Exception => ex raise ex if ex.class == SystemExit AlWorker.log( ex ) end if @is_singleshot @is_start = false break # break a while and thread. end # sleep next interval int_timeup += @interval dt = int_timeup - Time.now.to_f if dt > 0 sleep dt else # over the interval, restart now time. int_timeup = Time.now.to_f Thread.pass end end } return true end |
#stop ⇒ Object
タイマー停止
141 142 143 144 |
# File 'lib/al_worker_timer.rb', line 141 def stop() @thread[:flag_stop] = true if @thread @is_start = false end |