Class: RubySDK::GRPCServer

Inherits:
Proto::Plugin::Service show all
Defined in:
lib/rubysdk.rb

Overview

GRPCServer provides an implementation of the Plugin service.

Instance Method Summary collapse

Constructor Details

#initialize(cached_jobs) ⇒ GRPCServer

Returns a new instance of GRPCServer.



17
18
19
# File 'lib/rubysdk.rb', line 17

def initialize(cached_jobs)
  @cached_jobs = cached_jobs
end

Instance Method Details

#execute_job(job, _call) ⇒ Object

execute_job executes the given job and returns a result.



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rubysdk.rb', line 29

def execute_job(job, _call)
  cjob = nil
  @cached_jobs.each do |cached_job|
    cjob = cached_job if cached_job.job.unique_id == job.unique_id
  end
  if cjob == nil
    Proto::JobResult.new(failed: true,
                  exit_pipeline: true,
                  message: "job not found in plugin " + job.title)
    return
  end

  # Transform arguments
  args = []
  if !job.args.empty?
    job.args.each do |arg|
      new_arg = Proto::Argument.new(key: arg.key,
                           value: arg.value)
      args.push new_arg
    end
  end

  # Execute job
  job_failed = false
  exit_pipeline = false
  message = ""
  unique_id = 0
  begin
    cjob.handler.call(args)
  rescue => e
    # Check if job wants to force exit pipeline.
    # We will exit the pipeline but not mark it as 'failed'.
    job_failed = true if e == ErrorExitPipeline

    # Set log message and job id
    exit_pipeline = true
    message = e.message
    unique_id = job.job.unique_id
  end
  Proto::JobResult.new(unique_id: unique_id,
                       failed: job_failed,
                       exit_pipeline: exit_pipeline,
                       message: message)
end

#get_jobs(empty, _call) ⇒ Object

get_jobs returns all registered jobs.



22
23
24
25
26
# File 'lib/rubysdk.rb', line 22

def get_jobs(empty, _call)
  jobs = []
  @cached_jobs.each { |job| jobs.push job.job }
  jobs.each
end