Class: DataPipe2::Job

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

Overview

Hold a single job definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Job

Returns a new instance of Job.



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

def initialize(path)
  @path = path
  @name = File.basename(path, '.dsl')
  @cron_string = ''

  @error_list = []
  set_cron
end

Instance Attribute Details

#errorListObject (readonly)

Returns the value of attribute errorList.



7
8
9
# File 'lib/jobs.rb', line 7

def errorList
  @errorList
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/jobs.rb', line 7

def name
  @name
end

#nextObject (readonly)

Returns the value of attribute next.



7
8
9
# File 'lib/jobs.rb', line 7

def next
  @next
end

Instance Method Details

#add_error(e) ⇒ Object

Job Error -> Time, Exception Class Name, nsg, backtrace



19
20
21
# File 'lib/jobs.rb', line 19

def add_error(e)
  @error_list << "#{e.class.name}: #{e.message}\n#{e.backtrace.join("\n")}"
end

#callObject



39
40
41
# File 'lib/jobs.rb', line 39

def call
  run if Time.now > @next
end

#clear_errorObject



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

def clear_error
  @error_list = []
end

#runObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/jobs.rb', line 43

def run
  begin
    DataPipe2.log "path: #{@path}", true
    DataPipe2.log "dsl: #{@name}"
    load @path
    clear_error
  rescue SystemExit, Interrupt
    raise
  rescue StandardError => e
    string = e.message
    p e.backtrace
    DataPipe2.log_dsl @name, string
    add_error(e)
  end

  set_cron
  @next = @cron.next(Time.now)
end

#run_nowObject



27
28
29
# File 'lib/jobs.rb', line 27

def run_now
  @next = Time.now - 1
end

#set_cronObject



31
32
33
34
35
36
37
# File 'lib/jobs.rb', line 31

def set_cron
  tmp = ENV["#{@name}_CRON"] ||= '0 0 * * *'
  return if tmp == @cron_string

  @cron_string = tmp
  @cron = CronParser.new(@cron_string)
end