Module: Kiba::Extend::Job

Defined in:
lib/kiba/extend/job.rb

Overview

Convenience methods callable on a given job

Class Method Summary collapse

Class Method Details

.output?(jobkey, mode: :warn_if_unregistered) ⇒ true, false

Parameters:

  • jobkey (Symbol) —

    registry entry for job with namespace

  • mode (:warn, :agnostic) (defaults to: :warn_if_unregistered) —

    use :agnostic in dynamic code where not all jobs tried are expected to actually exist (since 7.0.0)

Returns:

  • (true) —

    if output file already exists when run, or when running job results in 1 or more rows being written

  • (false) —

    if jobkey is not defined, or if job results in 0 rows when run

Since:

  • 4.0.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kiba/extend/job.rb', line 43

def output?(jobkey, mode: :warn_if_unregistered)
  return false if Kiba::Extend::ProjectConfig.blank_jobs.include?(jobkey)

  begin
    reg = Kiba::Extend::Registry.entry_for(jobkey)
  rescue Kiba::Extend::JobNotRegisteredError => err
    if mode == :warn_if_unregistered
      puts Kiba::Extend.warn(err.message)
    end
    return false
  end
  return true if File.exist?(reg.path)

  res = Kiba::Extend::Command::Run.job(jobkey)
  unless res
    Kiba::Extend::ProjectConfig.blank_jobs << jobkey
    return false
  end

  return true if res.outrows > 0

  Kiba::Extend::ProjectConfig.blank_jobs << jobkey
  false
end

.output_fields(jobkey, suppress_warnings: false) ⇒ Array<Symbol>

Note:

Only works for CSV and JsonArray destinations. For JsonArray, only returns the top-level fields of the objects/rows in the output.

Returns headers/fields for given job output.

Parameters:

  • jobkey (Symbol) —

    registry entry for job with namespace

Returns:

  • (Array<Symbol>) —

    headers/fields for given job output

Since:

  • 5.1.0



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kiba/extend/job.rb', line 14

def output_fields(jobkey, suppress_warnings: false)
  unless output?(jobkey)
    msg = "Kiba::Extend::Job.output_fields(#{jobkey}) is returning an "\
      "empty Array instead of nil because #{jobkey} has no output. "\
      "This is a recent change in this method's behavior, so make "\
      "sure your code is still behaving as expected"
    warn(Kiba::Extend.warn(msg)) unless suppress_warnings
    return []
  end

  entry = Kiba::Extend::Registry.entry_for(jobkey)
  path = Pathname.new(entry.path)
  dest = entry.dest_class.new(filename: path)
  unless dest.respond_to?(:fields)
    raise "No output field extraction logic exists for "\
      "#{entry.dest_class}"
  end

  dest.fields
end

.registered?(jobkey) ⇒ Boolean

Note:

Use this if you only care about whether a job has been registered, not whether it has output

Returns without drama about registry errors.

Parameters:

  • jobkey (Symbol) —

    registry entry for job with namespace

Returns:

  • (Boolean) —

    without drama about registry errors.

Since:

  • 7.0.0



73
74
75
# File 'lib/kiba/extend/job.rb', line 73

def registered?(jobkey) = Kiba::Extend.registry.entry_objs
.map { |eo| eo.key.to_sym }
.include?(jobkey)