Class: Cnvrg::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/task.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_path, path: nil, content: {}) ⇒ Task

Returns a new instance of Task.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/cnvrg/task.rb', line 4

def initialize(project_path, path: nil, content: {})
  @path = path
  @content = content
  @source = 'flow' if @content.present?
  @source = 'file' if @path.present?
  @project = Cnvrg::Project.new(project_path)
  @project_path = project_path
  #generic
  @type = nil #type can be exec, data, library, deploy
  @compute = 'medium'
  @title = nil
  @uid = nil
  @params = {}

  #exec
  @cmd = nil
  @params_path = nil

  #library
  @library = nil

  #dataset
  @dataset = nil
  @query = nil

  #deploy
  @function = nil


  @base_resource = @project.base_resource + "/tasks"

  self.reload_task
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/cnvrg/task.rb', line 3

def path
  @path
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/cnvrg/task.rb', line 3

def source
  @source
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/cnvrg/task.rb', line 3

def title
  @title
end

Instance Method Details

#reload_taskObject



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
# File 'lib/cnvrg/task.rb', line 43

def reload_task
  task_raw = get_content
  @title = task_raw[:title]
  @type = task_raw[:type]
  @uid = task_raw[:uid]
  @title = task_raw[:title] || @uid
  @compute = task_raw[:compute] || @compute

  case @type
  when 'exec'
    @cmd = task_raw[:cmd]
    init_params(task_raw)
  when 'library'
    @library = task_raw[:library]
    init_params(task_raw)
  when 'data'
    @dataset = task_raw[:dataset]
    @query = task_raw[:query]
  when 'deploy'
    @cmd = task_raw[:cmd]
    @function = task_raw[:function]
  else
    error("Cant parse task of type #{@type}")
  end
end

#runObject



89
90
91
92
93
94
95
96
97
# File 'lib/cnvrg/task.rb', line 89

def run
  verify_task
  if @type == 'data'
    raise StandardError.new("Data Tasks are not runnable")
  end
  resp = Cnvrg::API.request(@base_resource, "POST", {task: to_api})
  Cnvrg::CLI.is_response_success(resp, true)
  Cnvrg::Helpers.remote_url + resp['result']['url']
end

#saveObject



38
39
40
41
# File 'lib/cnvrg/task.rb', line 38

def save
  path = @path || gen_path
  File.open(path, 'w'){|f| f.write(get_content.to_yaml)}
end

#to_apiObject



85
86
87
# File 'lib/cnvrg/task.rb', line 85

def to_api
  get_content.merge(params: @params)
end

#verify_taskObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cnvrg/task.rb', line 69

def verify_task
  case @type
  when 'exec'
    verify_exec
  when 'data'
    verify_data
  when 'deploy'
    verify_deploy
  when 'library'
    verify_library
  else
    error("Cant parse task of type #{@type}")
  end
end