Class: VCAP::Services::Base::AsyncJob::Snapshot::SnapshotJob

Inherits:
Object
  • Object
show all
Includes:
Resque::Plugins::Status, VCAP::Services::Base::AsyncJob::Snapshot
Defined in:
lib/base/job/snapshot.rb

Overview

common utils for snapshot job

Constant Summary

Constants included from VCAP::Services::Base::AsyncJob::Snapshot

FILTER_KEYS, MAX_NAME_LENGTH, SNAPSHOT_ID, SNAPSHOT_KEY_PREFIX

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from VCAP::Services::Base::AsyncJob::Snapshot

#client, #delete_snapshot, #filter_keys, #fmt_time, #new_snapshot_id, redis_connect, redis_init, #save_snapshot, #service_snapshots, #service_snapshots_count, #snapshot_details, #snapshot_filepath, #update_name

Methods included from Error

#failure, #internal_fail, #parse_msg, #success, #timeout_fail

Constructor Details

#initialize(*args) ⇒ SnapshotJob

Returns a new instance of SnapshotJob.



161
162
163
164
165
166
# File 'lib/base/job/snapshot.rb', line 161

def initialize(*args)
  super(*args)
  parse_config
  init_worker_logger
  Snapshot.redis_connect
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



140
141
142
# File 'lib/base/job/snapshot.rb', line 140

def name
  @name
end

#snapshot_idObject (readonly)

Returns the value of attribute snapshot_id.



140
141
142
# File 'lib/base/job/snapshot.rb', line 140

def snapshot_id
  @snapshot_id
end

Class Method Details

.queue_lookup_keyObject



146
147
148
# File 'lib/base/job/snapshot.rb', line 146

def queue_lookup_key
  :node_id
end

.select_queue(*args) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/base/job/snapshot.rb', line 150

def select_queue(*args)
  result = nil
  args.each do |arg|
    result = arg[queue_lookup_key]if (arg.is_a? Hash)&& (arg.has_key?(queue_lookup_key))
  end
  @logger = Config.logger
  @logger.info("Select queue #{result} for job #{self.class} with args:#{args.inspect}") if @logger
  result
end

Instance Method Details

#cleanup(name, snapshot_id) ⇒ Object



197
198
199
200
201
202
# File 'lib/base/job/snapshot.rb', line 197

def cleanup(name, snapshot_id)
  return unless name && snapshot_id
  @logger.info("Clean up snapshot and files for #{name}, snapshot id: #{snapshot_id}")
  delete_snapshot(name, snapshot_id)
  FileUtils.rm_rf(get_dump_path(name, snapshot_id))
end

#create_lockObject



181
182
183
184
185
186
# File 'lib/base/job/snapshot.rb', line 181

def create_lock
  lock_name = "lock:lifecycle:#{name}"
  ttl = @config['job_ttl'] || 600
  lock = Lock.new(lock_name, :logger => @logger, :ttl => ttl)
  lock
end

#fmt_error(e) ⇒ Object



168
169
170
# File 'lib/base/job/snapshot.rb', line 168

def fmt_error(e)
  "#{e}: [#{e.backtrace.join(" | ")}]"
end

#get_dump_path(name, snapshot_id) ⇒ Object



188
189
190
# File 'lib/base/job/snapshot.rb', line 188

def get_dump_path(name, snapshot_id)
  snapshot_filepath(@config["snapshots_base_dir"], @config["service_name"], name, snapshot_id)
end

#handle_error(e) ⇒ Object



204
205
206
207
208
209
# File 'lib/base/job/snapshot.rb', line 204

def handle_error(e)
  @logger.error("Error in #{self.class} uuid:#{@uuid}: #{fmt_error(e)}")
  err = (e.instance_of?(ServiceError)? e : ServiceError.new(ServiceError::INTERNAL_ERROR)).to_hash
  err_msg = Yajl::Encoder.encode(err["msg"])
  failed(err_msg)
end

#init_worker_loggerObject



172
173
174
# File 'lib/base/job/snapshot.rb', line 172

def init_worker_logger
  @logger = Config.logger
end

#parse_configObject



192
193
194
195
# File 'lib/base/job/snapshot.rb', line 192

def parse_config
  @config = Yajl::Parser.parse(ENV['WORKER_CONFIG'])
  raise "Need environment variable: WORKER_CONFIG" unless @config
end

#required_options(*args) ⇒ Object

Raises:

  • (ArgumentError)


176
177
178
179
# File 'lib/base/job/snapshot.rb', line 176

def required_options(*args)
  missing_opts = args.select{|arg| !options.has_key? arg.to_s}
  raise ArgumentError, "Missing #{missing_opts.join(', ')} in options: #{options.inspect}" unless missing_opts.empty?
end