Class: PolyLingo::Resources::Jobs

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

Constant Summary collapse

DEFAULT_POLL_INTERVAL =
5
DEFAULT_TIMEOUT =
1200

Instance Method Summary collapse

Constructor Details

#initialize(http) ⇒ Jobs

Returns a new instance of Jobs.



11
12
13
# File 'lib/polylingo/resources/jobs.rb', line 11

def initialize(http)
  @http = http
end

Instance Method Details

#create(content:, targets:, format: nil, source: nil, model: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/polylingo/resources/jobs.rb', line 15

def create(content:, targets:, format: nil, source: nil, model: nil)
  body = {
    "content" => content,
    "targets" => targets
  }
  body["format"] = format unless format.nil?
  body["source"] = source unless source.nil?
  body["model"] = model unless model.nil?

  @http.request("/jobs", method: :post, body: body, expect_status: 202)
end

#get(job_id) ⇒ Object



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

def get(job_id)
  enc = ERB::Util.url_encode(job_id.to_s)
  @http.request("/jobs/#{enc}", method: :get, expect_status: 200)
end

#translate(content:, targets:, format: nil, source: nil, model: nil, poll_interval: DEFAULT_POLL_INTERVAL, timeout: DEFAULT_TIMEOUT, on_progress: nil) ⇒ Object

Submit a translation job and poll until it completes or fails. poll_interval and timeout are in seconds (Ruby convention).



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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/polylingo/resources/jobs.rb', line 34

def translate(content:, targets:, format: nil, source: nil, model: nil,
              poll_interval: DEFAULT_POLL_INTERVAL, timeout: DEFAULT_TIMEOUT,
              on_progress: nil)
  job = create(
    content: content,
    targets: targets,
    format: format,
    source: source,
    model: model
  )
  job_id = job["job_id"]
  if job_id.nil? || job_id.to_s.empty?
    raise JobFailedError.new("unknown", 500, "invalid_response", "Job create response missing job_id")
  end

  deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout.to_f

  loop do
    status = get(job_id)
    st = status["status"]
    if (st == "pending" || st == "processing") && on_progress
      on_progress.call(status["queue_position"])
    end

    case st
    when "completed"
      unless status["translations"] && status["usage"]
        raise JobFailedError.new(
          job_id,
          500,
          "invalid_response",
          "Job completed but translations or usage was missing"
        )
      end
      return {
        "translations" => status["translations"],
        "usage" => status["usage"]
      }
    when "failed"
      err = status["error"].is_a?(String) ? status["error"] : "job_failed"
      msg = if status["message"].is_a?(String)
              status["message"]
            elsif status["error"].is_a?(String)
              status["error"]
            else
              "Translation job failed"
            end
      raise JobFailedError.new(job_id, 200, err, msg)
    end

    if Process.clock_gettime(Process::CLOCK_MONOTONIC) >= deadline
      raise JobFailedError.new(
        job_id,
        408,
        "timeout",
        "Job polling exceeded the configured timeout"
      )
    end

    sleep(poll_interval)
  end
end