Class: Lobster::Service

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Service

Returns a new instance of Service.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lobster/service.rb', line 9

def initialize(config)
  @job_list = nil
  @running = true
  @sleeping = false
  @main_thread = Thread.current
  @config = config
  @poll_delay = 60

  rout, @wout = IO.pipe
  rerr, @werr = IO.pipe

  Thread.new do
    while (line = rout.gets)
      Lobster.logger.info line.chomp
    end
  end

  Thread.new do
    while (line = rerr.gets)
      Lobster.logger.error line.chomp
    end
  end

  trap 'HUP' do
    begin
      @config = Configuration.new(@config[:lobster_dir], @config[:environment])
    rescue Exception => e
      Lobster.logger.error "Cannot reload conf, Exception: #{e}"
      break
    end
    Lobster.logger.info "Lobster config reloaded: #{@config}"
  end

  at_exit do
    @running = false
    sleep 0.01 until @sleeping # make sure no new jobs are created
   
    Lobster.logger.info  "Exiting, all jobs are getting killed."
    @job_list.jobs.each_value do |job|
      job.kill 'INT'
    end if @job_list

    @main_thread.wakeup # stop sleeping and exit properly
  end
end

Class Method Details

.start(config) ⇒ Object



5
6
7
# File 'lib/lobster/service.rb', line 5

def self.start(config)
  new(config).run
end

Instance Method Details

#reload_scheduleObject



75
76
77
78
79
80
81
82
# File 'lib/lobster/service.rb', line 75

def reload_schedule
  @job_list ||= JobList.new(@config[:schedule_file])
  begin
    @job_list.reload
  rescue Exception => e
    Lobster.logger.error "#{e}: error while reading config file in #{@config[:schedule_file]}, not updating"
  end
end

#runObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/lobster/service.rb', line 55

def run
  Lobster.logger.info "Lobster started version #{Lobster::VERSION}"
  Lobster.logger.info "Lobster config: #{@config}"
  
  while @running
    @sleeping = false
    now = Time.now
    
    reload_schedule

    @job_list.jobs.each_value do |job|
      if not job.running? and now >= job.next_run
        job.run(@wout, @werr, @config[:lobster_dir])
      end
    end
    @sleeping = true
    sleep @poll_delay
  end
end