Class: TimeScheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/time_scheduler/event.rb,
lib/time_scheduler/version.rb,
lib/time_scheduler/order_set.rb,
lib/time_scheduler/scheduler.rb

Defined Under Namespace

Classes: Counter, Error, EventItem, EventQueue, OrderSet, Schedule, Scheduler

Constant Summary collapse

VERSION =
"1.2.0"

Instance Method Summary collapse

Constructor Details

#initializeTimeScheduler

Returns a new instance of TimeScheduler.



208
209
210
211
# File 'lib/time_scheduler/scheduler.rb', line 208

def initialize
  @first_counter  =  Hash.new{|h,k| h[k] = Counter.new }
  @last_counter   =  Hash.new{|h,k| h[k] = Counter.new }
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/time_scheduler/scheduler.rb', line 241

def active?
  TimeScheduler::Schedule.active?
end

#cancel(*topics) ⇒ Object



235
236
237
238
239
# File 'lib/time_scheduler/scheduler.rb', line 235

def cancel( *topics )
  topics.each do |topic|
    scheduler.cancel( topic )
  end
end

#first_only(ident = nil, timeout: 1, &block) ⇒ Object



253
254
255
256
257
258
259
260
261
# File 'lib/time_scheduler/scheduler.rb', line 253

def first_only( ident = nil, timeout: 1, &block )
  key  =  [ caller[0], ident.to_s ].join(":")
  count  =  @first_counter[key].incr
  block.call    if count == 1
  ::Thread.start( key ) do |key|
    ::Kernel.sleep  timeout
    @first_counter[key].decr
  end
end

#last_only(ident = nil, timeout: 1, &block) ⇒ Object



263
264
265
266
267
268
269
270
271
# File 'lib/time_scheduler/scheduler.rb', line 263

def last_only( ident = nil, timeout: 1, &block )
  key  =  [ caller[0], ident.to_s ].join(":")
  @last_counter[key].incr
  ::Thread.start( key ) do |key|
    ::Kernel.sleep  timeout
    count  =  @last_counter[key].decr
    block.call    if count == 0
  end
end

#resumeObject



249
250
251
# File 'lib/time_scheduler/scheduler.rb', line 249

def resume
  TimeScheduler::Schedule.resume
end

#schedulerObject



213
214
215
# File 'lib/time_scheduler/scheduler.rb', line 213

def scheduler
  @@Scheduler  ||=  TimeScheduler::Scheduler.new
end

#suspendObject



245
246
247
# File 'lib/time_scheduler/scheduler.rb', line 245

def suspend
  TimeScheduler::Schedule.suspend
end

#topicsObject



231
232
233
# File 'lib/time_scheduler/scheduler.rb', line 231

def topics
  scheduler.topics
end

#wait(topic = Time.now.iso8601(6), **option, &block) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/time_scheduler/scheduler.rb', line 217

def wait( topic = Time.now.iso8601(6), **option, &block )
  raise  TimeScheduler::Error, "option missing."    if  option.empty?

  if scheduler.reserved?( topic )
    scheduler.wait_reset( topic, **option, &block )
  else
    if option[:action].nil?  &&  block.nil?
      scheduler.wait_once( topic, **option )
    else
      scheduler.wait_each( topic, **option, &block )
    end
  end
end