Class: RestFtpDaemon::Job

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

Overview

Reprensents work to be done along with parameters to process it

Constant Summary collapse

FIELDS =
[:source, :target, :label, :priority, :notify, :overwrite, :mkdir, :tempfile]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Job.



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
62
63
64
65
66
# File 'lib/rest-ftp-daemon/job.rb', line 35

def initialize job_id, params = {}
  # Call super
  # super()

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

  # Logger
  @logger = RestFtpDaemon::LoggerPool.instance.get :jobs

  # Protect with a mutex
  @mutex = Mutex.new

  # Import query params
  FIELDS.each do |name|
    instance_variable_set "@#{name}", params[name]
  end

  # Set job queue, thus reset
  reset

  # Read source file size and parameters
  @notify_after_sec = Settings.at(:transfer, :notify_after_sec) rescue nil
end

Instance Attribute Details

#errorObject (readonly)

Returns the value of attribute error.



19
20
21
# File 'lib/rest-ftp-daemon/job.rb', line 19

def error
  @error
end

#finished_atObject (readonly)

Returns the value of attribute finished_at.



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

def finished_at
  @finished_at
end

#idObject (readonly)

Returns the value of attribute id.



18
19
20
# File 'lib/rest-ftp-daemon/job.rb', line 18

def id
  @id
end

#infosObject (readonly)

Returns the value of attribute infos.



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

def infos
  @infos
end

#loggerObject (readonly)

Returns the value of attribute logger.



8
9
10
# File 'lib/rest-ftp-daemon/job.rb', line 8

def logger
  @logger
end

#queued_atObject (readonly)

Returns the value of attribute queued_at.



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

def queued_at
  @queued_at
end

#runsObject (readonly)

Returns the value of attribute runs.



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

def runs
  @runs
end

#started_atObject (readonly)

Returns the value of attribute started_at.



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

def started_at
  @started_at
end

#statusObject (readonly)

Returns the value of attribute status.



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

def status
  @status
end

#target_methodObject (readonly)

Returns the value of attribute target_method.



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

def target_method
  @target_method
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



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

def updated_at
  @updated_at
end

#widObject

Returns the value of attribute wid.



16
17
18
# File 'lib/rest-ftp-daemon/job.rb', line 16

def wid
  @wid
end

Instance Method Details

#ageObject



231
232
233
234
# File 'lib/rest-ftp-daemon/job.rb', line 231

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

#exectimeObject



218
219
220
221
# File 'lib/rest-ftp-daemon/job.rb', line 218

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

#get(attribute) ⇒ Object



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

def get attribute
  @mutex.synchronize do
    @infos || {}
    @infos[attribute]
  end
end

#json_errorObject



240
241
242
# File 'lib/rest-ftp-daemon/job.rb', line 240

def json_error
  utf8 @error unless @error.nil?
end

#json_statusObject



244
245
246
# File 'lib/rest-ftp-daemon/job.rb', line 244

def json_status
  utf8 @status unless @status.nil?
end

#json_targetObject



236
237
238
# File 'lib/rest-ftp-daemon/job.rb', line 236

def json_target
  utf8 @target_method
end

#oops_after_crash(exception) ⇒ Object



223
224
225
# File 'lib/rest-ftp-daemon/job.rb', line 223

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

#oops_you_stop_now(exception) ⇒ Object



227
228
229
# File 'lib/rest-ftp-daemon/job.rb', line 227

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

#processObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rest-ftp-daemon/job.rb', line 88

def process
  # Update job's status
  log_info "Job.process"

  # Prepare job
  begin
    prepare

  rescue RestFtpDaemon::JobMissingAttribute => exception
    return oops :started, exception, :missing_attribute

  rescue RestFtpDaemon::JobUnresolvedTokens => exception
    return oops :started, exception, :unresolved_tokens

  rescue RestFtpDaemon::JobTargetUnparseable => exception
    return oops :started, exception, :target_unparseable

  rescue RestFtpDaemon::JobTargetUnsupported => exception
    return oops :started, exception, :target_unsupported

  rescue RestFtpDaemon::JobAssertionFailed => exception
    return oops :started, exception, :assertion_failed

  rescue URI::InvalidURIError => exception
    return oops :started, exception, :target_invalid

  else
    # Prepare done !
    set_status JOB_STATUS_PREPARED
    log_info "Job.process notify [started]"
    client_notify :started
  end

  # Process job
  begin
    #raise Net::FTPTempError, '451 Téléchargement avorté. Input/output error'.force_encoding("ASCII-8BIT")
    run

  rescue SocketError => exception
    return oops :ended, exception, :conn_socket_error

  rescue EOFError => exception
    return oops :ended, exception, :conn_eof

  rescue Errno::EHOSTDOWN => exception
    return oops :ended, exception, :conn_host_is_down

  rescue Errno::ENETUNREACH => exception
    return oops :ended, exception, :conn_unreachable

  rescue Errno::ECONNRESET => exception
    return oops :ended, exception, :conn_reset_by_peer

  rescue Errno::ENOTCONN => exception
    return oops :ended, exception, :conn_failed

  rescue Errno::ECONNREFUSED => exception
    return oops :ended, exception, :conn_refused

  rescue Timeout::Error, Errno::ETIMEDOUT, Net::ReadTimeout => exception
    return oops :ended, exception, :conn_timed_out

  rescue OpenSSL::SSL::SSLError => exception
    return oops :ended, exception, :conn_openssl_error

  rescue Net::FTPPermError => exception
    return oops :ended, exception, :ftp_perm_error

  rescue Net::FTPTempError => exception
    return oops :ended, exception, :net_temp_error

  rescue Net::SFTP::StatusException => exception
    return oops :ended, exception, :sftp_exception

  rescue Net::SSH::HostKeyMismatch => exception
    return oops :ended, exception, :sftp_key_mismatch

  rescue Net::SSH::AuthenticationFailed => exception
    return oops :ended, exception, :sftp_auth_failed

  rescue Errno::EMFILE => exception
    return oops :ended, exception, :too_many_open_files

  rescue Errno::EINVAL => exception
    return oops :ended, exception, :invalid_argument, true

  rescue Encoding::UndefinedConversionError => exception
    return oops :ended, exception, :encoding_error, true

  rescue RestFtpDaemon::JobSourceNotFound => exception
    return oops :ended, exception, :source_not_found

  rescue RestFtpDaemon::JobSourceNotReadable => exception
    return oops :ended, exception, :source_not_readable

  rescue RestFtpDaemon::JobTargetFileExists => exception
    return oops :ended, exception, :target_file_exists

  rescue RestFtpDaemon::JobTargetDirectoryError => exception
    return oops :ended, exception, :target_directory_missing

  rescue RestFtpDaemon::JobTargetPermissionError => exception
    return oops :ended, exception, :target_permission_error

  rescue RestFtpDaemon::JobAssertionFailed => exception
    return oops :ended, exception, :assertion_failed

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

#resetObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rest-ftp-daemon/job.rb', line 68

def reset
  # Set super-default flags
  flag_default :mkdir, false
  flag_default :overwrite, false
  flag_default :tempfile, false

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

  # Send first notification
  log_info "Job.initialize notify[queued] notify_after_sec[#{@notify_after_sec}] interval[#{JOB_UPDATE_INTERVAL}]"
  client_notify :queued

  # Update job status
  set_status JOB_STATUS_QUEUED
  set_error nil
  @infos = {}
end

#weightObject



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

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