Class: Ruote::MnemoWfidGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruote/id/mnemo_wfid_generator.rb

Overview

The default wfid generator.

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ MnemoWfidGenerator

Returns a new instance of MnemoWfidGenerator


36
37
38
39
40
41
42
43
# File 'lib/ruote/id/mnemo_wfid_generator.rb', line 36

def initialize(context)

  @context = context

  @here = "#{Ruote.local_ip}!#{Process.pid}"
  @counter = 0
  #@mutex = Mutex.new
end

Instance Method Details

#generateObject


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruote/id/mnemo_wfid_generator.rb', line 45

def generate

  t = Time.now.utc
  time = t.strftime('%Y%m%d-%H%M')
  ms = t.to_f % 60.0

  #c = @mutex.synchronize { @counter = (@counter + 1) % 100_000 }
  @counter = (@counter + 1) % 100_000
    #
    # no need to worry about skipping a beat, no mutex.

  s = "#{ms}!#{Thread.current.object_id}!#{@here}!#{@counter}"
  s = Digest::MD5.hexdigest(s)

  x = Rufus::Mnemo.from_i(s[0, 6].to_i(16))
  y = Rufus::Mnemo.from_i(s[6, 6].to_i(16))

  "#{time}-#{x}-#{y}"
end