Class: Mir::Models::Resource

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/mir/models/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.chunked_groups(qry_func, chunk_size) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/mir/models/resource.rb', line 54

def self.chunked_groups(qry_func, chunk_size)
  num_results = Resource.count
  offset = 0
  pages = (num_results/chunk_size.to_f).ceil
  pages.times do |i|
    response = qry_func.call().limit(chunk_size).offset(i*chunk_size)
    yield response
  end
end

.create_from_file_and_name(file, name) ⇒ Resource

Builds a resource for the backup index from a file

Parameters:

  • a (File)

    file object

  • the (String)

    name of the file on the remote disk

Returns:

  • (Resource)

    a new Resource instance that with a queued status



14
15
16
17
18
19
20
21
22
23
# File 'lib/mir/models/resource.rb', line 14

def self.create_from_file_and_name(file, name)
  is_dir = File.directory?(file)
  create(:filename => name,
         :size => file.size,
         :last_modified => file.ctime,
         :add_date => DateTime.now,
         :queued => !is_dir,
         :checksum => is_dir ? nil : Digest::MD5.file(file).to_s,
         :is_directory => is_dir)
end

.delete_all_except(index_date) ⇒ Object

Removes all resources not that were not indexed on the specified date

Parameters:

  • (DateTime)


29
30
31
# File 'lib/mir/models/resource.rb', line 29

def self.delete_all_except(index_date)
  not_indexed_on(index_date).delete_all
end

.ordered_groups(group_size = 10) {|Array| ... } ⇒ Object

Returns groups of file resources ordered by name

Parameters:

  • the (Integer)

    number of records to return per chunk

Yields:

  • (Array)

    instances of Models::Resource



49
50
51
52
# File 'lib/mir/models/resource.rb', line 49

def self.ordered_groups(group_size = 10)
  qry = lambda { Resource.order(:filename) }
  chunked_groups(qry, group_size) { |chunk| yield chunk }
end

.pending_jobs?Boolean

Returns true when jobs are still queued

Returns:

  • (Boolean)


34
35
36
# File 'lib/mir/models/resource.rb', line 34

def self.pending_jobs?
  self.where(:queued => true).size > 0
end

.pending_sync_groups(response_size, &block) ⇒ Object

Yields a Models::Resource object that needs to be synchronized



39
40
41
42
# File 'lib/mir/models/resource.rb', line 39

def self.pending_sync_groups(response_size, &block)
  qry = lambda { Resource.where(:queued => true, :is_directory => false) }
  chunked_groups(qry, response_size) { |chunk| yield chunk }
end

Instance Method Details

#abs_pathObject



110
111
112
# File 'lib/mir/models/resource.rb', line 110

def abs_path
  File.join(Models::AppSetting.backup_path, filename)
end

#flag_for_updateObject

Places the resource into a queueble state



87
88
89
90
91
# File 'lib/mir/models/resource.rb', line 87

def flag_for_update
  update_attributes :queued => true, 
                    :checksum => Digest::MD5.file(abs_path).to_s,
                    :last_modified => File.new(abs_path).ctime
end

#start_progressObject



93
94
95
# File 'lib/mir/models/resource.rb', line 93

def start_progress
  update_attribute :in_progress, true
end

#synchronizable?Boolean

Whether the item can be synchronized to a remote disk

Returns:

  • (Boolean)

    true when the resource is not a directory



82
83
84
# File 'lib/mir/models/resource.rb', line 82

def synchronizable?
  !is_directory?
end

#synchronized?(file) ⇒ Boolean

Compares a file asset to the index to deterimine whether the file needs to be updated

Parameters:

  • a (String)

    path to a file or directory

Returns:

  • (Boolean)

    returns true when the file’s checksum is equal to the value stored in the index, or when the file is a directory



70
71
72
73
74
75
76
77
78
# File 'lib/mir/models/resource.rb', line 70

def synchronized?(file)
  if File.directory?(file)
    return true
  elsif !File.exist?(file) or in_progress? or queued?
    return false
  else
    Digest::MD5.file(file).to_s == self.checksum
  end
end

#update_failureObject



104
105
106
107
108
# File 'lib/mir/models/resource.rb', line 104

def update_failure
  num_times_failed = times_failed + 1
  will_requeue = (num_times_failed < Mir::Application.config.max_upload_attempts)
  update_attributes :times_failed => num_times_failed, :in_progress => false, :queued => will_requeue
end

#update_successObject



97
98
99
100
101
102
# File 'lib/mir/models/resource.rb', line 97

def update_success
  update_attributes :in_progress => false, 
                    :last_synchronized => DateTime.now, 
                    :queued => false,
                    :times_failed => 0
end