Class: Coupler::Scheduler

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/coupler/scheduler.rb

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



5
6
7
8
# File 'lib/coupler/scheduler.rb', line 5

def initialize
  super
  @mutex = Mutex.new
end

Instance Method Details

#is_started?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/coupler/scheduler.rb', line 60

def is_started?
  !@loop.nil?
end

#run_jobsObject



34
35
36
37
38
39
40
41
42
# File 'lib/coupler/scheduler.rb', line 34

def run_jobs
  @mutex.synchronize do
    count = Models::Job.filter(:status => 'running').count
    if count == 0
      job = Models::Job.filter(:status => 'scheduled').order(:created_at).first
      Thread.new(job) { |j| j.execute } if job
    end
  end
end

#schedule_import_job(import) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/coupler/scheduler.rb', line 26

def schedule_import_job(import)
  Models::Job.create({
    :name => "import",
    :import => import,
    :status => "scheduled"
  })
end

#schedule_run_scenario_job(scenario) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/coupler/scheduler.rb', line 18

def schedule_run_scenario_job(scenario)
  Models::Job.create({
    :name => "run_scenario",
    :scenario => scenario,
    :status => "scheduled"
  })
end

#schedule_transform_job(resource) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/coupler/scheduler.rb', line 10

def schedule_transform_job(resource)
  Models::Job.create({
    :name => "transform",
    :resource => resource,
    :status => "scheduled"
  })
end

#shutdownObject



55
56
57
58
# File 'lib/coupler/scheduler.rb', line 55

def shutdown
  @loop.exit
  @loop = nil
end

#startObject



44
45
46
47
48
49
50
51
52
53
# File 'lib/coupler/scheduler.rb', line 44

def start
  if !is_started?
    @loop = Thread.new do
      loop do
        sleep 10
        run_jobs
      end
    end
  end
end