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

- (Timer) initialize(a1, a2, a3)

Note:

constructor ユーザプログラムからは直接使わない。



34
35
36
37
38
39
# File 'lib/al_worker_timer.rb', line 34

def initialize( a1, a2, a3 )
  @is_singleshot = a1
  @interval = a2
  @mode_sync = a3
  @is_start = false
end

Instance Attribute Details

- (Numeric, Time) interval

Returns タイマー間隔 (sec)

Returns:

  • (Numeric, Time)

    タイマー間隔 (sec)



20
21
22
# File 'lib/al_worker_timer.rb', line 20

def interval
  @interval
end

- (Boolean) is_singleshot (readonly)

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

Returns:

  • (Boolean)

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



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

def is_singleshot
  @is_singleshot
end

- (Boolean) is_start (readonly)

Returns 動作中か?

Returns:

  • (Boolean)

    動作中か?



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

def is_start
  @is_start
end

- (Symbol) mode_sync

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

Returns:

  • (Symbol)

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



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

def mode_sync
  @mode_sync
end

Class Method Details

+ (AlWorker::Timer) periodic(interval = nil)

繰り返しタイマーの生成

Parameters:

  • interval (Numeric) (defaults to: nil)

    タイマー間隔

Returns:



62
63
64
# File 'lib/al_worker_timer.rb', line 62

def self.periodic( interval = nil )
  return self.new( false, interval, :sync )
end

+ (AlWorker::Timer) singleshot(interval) + (AlWorker::Timer) singleshot(timeup)

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

Overloads:

  • + (AlWorker::Timer) singleshot(interval)

    Parameters:

    • interval (Numeric)

      タイマー間隔

  • + (AlWorker::Timer) singleshot(timeup)

    Parameters:

    • timeup (Time)

      タイムアップ時間

Returns:



51
52
53
# File 'lib/al_worker_timer.rb', line 51

def self.singleshot( interval = nil )
  return self.new( true, interval, :sync )
end

Instance Method Details

- (Boolean) run(*arg) { ... }

タイマー開始

Parameters:

  • arg (Array)

    ブロックに渡す引数

Yields:

  • タイムアップ時の動作

Returns:

  • (Boolean)

    開始できたか?



74
75
76
77
78
79
80
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
136
137
138
139
140
141
142
143
# File 'lib/al_worker_timer.rb', line 74

def run( *arg )
  return false  if @is_start
  @is_start = true

  #
  # single shot
  #
  if @is_singleshot
    @thread = Thread.start( arg ) { |arg|
      # sleeping...
      if @interval.is_a?( Numeric )
        sleep @interval  if @interval > 0
      elsif @interval.is_a?( Time )
        dt = @interval.to_f - Time.now.to_f
        sleep dt  if dt > 0
      else
        raise self.to_s + ": sleep time must be Numeric or Time."
      end

      # 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
      @is_start = false
    }
    return true
  end

  #
  # periodic
  #
  @thread = Thread.start( arg ) { |arg|
    timeup = Time.now.to_f
    loop do
      # sleeping...
      timeup += @interval
      dt = timeup - Time.now.to_f
      if dt > 0
        sleep dt
      else
        timeup = Time.now.to_f
        Thread.pass
      end

      # 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
    end
  }
  return true
end

- (Object) stop

タイマー停止



149
150
151
152
# File 'lib/al_worker_timer.rb', line 149

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