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

Constructor Details

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

Returns a new instance of Task.



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

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.



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

def exclude
  @exclude
end

#generationObject (readonly)

Returns the value of attribute generation.



505
506
507
# File 'lib/bitferry.rb', line 505

def generation
  @generation
end

#includeObject (readonly)

Returns the value of attribute include.



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

def include
  @include
end

#modifiedObject (readonly)

Returns the value of attribute modified.



508
509
510
# File 'lib/bitferry.rb', line 508

def modified
  @modified
end

#tagObject (readonly)

Returns the value of attribute tag.



502
503
504
# File 'lib/bitferry.rb', line 502

def tag
  @tag
end

Class Method Details

.[](tag) ⇒ Object



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

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

.delete(*tags) ⇒ Object



531
532
533
534
535
536
537
538
539
540
541
542
543
# File 'lib/bitferry.rb', line 531

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



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

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

.liveObject



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

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



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

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



622
623
624
625
626
627
# File 'lib/bitferry.rb', line 622

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



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

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

.register(task) ⇒ Object



639
640
641
642
643
644
645
646
647
648
# File 'lib/bitferry.rb', line 639

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



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

def self.registered = @@registry.values

.resetObject



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

def self.reset = @@registry = {}

.restore(hash) ⇒ Object



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

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

.staleObject



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

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

Instance Method Details

#commitObject



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

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

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



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

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

#deleteObject



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

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

#externalizeObject



575
576
577
578
579
580
581
582
# File 'lib/bitferry.rb', line 575

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

#live?Boolean

Returns:

  • (Boolean)


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

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

#process_optionsObject

As a mandatory option it should never be nil



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

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

#restore(hash) ⇒ Object



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

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



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

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

#show_filtersObject



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

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

#touchObject



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

def touch = @modified = DateTime.now