Class: SDBTools::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/sdbtools.rb

Direct Known Subclasses

Dump, Load

Instance Method Summary collapse

Constructor Details

#initialize(status_file, logger) ⇒ Task

Returns a new instance of Task.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sdbtools.rb', line 17

def initialize(status_file, logger)
  @logger = logger
  @status_file = Pathname(status_file)
  @status = PStore.new(@status_file.to_s)
  unless @status_file.exist?
    initialize_status!(yield)
  end
  @status.transaction(false) do
    @logger.info "Initializing #{@status_file} for PID #{$$}"
    @status[$$] = {}
    @status[$$][:working_items] = []
  end
end

Instance Method Details

#failed_itemsObject



43
44
45
46
47
# File 'lib/sdbtools.rb', line 43

def failed_items
  @status.transaction(true) do |status|
    status[:failed_items]
  end
end

#finish_items!(items) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/sdbtools.rb', line 58

def finish_items!(items)
  @status.transaction(false) do |status|
    items.each do |item_name|
      status[:complete_items] << 
        status[$$][:working_items].delete(item_name)
      @logger.info("Marked item #{item_name} complete")
    end
  end
end

#incomplete_countObject



37
38
39
40
41
# File 'lib/sdbtools.rb', line 37

def incomplete_count
  @status.transaction(true) {|status|
    Array(status[:incomplete_items]).size
  }
end

#release_working_items!Object



68
69
70
71
72
73
74
75
# File 'lib/sdbtools.rb', line 68

def release_working_items!
  @logger.info("Releasing working items")
  @status.transaction(false) do |status|
    items = status[$$][:working_items]
    status[:incomplete_items].concat(items)
    status[$$][:working_items].clear
  end
end

#reportObject



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sdbtools.rb', line 77

def report
  @status.transaction(true) do |status|
    done     = status[:complete_items].size
    not_done = status[:incomplete_items].size
    failed   = status[:failed_items].size
    puts "Items (not done/done/failed): #{not_done}/#{done}/#{failed}"
    status.roots.select{|root| root.kind_of?(Integer)}.each do |root|
      pid = root
      items = status[root][:working_items].size
      puts "Process #{pid} working on #{items} items"
    end
  end
end

#reserve_items!(size) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/sdbtools.rb', line 49

def reserve_items!(size)
  @status.transaction(false) do |status|
    chunk = status[:incomplete_items].slice!(0,size)
    status[$$][:working_items].concat(chunk)
    @logger.info("Reserved #{chunk.size} items")
    chunk
  end
end

#sessionObject



31
32
33
34
35
# File 'lib/sdbtools.rb', line 31

def session
  yield
ensure
  release_working_items!
end