Sha256: e5d6160931b6d2a15ffb75edad23842a27f9d7362f0e382a336bff92dadc028e

Contents?: true

Size: 1.14 KB

Versions: 1

Compression:

Stored size: 1.14 KB

Contents

module Adminix
  module Entities
    class Job
      attr_accessor :id, :script
      attr_reader :output, :status, :success

      STATUS_IN_QUEUE = 'in_queue'.freeze
      STATUS_COMPLETED = 'completed'.freeze

      def initialize(id:, script:)
        @id = id
        @script = script
        @status = STATUS_IN_QUEUE
        @created_at = Time.now
        @success = false

        filter_script
      end

      def perform
        @output = `/bin/bash #{Adminix.config.scripts[:run_script]} #{script}`
        @success = $?.success?
        @status = STATUS_COMPLETED
        @completed_at = Time.now
      end

      def in_queue?
        @status == STATUS_IN_QUEUE
      end

      def completed?
        @status == STATUS_COMPLETED
      end

      def filter_script
        ['sudo', 'shutdown', 'kill'].each do |word|
          @script = @script.gsub('word', '')
        end
      end

      def to_api
        {
          id: @id,
          status: @status,
          script: @script,
          success: @success,
          output: @output,
          created_at: @created_at,
          completed_at: @completed_at
        }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
adminix-0.2 lib/adminix/entities/job.rb