Class: Dapp::Kube::Kubernetes::Manager::Job

Inherits:
Base
  • Object
show all
Defined in:
lib/dapp/kube/kubernetes/manager/job.rb

Instance Attribute Summary

Attributes inherited from Base

#dapp, #name

Instance Method Summary collapse

Constructor Details

#initialize(dapp, name) ⇒ Job

Returns a new instance of Job.



5
6
7
8
9
# File 'lib/dapp/kube/kubernetes/manager/job.rb', line 5

def initialize(dapp, name)
  super(dapp, name)

  @processed_pods_names = []
end

Instance Method Details

#wait_till_exists!Object



11
12
13
14
15
16
# File 'lib/dapp/kube/kubernetes/manager/job.rb', line 11

def wait_till_exists!
  loop do
    break if dapp.kubernetes.job?(name)
    sleep 0.1
  end
end

#watch_till_done!Object



18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/dapp/kube/kubernetes/manager/job.rb', line 18

def watch_till_done!
  wait_till_exists!

  job = Kubernetes::Client::Resource::Job.new(dapp.kubernetes.job(name))

  loop do
    # Получить очередной pod для обработки
    process_pod = dapp.kubernetes.pod_list.fetch('items', [])
      .select do |pod_spec|
        pod_spec.fetch('metadata', {}).fetch('labels', {})['controller-uid'] == job.uid
      end
      .reject do |pod_spec|
        @processed_pods_names.include? pod_spec.fetch('metadata', {})['name']
      end
      .sort_by do |pod_spec|
        Time.parse(pod_spec.fetch('metadata', {})['creationTimestamp'])
      end
      .map {|pod_spec| Kubernetes::Client::Resource::Pod.new(pod_spec)}
      .first

    if process_pod.nil?
      job = Kubernetes::Client::Resource::Job.new(dapp.kubernetes.job(name))
      if job.succeeded?
        break
      elsif job.failed?
        dapp.log_warning "Job '#{name}' has been failed: #{job.spec['status']}"
        break
      end

      sleep 0.1

      next
    end

    pod_manager = Kubernetes::Manager::Pod.new(dapp, process_pod.name)
    begin
      pod_manager.watch_till_done!
    rescue Kubernetes::Client::Error::Pod::NotFound => err
      dapp.log_warning "Pod '#{pod_manager.name}' has been deleted"
    ensure
      @processed_pods_names << process_pod.name
    end
  end
end