Class: MultiSync::Client

Inherits:
Object
  • Object
show all
Includes:
Mixins::PluralizeHelper
Defined in:
lib/multi_sync/client.rb

Instance Method Summary collapse

Methods included from Mixins::PluralizeHelper

#pluralize

Constructor Details

#initialize(*args) ⇒ Client

Initialize a new Client object

Parameters:

  • options (Hash)


28
29
30
31
# File 'lib/multi_sync/client.rb', line 28

def initialize(*args)
  self.supervisor = Celluloid::SupervisionGroup.run!
  super
end

Instance Method Details

#add_source(name, options = {}) ⇒ Object Also known as: source



43
44
45
46
47
48
49
# File 'lib/multi_sync/client.rb', line 43

def add_source(name, options = {})
  clazz = MultiSync.const_get("#{options[:type].capitalize}Source")
  sources << clazz.new(options)
rescue NameError
  MultiSync.warn "Unknown source type: #{options[:type]}"
  raise ArgumentError, "Unknown source type: #{options[:type]}"
end

#add_target(name, options = {}) ⇒ Object Also known as: target



33
34
35
36
37
38
39
40
# File 'lib/multi_sync/client.rb', line 33

def add_target(name, options = {})
  fail ArgumentError, "Duplicate target names detected, please rename '#{name}' to be unique" if supervisor_actor_names.include?(name)
  clazz = MultiSync.const_get("#{options[:type].capitalize}Target")
  supervisor.pool(clazz, as: name, args: [options], size: MultiSync.target_pool_size)
rescue NameError
  MultiSync.warn "Unknown target type: #{options[:type]}"
  raise ArgumentError, "Unknown target type: #{options[:type]}"
end

#finalizeObject Also known as: fin



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/multi_sync/client.rb', line 83

def finalize
  if finished_at
    elapsed = finished_at.to_f - started_at.to_f
    minutes, seconds = elapsed.divmod 60.0
    bytes = get_total_file_size_from_complete_jobs
    MultiSync.debug "Sync completed in #{pluralize(minutes.round, 'minute')} and #{pluralize(seconds.round, 'second')}"
    MultiSync.debug "#{pluralize(complete_jobs.length, 'file')} were synchronised (#{pluralize(get_complete_deleted_jobs.length, 'deleted file')} and #{pluralize(get_complete_upload_jobs.length, 'uploaded file')}) from #{pluralize(sources.length, 'source')} to #{pluralize(supervisor.actors.length, 'target')}"
    if bytes > 1024.0
      kilobytes = bytes / 1024.0
      MultiSync.debug "The upload weight totalled %.#{0}f #{pluralize(kilobytes, 'kilobyte', nil, false)}" % kilobytes
    else
      MultiSync.debug "The upload weight totalled %.#{0}f #{pluralize(bytes, 'byte', nil, false)}" % bytes
    end
    MultiSync.debug "#{pluralize(file_sync_attempts, 'failed request')} were detected and re-tried"
  else
    MultiSync.debug "Sync failed to complete with #{pluralize(incomplete_jobs.length, 'outstanding file')} to be synchronised"
    MultiSync.debug "#{pluralize(complete_jobs.length, 'file')} were synchronised (#{pluralize(get_complete_deleted_jobs.length, 'deleted file')} and #{pluralize(get_complete_upload_jobs.length, 'uploaded file')}) from #{pluralize(sources.length, 'source')} to #{pluralize(supervisor.actors.length, 'target')}"
  end

  supervisor.finalize
end

#get_complete_deleted_jobsObject



106
107
108
# File 'lib/multi_sync/client.rb', line 106

def get_complete_deleted_jobs
  complete_jobs.select { |job| job[:method] == :delete }
end

#get_complete_upload_jobsObject



110
111
112
# File 'lib/multi_sync/client.rb', line 110

def get_complete_upload_jobs
  complete_jobs.select { |job| job[:method] == :upload }
end

#get_total_file_size_from_complete_jobsObject



114
115
116
117
118
119
120
121
# File 'lib/multi_sync/client.rb', line 114

def get_total_file_size_from_complete_jobs
  total_file_size = 0
  get_complete_upload_jobs.each do | job |
    job_content_length = job[:response].content_length || job[:response].determine_content_length || 0
    total_file_size += job_content_length
  end
  total_file_size
end

#synchronizeObject Also known as: sync



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/multi_sync/client.rb', line 52

def synchronize
  MultiSync.debug 'Preventing synchronization as there are no sources found.' && return if sync_pointless?
  MultiSync.debug 'Starting synchronization...'

  determine_sync if first_run?
  sync_attempted

  MultiSync.debug 'Scheduling jobs in the future...'
  incomplete_jobs.delete_if do | job |
    running_jobs << { id: job[:id], future: Celluloid::Actor[job[:target_id]].future.send(job[:method], job[:args]), method: job[:method] }
  end

  MultiSync.debug 'Fetching jobs from the future...'
  running_jobs.delete_if do | job |
    begin
      completed_job = { id: job[:id], response: job[:future].value, method: job[:method] }
    rescue => error
      self.file_sync_attempts = file_sync_attempts + 1
      MultiSync.warn error.inspect
      false
    else
      complete_jobs << completed_job
      true
    end
  end

  finish_sync
  finalize
end