Module: Skynet::GuidGenerator

Included in:
MessageQueueAdapter::Mysql, Job, TaskIterator, Worker
Defined in:
lib/skynet/skynet_guid_generator.rb

Constant Summary collapse

@@pid_ctr =
0

Instance Method Summary collapse

Instance Method Details

#get_unique_id(nodb = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/skynet/skynet_guid_generator.rb', line 39

def get_unique_id(nodb=nil)

  if defined?(Skynet::CONFIG) and Skynet::CONFIG[:GUID_GENERATOR]
    Skynet::CONFIG[:GUID_GENERATOR].call
  else
    @@pid_id ||= Skynet::UniqueDBNumGenerator.pid_id

    if not  Skynet::UniqueDBNumGenerator.server_num or not @@pid_id
      raise 'SERVER_NUM or PIDID not defined, please check environment.rb for the proper code.'
    end

    Mutex.new.synchronize do
      timeprt = Time.now.to_f - 1186210800   # figure it out
      timeprt = timeprt  * (2 ** 3)
      @@pid_ctr += 1

      guid_parts = [[timeprt,30],[Skynet::UniqueDBNumGenerator.server_num,8],[@@pid_id,14],[@@pid_ctr,12]]
      
      guid = 0
      guid_parts.each do |part, bitlength|
        guid = guid << bitlength
        guid += part.to_i % (2 ** bitlength)
      end
      guid
    end
  end
end