lib/ruote/storage/base.rb in ruote-2.1.11 vs lib/ruote/storage/base.rb in ruote-2.2.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2005-2010, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2005-2011, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -35,37 +35,37 @@ def context @context ||= Ruote::Context.new(self) end - def context= (c) + def context=(c) @context = c end # Attempts to delete a document, returns true if the deletion # succeeded. This is used with msgs to reserve work on them. # - def reserve (doc) + def reserve(doc) delete(doc).nil? end #-- # configurations #++ - def get_configuration (key) + def get_configuration(key) get('configurations', key) end #-- # messages #++ - def put_msg (action, options) + def put_msg(action, options) msg = prepare_msg_doc(action, options) put(msg) @@ -91,20 +91,20 @@ ).sort { |a, b| a['put_at'] <=> b['put_at'] } end - def empty? (type) + def empty?(type) (get_many(type, nil, :count => true) == 0) end #-- # expressions #++ - def find_root_expression (wfid) + def find_root_expression(wfid) get_many('expressions', wfid).sort_by { |fexp| fexp['fei']['expid'] }.select { |e| e['parent_id'].nil? @@ -118,11 +118,11 @@ # # This is a base implementation, different storage implementations may # come up with different implementations (think CouchDB, which could # provide a view for it). # - def expression_wfids (opts) + def expression_wfids(opts) wfids = ids('expressions').collect { |fei| fei.split('!').last }.uniq.sort wfids = wfids.reverse if opts[:descending] @@ -144,11 +144,11 @@ #-- # ats and crons #++ - def get_schedules (delta, now) + def get_schedules(delta, now) # TODO : bring that 'optimization' back in, # maybe every minute, if min != last_min ... #if delta < 1.0 @@ -167,11 +167,11 @@ end # Places schedule in storage. Returns the id of the 'schedule' document. # If the schedule got triggered immediately, nil is returned. # - def put_schedule (flavour, owner_fei, s, msg) + def put_schedule(flavour, owner_fei, s, msg) doc = prepare_schedule_doc(flavour, owner_fei, s, msg) return nil unless doc @@ -180,26 +180,28 @@ raise "put_schedule failed" if r != nil doc['_id'] end - def delete_schedule (schedule_id) + def delete_schedule(schedule_id) + return if schedule_id.nil? + s = get('schedules', schedule_id) delete(s) if s end #-- # engine variables #++ - def get_engine_variable (k) + def get_engine_variable(k) get_engine_variables['variables'][k] end - def put_engine_variable (k, v) + def put_engine_variable(k, v) vars = get_engine_variables vars['variables'][k] = v put_engine_variable(k, v) unless put(vars).nil? @@ -211,11 +213,11 @@ # Copies the content of this storage into a target storage. # # Of course, the target storage may be a different implementation. # - def copy_to (target, opts={}) + def copy_to(target, opts={}) counter = 0 %w[ configurations errors expressions msgs schedules variables workitems @@ -250,11 +252,11 @@ protected # Used by put_msg # - def prepare_msg_doc (action, options) + def prepare_msg_doc(action, options) # merge! is way faster than merge (no object creation probably) @counter ||= 0 @@ -273,11 +275,11 @@ msg end # Used by put_schedule # - def prepare_schedule_doc (flavour, owner_fei, s, msg) + def prepare_schedule_doc(flavour, owner_fei, s, msg) at = if s.is_a?(Time) # at or every s elsif Ruote.is_cron_string(s) # cron Rufus::CronLine.new(s).next_time(Time.now + 1) @@ -311,11 +313,11 @@ 'type' => 'variables', '_id' => 'variables', 'variables' => {} } end # Returns all the ats whose due date arrived (now or earlier) # - def filter_schedules (schedules, now) + def filter_schedules(schedules, now) now = Ruote.time_to_utc_s(now) schedules.select { |sch| sch['at'] <= now } end @@ -331,10 +333,10 @@ # key). # # It's a class method meant to be used by the various storage # implementations. # - def self.key_match? (keys, doc) + def self.key_match?(keys, doc) _id = doc.is_a?(Hash) ? doc['_id'] : doc if keys.first.is_a?(String) keys.find { |key| _id[-key.length..-1] == key }