Class: Mutx::Tasks::Task

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task_data = nil) ⇒ Task

First, try to get task info from mongo. If it does not exist creates a new one with default values



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
# File 'lib/mutx/tasks/task.rb', line 39

def initialize task_data = nil
  Mutx::Support::Log.debug "[#{task_data["_id"]}:#{task_data["name"]}] Creating task object " if Mutx::Support::Log
  if task_data.is_a? Hash
    @id               = task_data["_id"]
    @name             = task_data["name"]
    @branch           = task_data["branch"]
    @type             = task_data["type"] || "task"
    @platform         = task_data["platform"] || "command line"
    @framework        = task_data["framework"]
    @status           = task_data["status"]
    @command          = task_data["command"]
    @custom_params    = task_data["custom_params"] || []
    @information      = task_data["information"]
    @running_execs    = []
    @cucumber         = task_data["cucumber_report"]
    @cucumber_report  = task_data["cucumber"]
    @max_execs        = task_data["max_execs"] || Mutx::Support::Configuration.maximum_execs_per_task
    @cronneable       = task_data["cronneable"]
    @cron_time        = task_data["cron_time"]
    @last_exec_time   = task_data["last_exec_time"]
    @application      = task_data["application"] || "command line"
  else
    Mutx::Support::Log.error "Creting task object. Argument is not a hash" if Mutx::Support::Log
    raise "Task data not defined correctly. Expecting info about task"
  end
  save!
end

Instance Attribute Details

#applicationObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def application
  @application
end

#branchObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def branch
  @branch
end

#commandObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def command
  @command
end

#cron_timeObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def cron_time
  @cron_time
end

#cronneableObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def cronneable
  @cronneable
end

#cucumberObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def cucumber
  @cucumber
end

#custom_paramsObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def custom_params
  @custom_params
end

#frameworkObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def framework
  @framework
end

#idObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def id
  @id
end

#informationObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def information
  @information
end

#last_exec_timeObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def last_exec_time
  @last_exec_time
end

#max_execsObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def max_execs
  @max_execs
end

#nameObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def name
  @name
end

#platformObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def platform
  @platform
end

#running_execsObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def running_execs
  @running_execs
end

#statusObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def status
  @status
end

#typeObject

Task means what a user can Run. It coul be a test or a tests suites according to the test organization and the used framework There are two types of task: A task properly said and a test. A platform can be specified. It could be one of following:

"bash" => Aimed to run bash commands
"ruby" => When you want to run ruby code
"java" => Well, if you wanto to do it, poor you :P


14
15
16
# File 'lib/mutx/tasks/task.rb', line 14

def type
  @type
end

Class Method Details

.delete_this(data) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mutx/tasks/task.rb', line 152

def self.delete_this(data)
   
  if Mutx::Tasks.exist? data["_id"]
    if Mutx::Database::MongoConnector.delete_task data["_id"]
      {action:"delete", success:true, message:"Task #{data['_id']} with name #{data["name"]} deleted"}
    else
      {action:"delete", success:false, message:"Could not delete task #{data['_id']} with name #{data["name"]}"}
    end
  else
    {action:"delete", success:false, message:"Could not find task to delete"}
  end
end

.get(task_id) ⇒ Object



77
78
79
80
81
82
# File 'lib/mutx/tasks/task.rb', line 77

def self.get task_id
  Mutx::Support::Log.debug "Getting task data for [id:#{task_id}]"  if Mutx::Support::Log
  task_data = Mutx::Database::MongoConnector.task_data_for task_id
  task_data = task_data.to_h if respond_to? :to_h
  new(task_data) if task_data
end

.get_task_with(name) ⇒ Object



84
85
86
# File 'lib/mutx/tasks/task.rb', line 84

def self.get_task_with name
  self.new(Mutx::Database::MongoConnector.task_data_for_name(name))
end

.new_task(data) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/mutx/tasks/task.rb', line 88

def self.new_task(data)

  Mutx::Support::Log.debug "Defining new task [#{data["name"]}]" if Mutx::Support::Log
  task_data = {
    "_id" => Mutx::Database::MongoConnector.generate_id,
    "name" => data["name"],
    "command" => data["command"],
    "type" => data["type"],
    "platform" => data["platform"],
    "information" => data["information"],
    "cucumber" => data["cucumber"],
    "branch" => Mutx::Support::Git.actual_branch,
    "status" => "READY",
    "max_execs" => data["max_execs"],
    "custom_params" => data["custom_params"],
    "cronneable" => data["cronneable"],
    "cron_time" => data["cron_time"],
    "last_exec_time" => Time.now.utc,
    "application" => data["application"]
  }
  self.new(task_data)
end

.valid_typesObject



33
34
35
# File 'lib/mutx/tasks/task.rb', line 33

def self.valid_types
  ["task","test"]
end

.validate(data) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mutx/tasks/task.rb', line 122

def self.validate(data)

  # cucumber value must be boolean
  errors = []
    if data["action"] == "edit"
      errors << self.validate_name_with_id(data["name"],data["_id"])
    else
      errors << self.validate_name(data["name"])
    end

    errors << self.validate_max_execs(data["max_execs"])

    errors << self.validate_type(data["type"])

    errors << self.validate_risk_command(data["command"])
    errors.compact
end

.validate_and_create(data) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/mutx/tasks/task.rb', line 111

def self.validate_and_create(data)
  errors = self.validate(data)
  return { success:false, message:errors.join(" ")} unless errors.empty?
  if self.new_task(data)
    {action:"create", success:true, message:"#{data['type'].capitalize} #{data["name"]} created"}
  else
    {action:"create", success:false, message:"#{data['type'].capitalize} #{data["name"]} could not be created"}
  end

end

.validate_and_update(data) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/mutx/tasks/task.rb', line 140

def self.validate_and_update(data)
  if Mutx::Tasks.exist? data["_id"]
    if Mutx::Database::MongoConnector.update_task data
      {action:"edit", success:true, message:"Task updated"}
    else
      {action:"edit", success:false, message:"Could not updated task #{data["name"]}"}
    end
  else
    {action:"edit", success:false, message:"Could not find task to update"}
  end
end

.validate_max_execs(max_execs) ⇒ Object

max_execs could not be greater than global max exec



178
179
180
# File 'lib/mutx/tasks/task.rb', line 178

def self.validate_max_execs max_execs
  return "Maximum executions cannot be greater than #{Mutx::Support::Configuration.max_execs}" if max_execs > Mutx::Support::Configuration.max_execs
end

.validate_name(name) ⇒ Object

task name must be unique



166
167
168
# File 'lib/mutx/tasks/task.rb', line 166

def self.validate_name(name)
  return "There is another task with '#{name}' name." if Mutx::Tasks.is_there_task_with? name
end

.validate_name_with_id(name, id) ⇒ Object



170
171
172
173
174
175
# File 'lib/mutx/tasks/task.rb', line 170

def self.validate_name_with_id(name, id)
  if Mutx::Tasks.is_there_task_with? name
    existing_id = Mutx::Tasks.task_id_for name
    return "There is another task with '#{name}' name." if existing_id != id
  end
end

.validate_risk_command(command) ⇒ Object

command must be evaluated for risks



188
189
190
191
# File 'lib/mutx/tasks/task.rb', line 188

def self.validate_risk_command command
  return "Your commands seems to be unsecure" unless Mutx::Support::Risk.secure? command

end

.validate_type(type) ⇒ Object

type could be only task or test



183
184
185
# File 'lib/mutx/tasks/task.rb', line 183

def self.validate_type type
  return "#{type} type is not permited." unless self.valid_types.include? type
end

Instance Method Details

#all_resultsObject



292
293
294
# File 'lib/mutx/tasks/task.rb', line 292

def all_results
  Mutx::Database::MongoConnector.results_for(id)
end

#all_results_idsObject



288
289
290
# File 'lib/mutx/tasks/task.rb', line 288

def all_results_ids
  all_results.inject([]){|res, result| res << result["_id"]}
end

#api_responseObject



68
69
70
71
72
73
74
75
# File 'lib/mutx/tasks/task.rb', line 68

def api_response
  response = task_data_structure
  response["results"]={
    "size" => number_of_results,
    "ids" => all_results_ids
  }
  response
end

#check_last_result!Object

If test tasks ir running



311
312
313
# File 'lib/mutx/tasks/task.rb', line 311

def check_last_result!
  raise "NO SE PUEDE USAR MAS ESTO, AHORA HAY QUE CHEQUEAR POR SEPARADO"
end

#delete_exec(result_id) ⇒ Object



279
280
281
282
# File 'lib/mutx/tasks/task.rb', line 279

def delete_exec result_id
  @running_execs.delete result_id
  self.save!
end

#has_custom_params?Boolean

Returns:

  • (Boolean)


220
221
222
# File 'lib/mutx/tasks/task.rb', line 220

def has_custom_params?
  !@custom_params.empty?
end

#has_info?Boolean

# def required_custom_params

#   all_required = Mutx::Task::Custom::Params.all_required_ids
#   custom_params.select{|param| all_required.include? param["_id"]}.map{|param| param["name"]}
# end

Returns:

  • (Boolean)


232
233
234
# File 'lib/mutx/tasks/task.rb', line 232

def has_info?
  not @information.empty?
end

#has_results?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/mutx/tasks/task.rb', line 296

def has_results?
  number_of_results > 0
end

#is_ready?Boolean

def deactivate!

raise "deactivate! => DEPRECATED #{__FILE__}:#{__LINE__}"
# @cucumber = false
# Mutx::Support::Log.debug "[#{@id}:#{@name}] Deactivated" if Mutx::Support::Log
# self.save!

end

Returns:

  • (Boolean)


254
255
256
# File 'lib/mutx/tasks/task.rb', line 254

def is_ready?
  status == "READY"
end

#is_running?Boolean

Returns:

  • (Boolean)


258
259
260
# File 'lib/mutx/tasks/task.rb', line 258

def is_running?
  status == "RUNNING"
end

#number_of_resultsObject



284
285
286
# File 'lib/mutx/tasks/task.rb', line 284

def number_of_results
  all_results.size
end

#push_exec(result_id) ⇒ Object



274
275
276
277
# File 'lib/mutx/tasks/task.rb', line 274

def push_exec result_id
  @running_execs << result_id
  self.save!
end

#save!Object



300
301
302
303
304
305
306
307
308
# File 'lib/mutx/tasks/task.rb', line 300

def save!
  if Mutx::Database::MongoConnector.task_data_for(id)
  # unless Mutx::Database::MongoConnector.task_data_for(id).empty?
    Mutx::Database::MongoConnector.update_task(task_data_structure)
  else
    Mutx::Database::MongoConnector.insert_task(task_data_structure)
  end
  Mutx::Support::Log.debug "[#{@id}:#{@name}] Task saved" if Mutx::Support::Log
end

#set_ready!Object



262
263
264
265
266
# File 'lib/mutx/tasks/task.rb', line 262

def set_ready!
  @status = "READY"
  Mutx::Support::Log.debug "[#{@id}:#{@name}] Marked as ready" if Mutx::Support::Log
  self.save!
end

#set_running!Object



268
269
270
271
272
# File 'lib/mutx/tasks/task.rb', line 268

def set_running!
  @status= "RUNNING"
  Mutx::Support::Log.debug "[#{@id}:#{@name}] Marked as running" if Mutx::Support::Log
  self.save!
end

#task_data_for(task_name) ⇒ Object



193
194
195
# File 'lib/mutx/tasks/task.rb', line 193

def task_data_for task_name
  Mutx::Database::MongoConnector.task_data_for(task_name)
end

#task_data_structureHash

Returns the structure of a task data

Returns:

  • (Hash)

    data structure



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/mutx/tasks/task.rb', line 199

def task_data_structure
  {
    "_id" => id,
    "name" => name,
    "branch" => branch,
    "type" => type,
    "status" => status,
    "command" => command,
    "custom_params" => custom_params,
    "information"  => information,
    "running_execs" => running_execs,
    "max_execs" => max_execs,
    "cucumber" => cucumber,
    "platform" => platform,
    "cronneable" => cronneable,
    "cron_time" => cron_time,
    "last_exec_time" => last_exec_time,
    "application" => application
  }
end

#test?Boolean

Returns:

  • (Boolean)


236
237
238
# File 'lib/mutx/tasks/task.rb', line 236

def test?
  self.type == "test"
end