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.



34
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
67
68
69
70
71
72
73
74
75
# File 'lib/rest-ftp-daemon/job.rb', line 34

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 super-default flags
  flag_default :mkdir, false
  flag_default :overwrite, false
  flag_default :tempfile, false

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

  # 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
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.



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

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.



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

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.



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

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.



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

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

#updated_atObject (readonly)

Returns the value of attribute updated_at.



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

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



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

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

#exectimeObject



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

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

#get(attribute) ⇒ Object



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

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

#oops_after_crash(exception) ⇒ Object



216
217
218
# File 'lib/rest-ftp-daemon/job.rb', line 216

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

#oops_you_stop_now(exception) ⇒ Object



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

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

#processObject



77
78
79
80
81
82
83
84
85
86
87
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
# File 'lib/rest-ftp-daemon/job.rb', line 77

def process
  # Update job's status
  @error = nil
  log_info "Job.process"

  # Prepare job
  begin
    set_status :prepare
    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 URI::InvalidURIError => exception
    return oops :started, exception, :target_invalid

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

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

  # Process job
  begin
    set_status :starting
    transfer

  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 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

#set_queuedObject



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

def set_queued
  # Update job status
  set_status JOB_STATUS_QUEUED
end

#weightObject



198
199
200
201
202
203
204
# File 'lib/rest-ftp-daemon/job.rb', line 198

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