lib/openwfe/expool/wfidgen.rb in openwferu-0.9.7 vs lib/openwfe/expool/wfidgen.rb in openwferu-0.9.8
- old
+ new
@@ -63,17 +63,26 @@
@last_fn = OpenWFE::get_work_directory + '/wfidgen.last'
load_last()
- @last_f = File.open(@last_fn, "w")
+ begin
+ @last_f = File.open(@last_fn, "w+")
+ rescue Exception => e
+ lwarn do
+ "new() failed to open #{@last_fn}, continuing anyway...\n"+
+ OpenWFE::exception_to_s(e)
+ end
+ end
end
#
# Returns a new workflow instance id
#
- def generate
+ # The launchitem parameter is not used by this generator.
+ #
+ def generate (launchitem=nil)
wfid = nil
@mutex.synchronize do
wfid = now
wfid = @last + 1 if wfid <= @last
@last = wfid
@@ -113,31 +122,32 @@
# Stops this service.
# In this particular implementation, makes sure the "wfidgen.last"
# file is closed.
#
def stop
- @last_f.close
+ @last_f.close if @last_f
end
protected
def now
wfid = Time.now.to_f * 1000 * 10
wfid.to_i
end
def save_last
+ return unless @last_f
@last_f.pos = 0
@last_f.puts @last
end
def load_last
@mutex.synchronize do
if File.exist?(@last_fn)
begin
- s = File.open(@last_fn, "r+") do |f|
+ s = File.open(@last_fn, "r") do |f|
f.readline
end
@last = Integer(s)
#puts @last
rescue Exception => e
@@ -214,15 +224,48 @@
Time.utc(year, month, day, hour, min, sec, usec)
end
def self.from_time (t)
- to_string (t.to_f * 10 * 1000).to_i
+ to_string(t.to_f * 10 * 1000).to_i
end
end
#
+ # This wfid generator returns as wfid the value found in a given
+ # field of the launchitem (if any).
+ #
+ # If there is no launchitem or no field, a Kotoba wfid is returned.
+ #
+ # This generator is useful for engines that have to use workflow
+ # instance ids generated by other systems.
+ #
+ class FieldWfidGenerator < KotobaWfidGenerator
+
+ def initialize (service_name, application_context, field_name)
+
+ super(service_name, application_context)
+
+ @field_name = field_name
+ end
+
+ def generate (launchitem=nil)
+
+ return super unless launchitem
+
+ wfid = launchitem.attributes[@field_name]
+
+ return wfid.to_s if wfid
+
+ super
+ #
+ # if the field is not present in the launchitem, will
+ # return a Kotoba wfid
+ end
+ end
+
+ #
# A wfid generator that uses any underlying "uuidgen" command it might
# find.
# By default, it favours "uuidgen -t".
#
# You can specifying a command by passing a :uuid_command param in the
@@ -261,10 +304,12 @@
end
#
# Generates a brand new UUID
#
- def generate
+ # The launchitem parameter is not used by this generator.
+ #
+ def generate (launchitem=nil)
return `#{@command}`.chomp
end
#
# Is a simple call to OpenWFE::split_wfid()