Class: Async::Cron::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/async/cron/scheduler.rb

Defined Under Namespace

Classes: Loader

Constant Summary collapse

PATH =
"config/async/cron/scheduler.rb"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



61
62
63
# File 'lib/async/cron/scheduler.rb', line 61

def initialize
	@schedules = Hash.new.compare_by_identity
end

Instance Attribute Details

#schedulesObject (readonly)

Returns the value of attribute schedules.



65
66
67
# File 'lib/async/cron/scheduler.rb', line 65

def schedules
  @schedules
end

Class Method Details

.load(root = Dir.pwd) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/async/cron/scheduler.rb', line 44

def self.load(root = Dir.pwd)
	path = ::File.join(root, PATH)
	
	if ::File.exist?(path)
		return self.load_file(path)
	end
end

.load_file(path) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/async/cron/scheduler.rb', line 52

def self.load_file(path)
	scheduler = self.new
	loader = Loader.new(scheduler)
	
	loader.instance_eval(::File.read(path), path)
	
	return scheduler
end

Instance Method Details

#add(schedule, &block) ⇒ Object



67
68
69
# File 'lib/async/cron/scheduler.rb', line 67

def add(schedule, &block)
	@schedules[schedule] = block
end

#runObject



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/async/cron/scheduler.rb', line 71

def run
	Sync do
		barrier = Async::Barrier.new
		
		@schedules.each do |schedule, block|
			barrier.async do
				schedule.run(&block)
			end
		end
		
		barrier.wait
	end
end