Class: RestFtpDaemon::Job

Inherits:
Object
  • Object
show all
Includes:
BmcDaemonLib::LoggerHelper, NewRelic::Agent::Instrumentation::ControllerInstrumentation, CommonHelpers
Defined in:
lib/rest-ftp-daemon/job.rb,
lib/rest-ftp-daemon/jobs/errors.rb

Direct Known Subclasses

JobDummy, JobTransfer, JobVideo

Constant Summary collapse

IMPORTED =

Fields to be imported from params

%w(type priority pool label priority source target overwrite mkdir tempfile video_options video_custom)
ERRORS =

Common errors

{
invalid_argument:         Errno::EINVAL,

source_not_supported:     RestFtpDaemon::SourceNotSupported,
source_not_found:         RestFtpDaemon::SourceNotFound,
target_file_exists:       RestFtpDaemon::TargetFileExists,
target_directory_error:   RestFtpDaemon::TargetDirectoryError,
target_permission_error:  RestFtpDaemon::TargetPermissionError,
target_not_supported:     RestFtpDaemon::TargetNotSupported,
assertion_failed:         RestFtpDaemon::AssertionFailed,
location_parse_error:     RestFtpDaemon::LocationParseError,

conn_socket_error:        SocketError,
conn_eof:                 EOFError,
conn_failed:              Errno::ENOTCONN,
conn_host_is_down:        Errno::EHOSTDOWN,
conn_broken_pipe:         Errno::EPIPE,
conn_unreachable:         Errno::ENETUNREACH,
conn_reset_by_peer:       Errno::ECONNRESET,
conn_refused:             Errno::ECONNREFUSED,
conn_timed_out_1:         Timeout::Error,
conn_timed_out_2:         Net::ReadTimeout,
conn_timed_out_3:         Errno::ETIMEDOUT,

ftp_connection_error:     Net::FTPConnectionError,
ftp_perm_error:           Net::FTPPermError,
ftp_reply_error:          Net::FTPReplyError,
ftp_temp_error:           Net::FTPTempError,
ftp_proto_error:          Net::FTPProtoError,
ftp_error:                Net::FTPError,

ffmpeg_error:             FFMPEG::Error,

sftp_exception:           Net::SFTP::StatusException,
sftp_key_mismatch:        Net::SSH::HostKeyMismatch,
sftp_auth_failed:         Net::SSH::AuthenticationFailed,
sftp_openssl_error:       OpenSSL::SSL::SSLError,
# rescue Encoding::UndefinedConversionError => exception
#   return oops :ended, exception, "encoding_error", true
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CommonHelpers

#dashboard_url, #exception_to_error, #format_bytes, #identifier, #underscore

Constructor Details

#initialize(job_id = nil, params = {}) ⇒ Job

Returns a new instance of Job.



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
96
97
98
# File 'lib/rest-ftp-daemon/job.rb', line 47

def initialize job_id = nil, params = {}
  # Minimal init
  @infos = {}
  @mutex = Mutex.new

  # Skip if no job_id passed or null (mock Job)
  return if job_id.nil?

  # Init context
  @id           = job_id.to_s
  @started_at   = nil
  @finished_at  = nil
  @updated_at   = nil
  @error        = nil
  @status       = nil
  @runs         = 0
  @wid          = nil

  # Prepare configuration
  @config       = Conf[:transfer] || {}
  @endpoints    = Conf[:endpoints] || {}
  @pools        = Conf[:pools] || {}

  # Logger
  @logger   = BmcDaemonLib::LoggerPool.instance.get :transfer

  # Import query params
  set_info :params, params
  IMPORTED.each do |field|
    instance_variable_set "@#{field}", params[field]
  end

  # Check if pool name exists
  @pool = DEFAULT_POOL unless @pools.keys.include?(@pool)

  # Prepare sources/target
  raise RestFtpDaemon::AttributeMissing, "source" unless params[:source]
  @source_loc = Location.new(params[:source])
  #set_info :location_source, params[:source]
  log_info "Job.initialize source #{@source_loc.uri}"

  raise RestFtpDaemon::AttributeMissing, "target" unless params[:target]
  @target_loc = Location.new(params[:target])
  #set_info :location_target, params[:target]
  log_info "Job.initialize target #{@target_loc.uri}"

  # Handle exceptions
  # rescue RestFtpDaemon::UnresolvedTokens => exception
  #   return oops :started, exception
  # rescue RestFtpDaemon::UnsupportedScheme => exception
  #   return oops :started, exception
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



27
28
29
# File 'lib/rest-ftp-daemon/job.rb', line 27

def error
  @error
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



34
35
36
# File 'lib/rest-ftp-daemon/job.rb', line 34

def finished_at
  @finished_at
end

#idObject (readonly)

Returns the value of attribute id.



26
27
28
# File 'lib/rest-ftp-daemon/job.rb', line 26

def id
  @id
end

#infosObject (readonly)

Returns the value of attribute infos.



36
37
38
# File 'lib/rest-ftp-daemon/job.rb', line 36

def infos
  @infos
end

#loggerObject (readonly)

Logging



14
15
16
# File 'lib/rest-ftp-daemon/job.rb', line 14

def logger
  @logger
end

#queued_atObject (readonly)

Returns the value of attribute queued_at.



31
32
33
# File 'lib/rest-ftp-daemon/job.rb', line 31

def queued_at
  @queued_at
end

#runsObject (readonly)

Returns the value of attribute runs.



29
30
31
# File 'lib/rest-ftp-daemon/job.rb', line 29

def runs
  @runs
end

#source_locObject (readonly)

Returns the value of attribute source_loc.



23
24
25
# File 'lib/rest-ftp-daemon/job.rb', line 23

def source_loc
  @source_loc
end

#started_atObject (readonly)

Returns the value of attribute started_at.



33
34
35
# File 'lib/rest-ftp-daemon/job.rb', line 33

def started_at
  @started_at
end

#statusObject (readonly)

Returns the value of attribute status.



28
29
30
# File 'lib/rest-ftp-daemon/job.rb', line 28

def status
  @status
end

#target_locObject (readonly)

Returns the value of attribute target_loc.



24
25
26
# File 'lib/rest-ftp-daemon/job.rb', line 24

def target_loc
  @target_loc
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



32
33
34
# File 'lib/rest-ftp-daemon/job.rb', line 32

def updated_at
  @updated_at
end

#widObject

Class options



21
22
23
# File 'lib/rest-ftp-daemon/job.rb', line 21

def wid
  @wid
end

Instance Method Details

#afterObject



160
161
# File 'lib/rest-ftp-daemon/job.rb', line 160

def after
end

#ageObject



192
193
194
195
# File 'lib/rest-ftp-daemon/job.rb', line 192

def age
  return nil if @queued_at.nil?
  (Time.now - @queued_at).round(2)
end

#beforeObject



156
157
# File 'lib/rest-ftp-daemon/job.rb', line 156

def before
end

#exectimeObject



179
180
181
182
# File 'lib/rest-ftp-daemon/job.rb', line 179

def exectime
  return nil if @started_at.nil? || @finished_at.nil?
  (@finished_at - @started_at).round(2)
end

#get_info(name) ⇒ Object



202
203
204
205
206
# File 'lib/rest-ftp-daemon/job.rb', line 202

def get_info name
  @mutex.synchronize do
    @infos[name]
  end
end

#oops_after_crash(exception) ⇒ Object



184
185
186
# File 'lib/rest-ftp-daemon/job.rb', line 184

def oops_after_crash exception
  oops :ended, exception, "crashed"
end

#oops_you_stop_now(exception) ⇒ Object



188
189
190
# File 'lib/rest-ftp-daemon/job.rb', line 188

def oops_you_stop_now exception
  oops :ended, exception, "timeout"
end

#processObject

Process job



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/rest-ftp-daemon/job.rb', line 120

def process
  # Check prerequisites
  raise RestFtpDaemon::AssertionFailed, "run/source_loc" unless @source_loc
  raise RestFtpDaemon::AssertionFailed, "run/target_loc" unless @target_loc

  # Notify we start working
  log_info "Job.process notify [started]"
  current_signal = :started
  set_status JOB_STATUS_WORKING
  client_notify :started

  # Before work
  log_debug "Job.process do_before"
  current_signal = :started
  do_before

  # Do the hard work
  log_debug "Job.process do_work"
  current_signal = :ended
  do_work

  # Finalize all this
  log_debug "Job.process do_after"
  current_signal = :ended
  do_after

rescue StandardError => exception
  return oops current_signal, exception

else
  # All done !
  set_status JOB_STATUS_FINISHED
  log_info "JobVideo.process notify [ended]"
  client_notify :ended
end

#resetObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rest-ftp-daemon/job.rb', line 100

def reset
  # Update job status
  set_status JOB_STATUS_PREPARING

  # Flag current job timestamps
  @queued_at = Time.now
  @updated_at = Time.now

  # Job has been prepared, reset infos
  set_status JOB_STATUS_PREPARED
  @infos = {}

  # Update job status, send first notification
  set_status JOB_STATUS_QUEUED
  set_error nil
  client_notify :queued
  log_info "Job.reset notify[queued]"
end

#set_info(name, value) ⇒ Object



208
209
210
211
212
213
214
# File 'lib/rest-ftp-daemon/job.rb', line 208

def set_info name, value
  @mutex.synchronize do
    @infos || {}
    @infos[name] = debug_value_utf8(value)
  end
  touch_job
end

#source_uriObject



163
164
165
# File 'lib/rest-ftp-daemon/job.rb', line 163

def source_uri
  @source_loc.uri if @source_loc
end

#target_uriObject



167
168
169
# File 'lib/rest-ftp-daemon/job.rb', line 167

def target_uri
  @target_loc.uri if @target_loc
end

#targethostObject



197
198
199
200
# File 'lib/rest-ftp-daemon/job.rb', line 197

def targethost
  @target_loc.host unless @target_loc.nil?
  #get_info :target_host
end

#weightObject



171
172
173
174
175
176
177
# File 'lib/rest-ftp-daemon/job.rb', line 171

def weight
  @weight = [
    - @runs.to_i,
    + @priority.to_i,
    - @queued_at.to_i,
    ]
end

#workObject



158
159
# File 'lib/rest-ftp-daemon/job.rb', line 158

def work
end