Class: Snapsync::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/snapsync/tasks/Task.rb

Overview

Tasks extending this should implement _run, _task_name

All messages should be sent through info, warn, error All sub-tasks should be executed by task or appropriately adding parent to <Task>.new Progress (if the task implements it) should be notified by set_progress

Direct Known Subclasses

AutoSync

Instance Method Summary collapse

Constructor Details

#initialize(parent: nil) ⇒ Task

Returns a new instance of Task.

Parameters:

  • parent (Task, nil) (defaults to: nil)


40
41
42
# File 'lib/snapsync/tasks/Task.rb', line 40

def initialize(parent: nil)
  @parent = parent
end

Instance Method Details

#_runObject

Raises:

  • (NotImplementedError)


75
76
77
# File 'lib/snapsync/tasks/Task.rb', line 75

def _run
  raise NotImplementedError
end

#_task_nameObject

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/snapsync/tasks/Task.rb', line 71

def _task_name
  raise NotImplementedError
end

#error(msg) ⇒ Object



25
26
27
28
29
30
# File 'lib/snapsync/tasks/Task.rb', line 25

def error(msg)
  Snapsync.error msg
  if Snapsync.NOTIFY
    @notification.update({:body => msg, :urgency => :critical})
  end
end

#info(msg) ⇒ Object



11
12
13
14
15
16
# File 'lib/snapsync/tasks/Task.rb', line 11

def info(msg)
  Snapsync.info msg
  if Snapsync.NOTIFY
    @notification.update({:body => msg})
  end
end

#runObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/snapsync/tasks/Task.rb', line 51

def run
  if Snapsync.NOTIFY
    @notification = Notification.new({
      :app_name => "Snapsync"+(Snapsync.SYSTEMD ? ' [systemd service]' : ''),
      :body => "Starting...", :summary => "#{_task_name} Running",
      :urgency => :normal, :append => false, :transient => false, :timeout => 15.0,
      :icon_path => "/usr/share/icons/gnome/scalable/emblems/emblem-default.svg"})
  end

  begin
    _run
  rescue Exception => e
    set_status "Failed"
    error e.message
    raise e
  end

  set_status "Finished"
end

#set_progress(percent) ⇒ Object

Parameters:

  • percent (Integer)


33
34
35
36
37
# File 'lib/snapsync/tasks/Task.rb', line 33

def set_progress(percent)
  if Snapsync.NOTIFY
    @notification.update({:body => "[#{'#'*(percent / 10)}]"})
  end
end

#set_status(status) ⇒ Object



44
45
46
47
48
# File 'lib/snapsync/tasks/Task.rb', line 44

def set_status(status)
  if Snapsync.NOTIFY
    @notification.update({ :summary => "#{_task_name} #{status}" })
  end
end

#warn(msg) ⇒ Object



18
19
20
21
22
23
# File 'lib/snapsync/tasks/Task.rb', line 18

def warn(msg)
  Snapsync.warn msg
  if Snapsync.NOTIFY
    @notification.update({:body => msg, :urgency => :critical})
  end
end