Class: VCAP::Services::Base::AsyncJob::Snapshot::BaseCreateSnapshotJob

Inherits:
SnapshotJob show all
Defined in:
lib/base/job/snapshot.rb

Constant Summary

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

FILTER_KEYS, MAX_NAME_LENGTH, SNAPSHOT_ID, SNAPSHOT_KEY_PREFIX

Instance Attribute Summary

Attributes inherited from SnapshotJob

#name, #snapshot_id

Instance Method Summary collapse

Methods inherited from SnapshotJob

#cleanup, #create_lock, #fmt_error, #get_dump_path, #handle_error, #init_worker_logger, #initialize, #parse_config, queue_lookup_key, #required_options, select_queue

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

This class inherits a constructor from VCAP::Services::Base::AsyncJob::Snapshot::SnapshotJob

Instance Method Details

#performObject

workflow template Sub class should implement execute method which returns hash represents of snapshot like: => 1,

:size => 100,
:files => ["my_snapshot.tgz", "readme.txt"]
:manifest => {:version => '1', :service => 'mysql'
}


220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/base/job/snapshot.rb', line 220

def perform
  begin
    required_options :service_id
    @name = options["service_id"]
    @metadata = VCAP.symbolize_keys(options["metadata"])
    @logger.info("Launch job: #{self.class} for #{name} with metadata: #{@metadata}")

    @snapshot_id = new_snapshot_id
    lock = create_lock

    @snapshot_files = []
    lock.lock do
      quota = @config["snapshot_quota"]
      if quota
        current = service_snapshots_count(name)
        @logger.debug("Current snapshots count for #{name}: #{current}, max: #{quota}")
        raise ServiceError.new(ServiceError::OVER_QUOTA, name, current, quota) if current >= quota
      end

      snapshot = execute
      snapshot = VCAP.symbolize_keys snapshot
      snapshot[:manifest] ||= {}
      snapshot[:manifest].merge! @metadata
      @logger.info("Results of create snapshot: #{snapshot.inspect}")

      # pack snapshot_file into package
      dump_path = get_dump_path(name, snapshot_id)
      FileUtils.mkdir_p(dump_path)
      package_file = "#{snapshot_id}.zip"

      package = Package.new(File.join(dump_path, package_file))
      package.manifest = snapshot[:manifest]
      files = Array(snapshot[:files])
      raise "No snapshot file to package." if files.empty?
      files.each do |f|
        full_path = File.join(dump_path, f)
        @snapshot_files << full_path
        package.add_files full_path
      end
      package.pack(dump_path)
      @logger.info("Package snapshot file: #{File.join(dump_path, package_file)}")

      # update snapshot metadata for package file
      snapshot.delete(:files)
      snapshot[:file] = package_file
      snapshot[:date] = fmt_time
      # add default service name
      snapshot[:name] = "Snapshot #{snapshot[:date]}"

      save_snapshot(name, snapshot)

      completed(Yajl::Encoder.encode(filter_keys(snapshot)))
      @logger.info("Complete job: #{self.class} for #{name}")
    end
  rescue => e
    cleanup(name, snapshot_id)
    handle_error(e)
  ensure
    set_status({:complete_time => Time.now.to_s})
    @snapshot_files.each{|f| File.delete(f) if File.exists? f} if @snapshot_files
  end
end