Class: BatchManager::BatchStatus

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/batch_manager/batch_status.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

included

Constructor Details

#initialize(path) ⇒ BatchStatus

Returns a new instance of BatchStatus.



6
7
8
9
10
11
12
13
# File 'lib/batch_manager/batch_status.rb', line 6

def initialize(path)
  @name = batch_name(path)
  File.open path do |f|
    f.each_line do |line|
      parse_batch_content line
    end
  end
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/batch_manager/batch_status.rb', line 4

def created_at
  @created_at
end

#group_nameObject

Returns the value of attribute group_name.



4
5
6
# File 'lib/batch_manager/batch_status.rb', line 4

def group_name
  @group_name
end

#managedObject

Returns the value of attribute managed.



4
5
6
# File 'lib/batch_manager/batch_status.rb', line 4

def managed
  @managed
end

#nameObject

Returns the value of attribute name.



4
5
6
# File 'lib/batch_manager/batch_status.rb', line 4

def name
  @name
end

#times_limitObject

Returns the value of attribute times_limit.



4
5
6
# File 'lib/batch_manager/batch_status.rb', line 4

def times_limit
  @times_limit
end

Instance Method Details

#can_run?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/batch_manager/batch_status.rb', line 37

def can_run?
  @times_limit.to_i <= 0 || @times_limit > schema_batch.try(:ran_times).to_i
end

#managed?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/batch_manager/batch_status.rb', line 33

def managed?
  @managed
end

#parse_batch_content(line) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/batch_manager/batch_status.rb', line 41

def parse_batch_content(line)
  if line.start_with?("#")
    @managed = true if line.include?(BatchManager.signal)
    if managed?
      if line.include?("=times_limit:")
        @times_limit = line.sub(/#\s*=times_limit:/, "").strip.to_i
      elsif line.include?("=created_at:")
        @created_at = Time.parse(line.sub(/#\s*=created_at:/, "").strip)
      elsif line.include?("=group_name:")
        @group_name = line.sub(/#\s*=group_name:/, "").strip
      end
    end
  end
end

#schema_batchObject



15
16
17
# File 'lib/batch_manager/batch_status.rb', line 15

def schema_batch
  BatchManager::SchemaBatch.find_by_name(@name) if @name
end

#update_schemaObject



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/batch_manager/batch_status.rb', line 19

def update_schema
  if managed?
    if schema_batch
      schema_batch.increment!(:ran_times)
    else
      BatchManager::SchemaBatch.create! do |s|
        s.name = @name
        s.ran_times = 1
        s.last_ran_at = Time.now
      end
    end
  end
end