Class: Updater::ORM::DataMapper
- Inherits:
-
Object
- Object
- Updater::ORM::DataMapper
- Includes:
- DataMapper::Resource
- Defined in:
- lib/updater/orm/datamapper.rb
Constant Summary collapse
- FINDER =
:get
- ID =
:id
Class Method Summary collapse
- .after_fork ⇒ Object
-
.before_fork ⇒ Object
For pooled connections it is necessary to empty the pool of the parents connections so that they do not comtiminate the child pool.
- .clear_all ⇒ Object
- .clear_locks(worker) ⇒ Object
- .current ⇒ Object
- .current_load ⇒ Object
- .delayed ⇒ Object
- .for(mytarget, myfinder, myfinder_args, myname = nil) ⇒ Object
- .future(start, finish) ⇒ Object
-
.lock_next(worker) ⇒ Object
Returns the Locked Job or nil if no jobs were availible.
- .queue_time ⇒ Object
-
.setup(options) ⇒ Object
For the server only, setup the connection to the database.
Instance Method Summary collapse
-
#lock(worker) ⇒ Object
attempt to lock this record for the worker.
-
#locked? ⇒ Boolean
Useful, but not in API.
-
#locked_by ⇒ Object
Useful, but not in API.
Class Method Details
.after_fork ⇒ Object
169 170 171 |
# File 'lib/updater/orm/datamapper.rb', line 169 def after_fork end |
.before_fork ⇒ Object
For pooled connections it is necessary to empty the pool of the parents connections so that they do not comtiminate the child pool. Note that while Datamapper is thread safe, it is not safe accross a process fork.
162 163 164 165 166 167 |
# File 'lib/updater/orm/datamapper.rb', line 162 def before_fork return unless (defined? ::DataObjects::Pooling) return if ::DataMapper.repository.adapter.to_s =~ /Sqlite3Adapter/ ::DataMapper.logger.debug "+-+-+-+-+ Cleaning up connection pool (#{::DataObjects::Pooling.pools.length}) +-+-+-+-+" ::DataObjects::Pooling.pools.each {|p| p.dispose} end |
.clear_all ⇒ Object
139 140 141 142 |
# File 'lib/updater/orm/datamapper.rb', line 139 def clear_all all.destroy! DMChained.all.destroy! end |
.clear_locks(worker) ⇒ Object
135 136 137 |
# File 'lib/updater/orm/datamapper.rb', line 135 def clear_locks(worker) all(:lock_name=>worker.name).update(:lock_name=>nil) end |
.current ⇒ Object
94 95 96 |
# File 'lib/updater/orm/datamapper.rb', line 94 def current all(:time.lte=>tnow, :lock_name=>nil) end |
.current_load ⇒ Object
98 |
# File 'lib/updater/orm/datamapper.rb', line 98 def current_load;current.count;end |
.delayed ⇒ Object
100 101 102 |
# File 'lib/updater/orm/datamapper.rb', line 100 def delayed all(:time.gt=>tnow).count end |
.for(mytarget, myfinder, myfinder_args, myname = nil) ⇒ Object
144 145 146 147 148 149 150 151 152 |
# File 'lib/updater/orm/datamapper.rb', line 144 def for(mytarget, myfinder, myfinder_args, myname=nil) search = all( :target=>mytarget, :finder=>myfinder, :finder_args=>myfinder_args, :lock_name=>nil ) myname ? search.all(:name=>myname ) : search end |
.future(start, finish) ⇒ Object
104 105 106 |
# File 'lib/updater/orm/datamapper.rb', line 104 def future(start, finish) all(:time.gt=>start+tnow,:time.lt=>finish+tnow).count end |
.lock_next(worker) ⇒ Object
Returns the Locked Job or nil if no jobs were availible.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/updater/orm/datamapper.rb', line 116 def lock_next(worker) updates = worker_set unless updates.empty? #concept copied form delayed_job. If there are a number of #different processes working on the queue, the niave approch #would result in every instance trying to lock the same record. #by shuffleing our results we greatly reduce the chances that #multilpe workers try to lock the same process updates = updates.to_a.sort_by{rand()} updates.each do |u| return u if u.lock(worker) end return nil end rescue DataObjects::ConnectionError sleep 0.1 retry end |
.queue_time ⇒ Object
108 109 110 111 112 113 |
# File 'lib/updater/orm/datamapper.rb', line 108 def queue_time nxt = self.first(:time.not=>nil,:lock_name=>nil, :order=>[:time.asc]) return nil unless nxt return 0 if nxt.time <= tnow return nxt.time - tnow end |
.setup(options) ⇒ Object
For the server only, setup the connection to the database
155 156 157 158 |
# File 'lib/updater/orm/datamapper.rb', line 155 def setup() ::DataMapper.logger = .delete(:logger) ::DataMapper.setup(:default,) end |
Instance Method Details
#lock(worker) ⇒ Object
attempt to lock this record for the worker
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/updater/orm/datamapper.rb', line 35 def lock(worker) return true if locked? && locked_by == worker.name #all this to make sure the check and the lock are simultanious: cnt = repository.update({properties[:lock_name]=>worker.name},self.class.all(:id=>self.id,:lock_name=>nil)) if 0 != cnt @lock_name = worker.name true else worker.say( "Worker #{worker.name} Failed to aquire lock on job #{id}" ) false end end |
#locked? ⇒ Boolean
Useful, but not in API
84 85 86 |
# File 'lib/updater/orm/datamapper.rb', line 84 def locked? not @lock_name.nil? end |
#locked_by ⇒ Object
Useful, but not in API
89 90 91 |
# File 'lib/updater/orm/datamapper.rb', line 89 def locked_by @lock_name end |