Class: Bitferry::Task

Inherits:
Object
  • Object
show all
Extended by:
Logging
Includes:
Logging
Defined in:
lib/bitferry.rb

Direct Known Subclasses

Rclone::Task, Restic::Task

Constant Summary collapse

ROUTE =
{
  copy: Rclone::Copy,
  update: Rclone::Update,
  synchronize: Rclone::Synchronize,
  equalize: Rclone::Equalize,
  backup: Restic::Backup,
  restore: Restic::Restore
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

log, log, log=

Constructor Details

#initialize(tag: Bitferry.tag, modified: DateTime.now, include: [], exclude: []) ⇒ Task

Returns a new instance of Task.



554
555
556
557
558
559
560
561
# File 'lib/bitferry.rb', line 554

def initialize(tag: Bitferry.tag, modified: DateTime.now, include: [], exclude: [])
  @tag = tag
  @generation = 0
  @include = include
  @exclude = exclude
  @modified = modified.is_a?(DateTime) ? modified : DateTime.parse(modified)
  # FIXME handle process_options at this level
end

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



519
520
521
# File 'lib/bitferry.rb', line 519

def exclude
  @exclude
end

#generationObject (readonly)

Returns the value of attribute generation.



513
514
515
# File 'lib/bitferry.rb', line 513

def generation
  @generation
end

#includeObject (readonly)

Returns the value of attribute include.



519
520
521
# File 'lib/bitferry.rb', line 519

def include
  @include
end

#modifiedObject (readonly)

Returns the value of attribute modified.



516
517
518
# File 'lib/bitferry.rb', line 516

def modified
  @modified
end

#tagObject (readonly)

Returns the value of attribute tag.



510
511
512
# File 'lib/bitferry.rb', line 510

def tag
  @tag
end

Class Method Details

.[](tag) ⇒ Object



622
# File 'lib/bitferry.rb', line 622

def self.[](tag) = @@registry[tag]

.delete(*tags) ⇒ Object



539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/bitferry.rb', line 539

def self.delete(*tags)
  process = []
  tags.each do |tag|
    case (tasks = Task.lookup(tag)).size
      when 0 then log.warn("no tasks matching (partial) tag #{tag}")
      when 1 then process += tasks
      else
        tags = tasks.collect { |v| v.tag }.join(', ')
        raise ArgumentError, "multiple tasks matching (partial) tag #{tag}: #{tags}"
    end
  end
  process.each { |task| task.delete }
end

.intactObject



658
# File 'lib/bitferry.rb', line 658

def self.intact = live.filter { |task| task.intact? }

.liveObject



641
# File 'lib/bitferry.rb', line 641

def self.live = registered.filter { |task| task.live? }

.lookup(*tags) ⇒ Object

Return list of registered tasks whose tags match at least one of specified partial tags



626
# File 'lib/bitferry.rb', line 626

def self.lookup(*tags) = match(tags, registered)

.match(tags, tasks) ⇒ Object

Return list of specified tasks whose tags match at least one of specified partial tags



630
631
632
633
634
635
# File 'lib/bitferry.rb', line 630

def self.match(tags, tasks)
  rxs = tags.collect { |x| Regexp.new(x) }
  tasks.filter do |task|
    rxs.any? { |rx| !(rx =~ task.tag).nil? }
  end
end

.new(*args, **opts) ⇒ Object



525
526
527
528
529
# File 'lib/bitferry.rb', line 525

def self.new(*args, **opts)
  task = allocate
  task.send(:create, *args, **opts)
  register(task)
end

.register(task) ⇒ Object



647
648
649
650
651
652
653
654
655
656
# File 'lib/bitferry.rb', line 647

def self.register(task)
  # Task with newer timestamp replaces already registered task, if any
  if (xtag = @@registry[task.tag]).nil?
    @@registry[task.tag] = task
  elsif xtag.modified < task.modified
    @@registry[task.tag] = task
  else
    xtag
  end
end

.registeredObject



638
# File 'lib/bitferry.rb', line 638

def self.registered = @@registry.values

.resetObject



644
# File 'lib/bitferry.rb', line 644

def self.reset = @@registry = {}

.restore(hash) ⇒ Object



532
533
534
535
536
# File 'lib/bitferry.rb', line 532

def self.restore(hash)
  task = allocate
  task.send(:restore, hash)
  register(task)
end

.staleObject



661
# File 'lib/bitferry.rb', line 661

def self.stale = live.filter { |task| !task.intact? }

Instance Method Details

#commitObject



606
607
608
609
610
611
# File 'lib/bitferry.rb', line 606

def commit
  case @state
  when :pristine then format
  when :removing then @state = nil
  end
end

#create(*args, **opts) ⇒ Object



564
565
566
567
568
# File 'lib/bitferry.rb', line 564

def create(*args, **opts)
  initialize(*args, **opts)
  @state = :pristine
  touch
end

#deleteObject



599
600
601
602
603
# File 'lib/bitferry.rb', line 599

def delete
  touch
  @state = :removing
  log.info("marked task #{tag} for removal")
end

#externalizeObject



583
584
585
586
587
588
589
590
# File 'lib/bitferry.rb', line 583

def externalize
  {
    task: tag,
    modified: modified,
    include: include.empty? ? nil : include,
    exclude: exclude.empty? ? nil : exclude
  }.compact
end

#live?Boolean

Returns:

  • (Boolean)


593
# File 'lib/bitferry.rb', line 593

def live? = !@state.nil? && @state != :removing

#process_optionsObject

As a mandatory option it should never be nil



522
# File 'lib/bitferry.rb', line 522

def process_options = @process_options.nil? ? [] : @process_options # As a mandatory option it should never be nil

#restore(hash) ⇒ Object



571
572
573
574
575
576
# File 'lib/bitferry.rb', line 571

def restore(hash)
  @include = hash.fetch(:include, [])
  @exclude = hash.fetch(:exclude, [])
  @state = :intact
  log.info("restored task #{tag}")
end

#restore_endpoint(x) ⇒ Object

FIXME move to Endpoint#restore



580
# File 'lib/bitferry.rb', line 580

def restore_endpoint(x) = Endpoint::ROUTE.fetch(x.fetch(:endpoint).intern).restore(x)

#show_filtersObject



614
615
616
617
618
619
# File 'lib/bitferry.rb', line 614

def show_filters
  xs = []
  xs << 'include: ' + include.join(',') unless include.empty?
  xs << 'exclude: ' + exclude.join(',') unless exclude.empty?
  xs.join(' ').to_s
end

#touchObject



596
# File 'lib/bitferry.rb', line 596

def touch = @modified = DateTime.now