Class: BlackStack::NextBot::Command

Inherits:
Object
  • Object
show all
Includes:
BaseCommand
Defined in:
lib/localcommand.rb

Overview

The “local” (or persistance class) that will load, update and create records in the database.

Constant Summary

Constants included from BaseCommand

BaseCommand::STATUS_CANCELED, BaseCommand::STATUS_FAILURE, BaseCommand::STATUS_ONGOING, BaseCommand::STATUS_PENDING, BaseCommand::STATUS_SUCCESS, BaseCommand::TYPE_CLOSE, BaseCommand::TYPE_GOTO, BaseCommand::TYPE_LOGIN, BaseCommand::TYPE_START_BOT, BaseCommand::TYPE_TRAFFIC

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BaseCommand

statusColor, statusDescription, statuses, typeDescription, types

Class Method Details

.load_next(id_worker) ⇒ Object

load the next command to be performed by a certain worker.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/localcommand.rb', line 26

def self.load_next(id_worker)
  row = DB[
    "SELECT c.id 
    FROM nbcommand c WITH (NOLOCK)
    WHERE c.id_worker = '#{id_worker}'
    AND c.run_end_time IS NULL
    ORDER BY c.create_time ASC"
  ].first
  return nil if row.nil?
  return BlackStack::NextBot::Command.where(:id => row[:id]).first if !row.nil?
end

Instance Method Details

#end(success = true, error_description = nil) ⇒ Object

update the end time, success flag and error description of the command.



45
46
47
48
49
50
# File 'lib/localcommand.rb', line 45

def end(success=true, error_description=nil)
  self.run_end_time = now()
  self.run_success = success
  self.run_error_description = error_description
  self.save
end

#startObject

update the start time of the command.



39
40
41
42
# File 'lib/localcommand.rb', line 39

def start()
  self.run_start_time = now()
  self.save
end

#statusObject

decide the status of this command, based on both the run_start_time, run_end_time and run_error_description



14
15
16
17
18
19
20
21
22
23
# File 'lib/localcommand.rb', line 14

def status()
  com = self
  commandstatus = nil
  commandstatus = BlackStack::NextBot::BaseCommand::STATUS_PENDING  if com.run_start_time.nil?
  commandstatus = BlackStack::NextBot::BaseCommand::STATUS_ONGOING  if !com.run_start_time.nil? && com.run_end_time.nil? #&& com.run_error_description.nil?
  commandstatus = BlackStack::NextBot::BaseCommand::STATUS_FAILURE  if !com.run_start_time.nil? && !com.run_end_time.nil? && !com.run_success
  commandstatus = BlackStack::NextBot::BaseCommand::STATUS_SUCCESS  if !com.run_start_time.nil? && !com.run_end_time.nil? && com.run_success
  commandstatus = BlackStack::NextBot::BaseCommand::STATUS_CANCELED if !com.run_start_time.nil? && !com.run_end_time.nil? && com.run_success.nil?
  commandstatus
end

#to_hashObject

return a hash descriptor of this object, in order to send it to a remote process as a response to an API call.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/localcommand.rb', line 55

def to_hash()
  ret = {}
  ret[:id] = self.id.to_guid
  ret[:id_worker] = self.id_worker.to_guid
  ret[:type] = self.type
  # start command params
  ret[:param_start_id_lnuser] = self.param_start_id_lnuser
  ret[:param_start_username] = self.param_start_id_lnuser.nil? ? nil : LnUser.where(:id=>self.param_start_id_lnuser).first.username
  # login command params 
  ret[:param_login_id_domain] = self..nil? ? nil : self..to_guid
  # goto command params
  ret[:param_goto_url] = self.param_goto_url
  # traffic command params
  ret[:param_traffic_url] = self.param_traffic_url
  ret[:param_traffic_id_proxy] = self.param_traffic_id_proxy
  ret[:param_traffic_number_of_visits] = self.param_traffic_number_of_visits
  ret[:param_traffic_visit_min_seconds] = self.param_traffic_visit_min_seconds
  ret[:param_traffic_visit_random_additional_seconds] = self.param_traffic_visit_random_additional_seconds
  # command
  return ret
end