Class: AlWorker::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/al_worker_timer.rb

Overview

タイマー

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#intervalNumeric

Returns タイマー間隔 (sec).

Returns:

  • (Numeric)

    タイマー間隔 (sec)



24
25
26
# File 'lib/al_worker_timer.rb', line 24

def interval
  @interval
end

#is_singleshotBoolean (readonly)

Returns シングルショットタイマーか?.

Returns:

  • (Boolean)

    シングルショットタイマーか?



18
19
20
# File 'lib/al_worker_timer.rb', line 18

def is_singleshot
  @is_singleshot
end

#is_startBoolean (readonly)

Returns 動作中か?.

Returns:

  • (Boolean)

    動作中か?



30
31
32
# File 'lib/al_worker_timer.rb', line 30

def is_start
  @is_start
end

#mode_syncSymbol

Returns タイムアップ時の動作 同期(:sync)/非同期(:async).

Returns:

  • (Symbol)

    タイムアップ時の動作 同期(:sync)/非同期(:async)



27
28
29
# File 'lib/al_worker_timer.rb', line 27

def mode_sync
  @mode_sync
end

#timeupNumeric, Time

Returns タイマー開始 (sec).

Returns:

  • (Numeric, Time)

    タイマー開始 (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

繰り返しタイマーの生成

Parameters:

  • interval (Numeric) (defaults to: nil)

    タイマー間隔(秒)

  • timeup (Numeric, Time) (defaults to: nil)

    初期起動までの時間(秒)

Returns:



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

シングルショットタイマーの生成

Overloads:

  • .singleshot(timeup) ⇒ AlWorker::Timer

    Parameters:

    • timeup (Numeric)

      タイムアップ時間(秒)

  • .singleshot(timeup) ⇒ AlWorker::Timer

    Parameters:

    • timeup (Time)

      タイムアップ時間(時刻)

Returns:



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

タイマー開始

Parameters:

  • arg (Array)

    ブロックに渡す引数

Yields:

  • タイムアップ時の動作

Returns:

  • (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

#stopObject

タイマー停止



141
142
143
144
# File 'lib/al_worker_timer.rb', line 141

def stop()
  @thread[:flag_stop] = true  if @thread
  @is_start = false
end