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: nil, include: [], exclude: []) ⇒ Task

Returns a new instance of Task.



569
570
571
572
573
574
575
576
577
578
579
580
# File 'lib/bitferry.rb', line 569

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

end

Instance Attribute Details

#excludeObject (readonly)

Returns the value of attribute exclude.



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

def exclude
  @exclude
end

#generationObject (readonly)

Returns the value of attribute generation.



528
529
530
# File 'lib/bitferry.rb', line 528

def generation
  @generation
end

#includeObject (readonly)

Returns the value of attribute include.



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

def include
  @include
end

#modifiedObject (readonly)

Returns the value of attribute modified.



531
532
533
# File 'lib/bitferry.rb', line 531

def modified
  @modified
end

#tagObject (readonly)

Returns the value of attribute tag.



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

def tag
  @tag
end

Class Method Details

.[](tag) ⇒ Object



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

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

.delete(*tags) ⇒ Object



554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/bitferry.rb', line 554

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



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

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

.liveObject



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

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



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

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



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

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



540
541
542
543
544
# File 'lib/bitferry.rb', line 540

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

.register(task) ⇒ Object



666
667
668
669
670
671
672
673
674
675
# File 'lib/bitferry.rb', line 666

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



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

def self.registered = @@registry.values

.resetObject



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

def self.reset = @@registry = {}

.restore(hash) ⇒ Object



547
548
549
550
551
# File 'lib/bitferry.rb', line 547

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

.staleObject



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

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

Instance Method Details

#commitObject



625
626
627
628
629
630
# File 'lib/bitferry.rb', line 625

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

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



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

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

#deleteObject



618
619
620
621
622
# File 'lib/bitferry.rb', line 618

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

#externalizeObject



602
603
604
605
606
607
608
609
# File 'lib/bitferry.rb', line 602

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

#live?Boolean

Returns:

  • (Boolean)


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

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

#process_optionsObject

As a mandatory option it should never be nil



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

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

#restore(hash) ⇒ Object



590
591
592
593
594
595
# File 'lib/bitferry.rb', line 590

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



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

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

#show_filtersObject



633
634
635
636
637
638
# File 'lib/bitferry.rb', line 633

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

#touchObject



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

def touch = @modified = DateTime.now