Class: VCAP::Services::Base::SnapshotV2::SnapshotJob

Inherits:
Object
  • Object
show all
Includes:
Resque::Plugins::Status
Defined in:
lib/base/snapshot_v2/snapshot.rb,
lib/base/snapshot_v2/snapshot_job.rb

Overview

common utils for snapshot job

Defined Under Namespace

Classes: QueueResolver

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ SnapshotJob

Returns a new instance of SnapshotJob.



35
36
37
38
39
40
# File 'lib/base/snapshot_v2/snapshot.rb', line 35

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.



13
14
15
# File 'lib/base/snapshot_v2/snapshot.rb', line 13

def name
  @name
end

#snapshot_idObject (readonly)

Returns the value of attribute snapshot_id.



13
14
15
# File 'lib/base/snapshot_v2/snapshot.rb', line 13

def snapshot_id
  @snapshot_id
end

Class Method Details

.queue_lookup_keyObject



20
21
22
# File 'lib/base/snapshot_v2/snapshot.rb', line 20

def queue_lookup_key
  :node_id
end

.select_queue(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/base/snapshot_v2/snapshot.rb', line 24

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



71
72
73
74
75
76
# File 'lib/base/snapshot_v2/snapshot.rb', line 71

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



55
56
57
58
59
60
# File 'lib/base/snapshot_v2/snapshot.rb', line 55

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



42
43
44
# File 'lib/base/snapshot_v2/snapshot.rb', line 42

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

#get_dump_path(name, snapshot_id) ⇒ Object



62
63
64
# File 'lib/base/snapshot_v2/snapshot.rb', line 62

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

#handle_error(e) ⇒ Object



78
79
80
81
82
83
# File 'lib/base/snapshot_v2/snapshot.rb', line 78

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



46
47
48
# File 'lib/base/snapshot_v2/snapshot.rb', line 46

def init_worker_logger
  @logger = Config.logger
end

#parse_configObject

end



89
90
91
92
# File 'lib/base/snapshot_v2/snapshot_job.rb', line 89

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)


50
51
52
53
# File 'lib/base/snapshot_v2/snapshot.rb', line 50

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