Class: VCAP::Services::Base::AsyncJob::Serialization::BaseImportFromDataJob

Inherits:
SerializationJob show all
Defined in:
lib/base/job/serialization.rb

Overview

Create a new snapshot with the given temp file

Constant Summary

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

VCAP::Services::Base::AsyncJob::Snapshot::FILTER_KEYS, VCAP::Services::Base::AsyncJob::Snapshot::MAX_NAME_LENGTH, VCAP::Services::Base::AsyncJob::Snapshot::SNAPSHOT_ID, VCAP::Services::Base::AsyncJob::Snapshot::SNAPSHOT_KEY_PREFIX

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

SERIALIZATION_KEY_PREFIX

Instance Attribute Summary collapse

Attributes inherited from SerializationJob

#name

Instance Method Summary collapse

Methods inherited from SerializationJob

#create_lock, #delete_download_token, #get_dump_path, #handle_error, #init_worker_logger, #initialize, #parse_config, queue_lookup_key, #required_options, select_queue, #snapshot_filename, #update_download_token, #validate_input

Methods included from Error

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

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 VCAP::Services::Base::AsyncJob::Serialization

#fmt_error, redis_connect, #redis_key

Constructor Details

This class inherits a constructor from VCAP::Services::Base::AsyncJob::Serialization::SerializationJob

Instance Attribute Details

#snapshot_idObject (readonly)

Returns the value of attribute snapshot_id.



304
305
306
# File 'lib/base/job/serialization.rb', line 304

def snapshot_id
  @snapshot_id
end

#temp_file_pathObject (readonly)

Returns the value of attribute temp_file_path.



304
305
306
# File 'lib/base/job/serialization.rb', line 304

def temp_file_path
  @temp_file_path
end

Instance Method Details

#executeObject

empty



360
361
362
# File 'lib/base/job/serialization.rb', line 360

def execute
  true
end

#performObject



306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/base/job/serialization.rb', line 306

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

    lock = create_lock
    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

      raise "Can't find temp file: #{@temp_file_path}" unless File.exists? temp_file_path
      raise ServiceError.new(ServiceError::BAD_SERIALIZED_DATAFILE, "request") unless validate_package(temp_file_path)

      @snapshot_id = new_snapshot_id
      @snapshot_path = get_dump_path(name, snapshot_id)
      @snapshot_file = File.join(@snapshot_path, "#{snapshot_id}.zip")
      # clean any data in snapshot folder
      FileUtils.rm_rf(@snapshot_path)
      FileUtils.mkdir_p(@snapshot_path)

      result = execute
      @logger.info("Results of import from url: #{result}")

      FileUtils.mv(@temp_file_path, @snapshot_file)

      snapshot = {
        :snapshot_id => snapshot_id,
        :size => File.open(@snapshot_file) {|f| f.size },
        :date => fmt_time,
        :file => snapshot_filename(name, snapshot_id)
      }
      save_snapshot(name, snapshot)
      @logger.info("Create new snapshot for #{name}:#{snapshot}")

      completed(Yajl::Encoder.encode(filter_keys(snapshot)))
      @logger.info("Complete job: #{self.class} for #{name}")
    end
  rescue => e
    handle_error(e)
    delete_snapshot(name, snapshot_id) if snapshot_id
    FileUtils.rm_rf(@snapshot_path) if @snapshot_path
  ensure
    set_status({:complete_time => Time.now.to_s})
    FileUtils.rm_rf(@temp_file_path) if @temp_file_path
  end
end