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) ⇒ Task

Returns a new instance of Task.



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

def initialize(tag: Bitferry.tag, modified: DateTime.now)
  @tag = tag
  @generation = 0
  @modified = modified
  # FIXME handle process_options at this level
end

Instance Attribute Details

#generationObject (readonly)

Returns the value of attribute generation.



496
497
498
# File 'lib/bitferry.rb', line 496

def generation
  @generation
end

#modifiedObject (readonly)

Returns the value of attribute modified.



499
500
501
# File 'lib/bitferry.rb', line 499

def modified
  @modified
end

#tagObject (readonly)

Returns the value of attribute tag.



493
494
495
# File 'lib/bitferry.rb', line 493

def tag
  @tag
end

Class Method Details

.[](tag) ⇒ Object



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

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

.delete(*tags) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/bitferry.rb', line 519

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



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

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

.liveObject



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

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



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

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



596
597
598
599
600
601
# File 'lib/bitferry.rb', line 596

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



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

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

.register(task) ⇒ Object

TODO settle on task with the latest timestamp



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

def self.register(task) = @@registry[task.tag] = task # TODO settle on task with the latest timestamp

.registeredObject



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

def self.registered = @@registry.values

.resetObject



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

def self.reset = @@registry = {}

.restore(hash) ⇒ Object



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

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

.staleObject



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

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

Instance Method Details

#commitObject



580
581
582
583
584
585
# File 'lib/bitferry.rb', line 580

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

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



542
543
544
545
546
# File 'lib/bitferry.rb', line 542

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

#deleteObject



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

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

#externalizeObject



559
560
561
562
563
564
# File 'lib/bitferry.rb', line 559

def externalize
  {
    task: tag,
    modified: @modified
  }.compact
end

#live?Boolean

Returns:

  • (Boolean)


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

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

#process_optionsObject

As a mandatory option it should never be nil



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

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

#restore(hash) ⇒ Object



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

def restore(hash)
  @state = :intact
  log.info("restored task #{tag}")
end

#restore_endpoint(x) ⇒ Object

FIXME move to Endpoint#restore



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

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

#touchObject



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

def touch = @modified = DateTime.now