Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add optional syncer_id; log started/finished

  • Loading branch information...
commit f38c1cf2eb1cebf68dccedfd99c7c393b44d0ba0 1 parent 2861335
Brian D. Burns authored
View
4 lib/backup/model.rb
@@ -112,7 +112,7 @@ def store_with(name, storage_id = nil, &block)
##
# Adds a syncer method to the array of syncer
# methods to use during the backup process
- def sync_with(name, &block)
+ def sync_with(name, syncer_id = nil, &block)
##
# Warn user of DSL changes
case name.to_s
@@ -134,7 +134,7 @@ def sync_with(name, &block)
EOS
name = "Cloud::#{ syncer }"
end
- @syncers << get_class_from_scope(Syncer, name).new(&block)
+ @syncers << get_class_from_scope(Syncer, name).new(syncer_id, &block)
end
##
View
18 lib/backup/storage/base.rb
@@ -11,19 +11,17 @@ class Base
attr_accessor :keep
##
- # (Optional)
# User-defined string used to uniquely identify multiple storages of the
- # same type. This will be appended to the YAML storage file used for
- # cycling backups.
- attr_accessor :storage_id
+ # same type. If multiple storages of the same type are added to a single
+ # backup model, then this identifier must be set. This will be appended
+ # to the YAML storage file used for cycling backups.
+ attr_reader :storage_id
- ##
- # Creates a new instance of the storage object
- # * Called with super(model, storage_id) from each subclass
def initialize(model, storage_id = nil)
- load_defaults!
@model = model
@storage_id = storage_id
+
+ load_defaults!
end
##
@@ -50,8 +48,8 @@ def path; end
##
# Return the storage name, with optional storage_id
def storage_name
- self.class.to_s.sub('Backup::', '') +
- (storage_id ? " (#{storage_id})" : '')
+ @storage_name ||= self.class.to_s.sub('Backup::', '') +
+ (storage_id ? " (#{ storage_id })" : '')
end
##
View
21 lib/backup/syncer/base.rb
@@ -14,7 +14,15 @@ class Base
# Flag for mirroring the files/directories
attr_accessor :mirror
- def initialize
+ ##
+ # Optional user-defined identifier to differentiate multiple syncers
+ # defined within a single backup model. Currently this is only used
+ # in the log messages.
+ attr_reader :syncer_id
+
+ def initialize(syncer_id = nil)
+ @syncer_id = syncer_id
+
load_defaults!
@path ||= '~/backups'
@@ -36,7 +44,16 @@ def add(path)
private
def syncer_name
- self.class.to_s.sub('Backup::', '')
+ @syncer_name ||= self.class.to_s.sub('Backup::', '') +
+ (syncer_id ? " (#{ syncer_id })" : '')
+ end
+
+ def log!(action)
+ msg = case action
+ when :started then 'Started...'
+ when :finished then 'Finished!'
+ end
+ Logger.info "#{ syncer_name } #{ msg }"
end
end
View
6 lib/backup/syncer/cloud/base.rb
@@ -39,7 +39,7 @@ class Base < Syncer::Base
# If not specified in the pre-configured defaults,
# the Cloud specific defaults are set here before evaluating
# any block provided in the user's configuration file.
- def initialize
+ def initialize(syncer_id = nil)
super
@path = path.sub(/^~\//, '')
@@ -50,8 +50,8 @@ def initialize
##
# Performs the Sync operation
def perform!
+ log!(:started)
Logger.info(
- "#{ syncer_name } started the syncing process:\n" +
"\s\sConcurrency: #{ @concurrency_type } Level: #{ @concurrency_level }"
)
@@ -61,7 +61,7 @@ def perform!
).sync! @mirror, @concurrency_type, @concurrency_level
end
- Logger.info("#{ syncer_name } Syncing Complete!")
+ log!(:finished)
end
private
View
2  lib/backup/syncer/cloud/cloud_files.rb
@@ -36,7 +36,7 @@ class CloudFiles < Base
#
# Once pre-configured defaults and Cloud specific defaults are set,
# the block from the user's configuration file is evaluated.
- def initialize(&block)
+ def initialize(syncer_id = nil, &block)
super
instance_eval(&block) if block_given?
View
2  lib/backup/syncer/cloud/s3.rb
@@ -27,7 +27,7 @@ class S3 < Base
#
# Once pre-configured defaults and Cloud specific defaults are set,
# the block from the user's configuration file is evaluated.
- def initialize(&block)
+ def initialize(syncer_id = nil, &block)
super
instance_eval(&block) if block_given?
View
4 lib/backup/syncer/rsync/base.rb
@@ -31,10 +31,6 @@ def paths_to_push
directories.map {|dir| "'#{ File.expand_path(dir) }'" }.join(' ')
end
- def log!
- Logger.info "#{ syncer_name } started..."
- end
-
end
end
end
View
6 lib/backup/syncer/rsync/local.rb
@@ -5,16 +5,18 @@ module Syncer
module RSync
class Local < Base
- def initialize(&block)
+ def initialize(syncer_id = nil, &block)
super
instance_eval(&block) if block_given?
end
def perform!
- log!
+ log!(:started)
create_dest_path!
run("#{ rsync_command } #{ paths_to_push } '#{ dest_path }'")
+
+ log!(:finished)
end
private
View
4 lib/backup/syncer/rsync/pull.rb
@@ -6,12 +6,14 @@ module RSync
class Pull < Push
def perform!
- log!
+ log!(:started)
write_password_file!
create_dest_path!
run("#{ rsync_command } #{ host_options }#{ paths_to_pull } " +
"'#{ dest_path }'")
+
+ log!(:finished)
ensure
remove_password_file!
end
View
5 lib/backup/syncer/rsync/push.rb
@@ -101,7 +101,7 @@ class Push < Base
# Flag for compressing (only compresses for the transfer)
attr_accessor :compress
- def initialize(&block)
+ def initialize(syncer_id = nil, &block)
super
instance_eval(&block) if block_given?
@@ -111,13 +111,14 @@ def initialize(&block)
end
def perform!
- log!
+ log!(:started)
write_password_file!
create_dest_path!
run("#{ rsync_command } #{ paths_to_push } " +
"#{ host_options }'#{ dest_path }'")
+ log!(:finished)
ensure
remove_password_file!
end
View
16 spec/model_spec.rb
@@ -166,14 +166,15 @@ def using_fake(const, replacement)
it 'should add storages' do
using_fake('Storage', Fake::TwoArgs) do
model.store_with('Base', 'foo') {|a| a.block_arg = :foo }
- model.store_with('Base', 'bar') {|a| a.block_arg = :bar }
+ # second arg is optional
+ model.store_with('Base') {|a| a.block_arg = :bar }
model.storages.count.should be(2)
s1, s2 = model.storages
s1.arg1.should be(model)
s1.arg2.should == 'foo'
s1.block_arg.should == :foo
s2.arg1.should be(model)
- s2.arg2.should == 'bar'
+ s2.arg2.should be_nil
s2.block_arg.should == :bar
end
end
@@ -188,20 +189,23 @@ def using_fake(const, replacement)
describe '#sync_with' do
it 'should add syncers' do
- using_fake('Syncer', Fake::NoArg) do
- model.sync_with('Base') {|a| a.block_arg = :foo }
+ using_fake('Syncer', Fake::OneArg) do
+ model.sync_with('Base', 'foo') {|a| a.block_arg = :foo }
+ # second arg is optional
model.sync_with('Base') {|a| a.block_arg = :bar }
model.syncers.count.should be(2)
s1, s2 = model.syncers
+ s1.arg1.should == 'foo'
s1.block_arg.should == :foo
+ s2.arg1.should be_nil
s2.block_arg.should == :bar
end
end
it 'should accept a nested class name' do
using_fake('Syncer', Fake) do
- model.sync_with('NoArg::Base')
- model.syncers.first.should be_an_instance_of Fake::NoArg::Base
+ model.sync_with('OneArg::Base')
+ model.syncers.first.should be_an_instance_of Fake::OneArg::Base
end
end
View
2  spec/storage/base_spec.rb
@@ -70,8 +70,8 @@
describe '#storage_name' do
context 'when given a storage_id' do
- before { storage.storage_id = 'storage id' }
it 'should return a log-friendly name with the storage_id' do
+ storage = Backup::Storage::Base.new(model, 'storage id')
storage.send(:storage_name).should == 'Storage::Base (storage id)'
end
end
View
26 spec/syncer/cloud/base_spec.rb
@@ -53,14 +53,6 @@
before do
syncer.stubs(:repository_object).returns(:a_repository_object)
-
- Backup::Logger.expects(:info).with(
- "Syncer::Cloud::Base started the syncing process:\n" +
- "\s\sConcurrency: false Level: 2"
- )
- Backup::Logger.expects(:info).with(
- 'Syncer::Cloud::Base Syncing Complete!'
- )
end
it 'should sync each directory' do
@@ -103,6 +95,24 @@
syncer.perform!
end
+
+ describe 'logging messages' do
+ it 'logs started/finished messages' do
+ Backup::Logger.expects(:info).with('Syncer::Cloud::Base Started...')
+ Backup::Logger.expects(:info).with("\s\sConcurrency: false Level: 2")
+ Backup::Logger.expects(:info).with('Syncer::Cloud::Base Finished!')
+ syncer.perform!
+ end
+
+ it 'logs messages using optional syncer_id' do
+ syncer = Backup::Syncer::Cloud::Base.new('My Syncer')
+
+ Backup::Logger.expects(:info).with('Syncer::Cloud::Base (My Syncer) Started...')
+ Backup::Logger.expects(:info).with("\s\sConcurrency: false Level: 2")
+ Backup::Logger.expects(:info).with('Syncer::Cloud::Base (My Syncer) Finished!')
+ syncer.perform!
+ end
+ end
end # describe '#perform'
describe 'Cloud::Base::SyncContext' do
View
18 spec/syncer/rsync/local_spec.rb
@@ -119,6 +119,24 @@ module Backup
syncer.perform!
end
+ describe 'logging messages' do
+ it 'logs started/finished messages' do
+ syncer = Syncer::RSync::Local.new
+
+ Logger.expects(:info).with('Syncer::RSync::Local Started...')
+ Logger.expects(:info).with('Syncer::RSync::Local Finished!')
+ syncer.perform!
+ end
+
+ it 'logs messages using optional syncer_id' do
+ syncer = Syncer::RSync::Local.new('My Syncer')
+
+ Logger.expects(:info).with('Syncer::RSync::Local (My Syncer) Started...')
+ Logger.expects(:info).with('Syncer::RSync::Local (My Syncer) Finished!')
+ syncer.perform!
+ end
+ end
+
end # describe '#perform!'
describe 'deprecations' do
View
17 spec/syncer/rsync/pull_spec.rb
@@ -106,6 +106,23 @@ module Backup
end
end # describe 'password handling'
+ describe 'logging messages' do
+ it 'logs started/finished messages' do
+ syncer = Syncer::RSync::Pull.new
+
+ Logger.expects(:info).with('Syncer::RSync::Pull Started...')
+ Logger.expects(:info).with('Syncer::RSync::Pull Finished!')
+ syncer.perform!
+ end
+
+ it 'logs messages using optional syncer_id' do
+ syncer = Syncer::RSync::Pull.new('My Syncer')
+
+ Logger.expects(:info).with('Syncer::RSync::Pull (My Syncer) Started...')
+ Logger.expects(:info).with('Syncer::RSync::Pull (My Syncer) Finished!')
+ syncer.perform!
+ end
+ end
end # describe '#perform!'
# same deprecations as RSync::Push
View
22 spec/syncer/rsync/push_spec.rb
@@ -15,7 +15,7 @@ module Backup
after { Syncer::RSync::Push.clear_defaults! }
it 'should use the values given' do
- syncer = Syncer::RSync::Push.new do |rsync|
+ syncer = Syncer::RSync::Push.new('my syncer') do |rsync|
rsync.mode = :valid_mode
rsync.host = '123.45.678.90'
rsync.port = 123
@@ -35,6 +35,7 @@ module Backup
end
end
+ expect( syncer.syncer_id ).to eq 'my syncer'
expect( syncer.mode ).to eq :valid_mode
expect( syncer.host ).to eq '123.45.678.90'
expect( syncer.port ).to be 123
@@ -53,6 +54,7 @@ module Backup
it 'should use default values if none are given' do
syncer = Syncer::RSync::Push.new
+ expect( syncer.syncer_id ).to be_nil
expect( syncer.mode ).to eq :ssh
expect( syncer.host ).to be_nil
expect( syncer.port ).to be 22
@@ -625,6 +627,24 @@ module Backup
end
end # describe 'dest_path creation'
+ describe 'logging messages' do
+ it 'logs started/finished messages' do
+ syncer = Syncer::RSync::Push.new
+
+ Logger.expects(:info).with('Syncer::RSync::Push Started...')
+ Logger.expects(:info).with('Syncer::RSync::Push Finished!')
+ syncer.perform!
+ end
+
+ it 'logs messages using optional syncer_id' do
+ syncer = Syncer::RSync::Push.new('My Syncer')
+
+ Logger.expects(:info).with('Syncer::RSync::Push (My Syncer) Started...')
+ Logger.expects(:info).with('Syncer::RSync::Push (My Syncer) Finished!')
+ syncer.perform!
+ end
+ end
+
end # describe '#perform!'
describe 'deprecations' do
Please sign in to comment.
Something went wrong with that request. Please try again.