Sha256: ee2f5b761e0f42c5272994de3e3ad4a2f27ad94fdd94309ab80e7901f6d854c5

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

require_relative '../Notification'

module Snapsync
  # 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`
  class Task

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

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

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

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

    # @param [Task, nil] parent
    def initialize(parent: nil)
      @parent = parent
    end

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


    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

    def _task_name
      raise NotImplementedError
    end

    def _run
      raise NotImplementedError
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
snapsync-0.5.0 lib/snapsync/tasks/Task.rb