Class: MinerMover::Mover

Inherits:
Worker
  • Object
show all
Defined in:
lib/miner_mover/worker.rb

Instance Attribute Summary collapse

Attributes inherited from Worker

#logging, #timer, #variance

Instance Method Summary collapse

Methods inherited from Worker

#id, #log, #to_s, #varied

Constructor Details

#initialize(batch_size: 10, rate: 2, work_type: :cpu, variance: 0, logging: false, timer: nil) ⇒ Mover

Returns a new instance of Mover.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/miner_mover/worker.rb', line 105

def initialize(batch_size: 10,
               rate: 2,  # 2M ore per sec
               work_type: :cpu,
               variance: 0,
               logging: false,
               timer: nil)
  @batch_size = batch_size * Ore::BLOCK
  @rate = rate.to_f * Ore::BLOCK
  @work_type = work_type
  @batch, @batches, @ore_moved = 0, 0, 0
  super(variance: variance, logging: logging, timer: timer)
end

Instance Attribute Details

#batchObject (readonly)

Returns the value of attribute batch.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def batch
  @batch
end

#batch_sizeObject (readonly)

Returns the value of attribute batch_size.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def batch_size
  @batch_size
end

#batchesObject (readonly)

Returns the value of attribute batches.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def batches
  @batches
end

#ore_movedObject (readonly)

Returns the value of attribute ore_moved.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def ore_moved
  @ore_moved
end

#rateObject (readonly)

Returns the value of attribute rate.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def rate
  @rate
end

#work_typeObject (readonly)

Returns the value of attribute work_type.



103
104
105
# File 'lib/miner_mover/worker.rb', line 103

def work_type
  @work_type
end

Instance Method Details

#load_ore(amt) ⇒ Object



135
136
137
138
139
140
# File 'lib/miner_mover/worker.rb', line 135

def load_ore(amt)
  @batch += amt
  move_batch if @batch >= @batch_size
  log format("LOAD %s", self.status)
  @batch
end

#move(duration) ⇒ Object



142
143
144
# File 'lib/miner_mover/worker.rb', line 142

def move(duration)
  MinerMover.work(duration, @work_type)
end

#move_batchObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/miner_mover/worker.rb', line 146

def move_batch
  raise "unexpected batch: #{@batch}" if @batch <= 0
  amt = [@batch, @batch_size].min
  duration = self.varied(amt / @rate)

  log format("MOVE %s (%.2f s)", Ore.display(amt), duration)
  _, elapsed = CompSci::Timer.elapsed { self.move(duration) }
  log format("MOVD %s (%.2f s)", Ore.display(amt), elapsed)

  # accounting
  @ore_moved += amt
  @batch -= amt
  @batches += 1

  # what moved
  amt
end

#stateObject



118
119
120
121
122
123
124
# File 'lib/miner_mover/worker.rb', line 118

def state
  super.merge(work_type: @work_type,
              batch_size: @batch_size,
              batch: @batch,
              batches: @batches,
              ore_moved: @ore_moved)
end

#statusObject



126
127
128
129
130
131
132
133
# File 'lib/miner_mover/worker.rb', line 126

def status
  [format("Batch %s / %s %i%%",
          Ore.units(@batch),
          Ore.units(@batch_size),
          @batch.to_f * 100 / @batch_size),
   format("Moved %ix (%s)", @batches, Ore.units(@ore_moved)),
  ].join(' | ')
end