Class: Kiba::Plus::Job

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/kiba/plus/job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#format_sql, #mysql2_connect_hash, #scheme

Constructor Details

#initialize(options) ⇒ Job

Returns a new instance of Job.



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/kiba/plus/job.rb', line 11

def initialize(options)
  @options = options
  @options.assert_valid_keys(:connect_url, :job_id, :job_name, :start_at, :completed_at, :schema, :job_table_name)
  url = URI.parse(connect_url)
  if url.scheme =~ /mysql/i
    @client = Mysql2::Client.new(mysql2_connect_hash(connect_url))
  elsif url.scheme =~ /postgres/i
    @client = PG.connect(connect_url)
    @client.exec "SET search_path TO %s" % [ options[:schema] ] if options[:schema]
  else
    raise 'No Imp!'
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/kiba/plus/job.rb', line 10

def client
  @client
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/kiba/plus/job.rb', line 10

def options
  @options
end

Instance Method Details

#completeObject



61
62
63
# File 'lib/kiba/plus/job.rb', line 61

def complete
  complete_job
end

#completed_atObject



45
46
47
# File 'lib/kiba/plus/job.rb', line 45

def completed_at
  options.fetch(:completed_at, Time.now)
end

#connect_urlObject



29
30
31
# File 'lib/kiba/plus/job.rb', line 29

def connect_url
  options.fetch(:connect_url)
end

#job_idObject



25
26
27
# File 'lib/kiba/plus/job.rb', line 25

def job_id
  options.fetch(:job_id, nil)
end

#job_nameObject



33
34
35
# File 'lib/kiba/plus/job.rb', line 33

def job_name
  options.fetch(:job_name)
end

#job_table_nameObject



37
38
39
# File 'lib/kiba/plus/job.rb', line 37

def job_table_name
  options.fetch(:job_table_name, "etl_jobs")
end

#last_pull_atObject



55
56
57
58
59
# File 'lib/kiba/plus/job.rb', line 55

def last_pull_at
  sql = "SELECT MAX(created_at) AS last_pull_at FROM #{job_table_name} WHERE status = 'completed' AND job_name = '#{job_name}'"
  Kiba::Plus.logger.info sql
  client.query(sql).first["last_pull_at"]
end

#startObject



49
50
51
52
53
# File 'lib/kiba/plus/job.rb', line 49

def start
  create_table
  result = create_job
  result.first["id"].to_i
end

#start_atObject



41
42
43
# File 'lib/kiba/plus/job.rb', line 41

def start_at
  options.fetch(:start_at, Time.now)
end