Class: Auth::Transaction::EventHolder

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
app/models/auth/transaction/event_holder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#abort_functionObject

Returns the value of attribute abort_function.



4
5
6
# File 'app/models/auth/transaction/event_holder.rb', line 4

def abort_function
  @abort_function
end

#events_ignored_since_already_completedObject

Returns the value of attribute events_ignored_since_already_completed.



6
7
8
# File 'app/models/auth/transaction/event_holder.rb', line 6

def events_ignored_since_already_completed
  @events_ignored_since_already_completed
end

Instance Method Details

#before_commit_new_events(latest_event_holder, events, ev) ⇒ Object



157
158
159
160
161
# File 'app/models/auth/transaction/event_holder.rb', line 157

def before_commit_new_events(latest_event_holder,events,ev)



end

#before_mark_event_as_processing(ev) ⇒ Object



371
372
373
374
375
# File 'app/models/auth/transaction/event_holder.rb', line 371

def before_mark_event_as_processing(ev)

	## self is available 

end

#before_mark_existing_event_as_complete(latest_event_holder, events, ev) ⇒ Object



234
235
236
237
238
# File 'app/models/auth/transaction/event_holder.rb', line 234

def before_mark_existing_event_as_complete(latest_event_holder,events,ev)



end

#commit_new_events(latest_event_holder, events, ev) ⇒ Auth::Transaction::EventHolder

will commit the new events generated by this event, only if the present event is the last event in the events array. to ensure that it is the last event, we first check if this is the event at events.size - 1, and that the index events.size does not exist. if these conditions are satisfied, then push all the new events.

Returns:



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/auth/transaction/event_holder.rb', line 170

def commit_new_events(latest_event_holder,events,ev)

	before_commit_new_events(latest_event_holder,events,ev)

	return latest_event_holder if events.empty?

	puts "came to commit new events."

	events_size = latest_event_holder.events.size

	puts "events size is: #{events_size}"

	qr = {
		"$and" => [
			{
				"_id" => BSON::ObjectId(self.id.to_s)
			},
			{
				"events.#{events_size - 1}._id" => BSON::ObjectId(ev.id.to_s)
			},
			{
				"events.#{events_size}" => {
					"$exists" => false
				}
			}
		]
	}	

	puts "query results is:"

	puts Auth::Transaction::EventHolder.where(qr).count

	## if the last event is this event -> go and push all the new events, where the last event id is this.
	doc_after_update = Auth::Transaction::EventHolder.where(qr).find_one_and_update(
		{
			## add to set all the events.
			## set the event status as completed.
			"$push" => {
				"events" => {
					"$each" => events.map{|c| c = c.attributes}
				}
			}
		},
		{
			:return_document => :after
		}
	)

	return doc_after_update

=begin
	if doc_after_update
		puts "Returning doc after update."
		return doc_after_update
	else
		puts "no doc after update."
		## only in case someone else did not update it, we need to call an error and abort.
		## so suppose someone else updated it?
		latest_event_holder = Auth::Transaction::EventHolder.find(self.id)
		return new_events_already_committed(latest_event_holder,ev)
	end
=end
end

#commit_new_events_and_mark_existing_event_as_complete(events, ev) ⇒ Object

@return event_holder if all operations succeed. nil : if there is any failure.



311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'app/models/auth/transaction/event_holder.rb', line 311

def commit_new_events_and_mark_existing_event_as_complete(events,ev)

	puts "came to commit new events and mark existing event as complete."

	## get the latest event_holder
	latest_event_holder = Auth::Transaction::EventHolder.find(self.id)

	puts "latest event holder"

	puts latest_event_holder

	if new_events_already_committed(latest_event_holder,ev)
		return mark_existing_event_as_complete(latest_event_holder,events,ev)
	else
		puts "new events not already committed."
		if latest_event_holder = commit_new_events(latest_event_holder,events,ev)
			return mark_existing_event_as_complete(latest_event_holder,events,ev)
		else
			return nil
		end
	end

end

#event_marked_complete_by_other_process?(ev) ⇒ Boolean

Returns:

  • (Boolean)


335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'app/models/auth/transaction/event_holder.rb', line 335

def event_marked_complete_by_other_process?(ev)
	## find a transaction event_holder with this id, where the event id is this events id, and the status of that event is completed.
	results = Auth::Transaction::EventHolder.collection.aggregate([
		{
			"$match" => {
				"_id" => BSON::ObjectId(self.id.to_s)
			}
		},
		{
			"$unwind" => {
				"path" => "$events"
			}
		},
		{
			"$unwind" => {
				"path" => "$events.statuses"
			}
		},
		{
			"$match" => {
				"$and" => [
					{
						"events.statuses._id" => BSON::ObjectId(ev.id.to_s)
					},
					{
						"events.statuses.condition" => "COMPLETED"
					}
				]
			}
		}
	])

	return true if results.count == 1

end

#eventsObject

it will have an array of embedded event objects. these objects will convey the last event that took place. each event object will have its own attributes and data.



11
# File 'app/models/auth/transaction/event_holder.rb', line 11

embeds_many :events, :class_name => "Auth::Transaction::Event"

#get_eventsObject



461
462
463
# File 'app/models/auth/transaction/event_holder.rb', line 461

def get_events
	Auth::Transaction::EventHolder.find(self.id).events
end

#mark_event_as_processing(ev) ⇒ Object

@param @return, the updated event.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# File 'app/models/auth/transaction/event_holder.rb', line 380

def mark_event_as_processing(ev)
			
	## this is currently used only while testing.
	before_mark_event_as_processing(ev)

	puts "came to mark event as processing."
	

	statuses_query = 
	[{
	"events.#{ev.event_index}.statuses.0" => 
		{
		"$exists" => false
		}
	}]
				

	if ev.statuses.size > 0
		puts "events statuses size was more than 0."
		## want to make sure that the size  is nil.
		## and the size - 1 is processing.
		## that way you can get the last one.
		## otherwise you fail.
		## and its no longer an or query.

		statuses_query =
		[
			{
				"events.#{ev.event_index}.statuses.#{ev.statuses.size - 1}.condition" => "PROCESSING"
			},
			{
				"events.#{ev.event_index}.statuses.#{ev.statuses.size}" => {
					"$exists" => false
				}
			}
		]
	end

	
	qr = {
		"$and" => [
			{
				"_id" => BSON::ObjectId(self.id.to_s)
			},
			{
				"events.#{ev.event_index}._id" => BSON::ObjectId(ev.id.to_s)
			}
		]
	}

	qr["$and"] << statuses_query
	qr["$and"].flatten!

	puts "the query is:"
	puts qr.to_s

	query_result = Auth::Transaction::EventHolder.where(qr)

	puts "the count of the query result is:"
	puts query_result.count

	
	doc_after_update = Auth::Transaction::EventHolder.where(qr).find_one_and_update(
		{
				"$push" => {
					"events.#{ev.event_index}.statuses" => Auth::Transaction::Status.new(:condition => "PROCESSING", :created_at => Time.now, :updated_at => Time.now).attributes
				}
		},
		{
				:return_document => :after
		}
	)

	return nil unless doc_after_update
	
	puts "returning doc after update , marked it as processing."

	return doc_after_update.events[ev.event_index]

end

#mark_existing_event_as_complete(latest_event_holder, events, ev) ⇒ Object

will first check if the event is already marked as completed? if yes, then returns the latest_event_holder passed in. if no, will check that the last status of the event at the event_index is ‘processing’ , and that the next index does not exist. @return returns the doc after update.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'app/models/auth/transaction/event_holder.rb', line 246

def mark_existing_event_as_complete(latest_event_holder,events,ev)


	
		before_mark_existing_event_as_complete(latest_event_holder,events,ev)

		puts "came to mark existing event as complete."

		## otherwise mark the event at the event index as completed, where it is not completed.

		qr = {
			"$and" => [
				{
					"_id" => BSON::ObjectId(self.id.to_s)
				},
				{
					"events.#{ev.event_index}._id" => BSON::ObjectId(ev.id.to_s)
				},
				{
					"events.#{ev.event_index}.statuses.#{ev.statuses.size - 1}.condition" => "PROCESSING"
				},
				{
					"events.#{ev.event_index}.statuses.#{ev.statuses.size}" => {
						"$exists" => false
					}
				}
			]
		}					

		puts "query result is:"
		puts Auth::Transaction::EventHolder.where(qr).count

		doc_after_update = Auth::Transaction::EventHolder.where(qr).find_one_and_update(
			{
				## add to set all the events.
				## set the event status as completed.
				"$push" => {
					"events.#{ev.event_index}.statuses" => Auth::Transaction::Status.new(:condition => "COMPLETED", :created_at => Time.now, :updated_at => Time.now).attributes
				}
			},
			{
				:return_document => :after
			}
		)

		puts "doc after update is:"
		puts doc_after_update.to_s

		return doc_after_update

	

end

#new_events_already_committed(latest_event_holder, ev) ⇒ Object

@return returns the event holder if the given event is not the last event in the events_array. otherwise returns nil.



302
303
304
305
306
307
# File 'app/models/auth/transaction/event_holder.rb', line 302

def new_events_already_committed(latest_event_holder,ev)
	puts "came to new events already commited."
	return latest_event_holder if (latest_event_holder.events.last.id.to_s != ev.id.to_s)
	puts "returning nil."
	return nil
end

#processObject

take each event if its complete , move forward if its failed , return out of the function. if its processing, return out of the function. otherwise process the event if the result is nil, if the result is not nil, commit and mark as complete the given event. proceed, till end,now finally check event count, if not_equal, to start_count, means that many events, were added. exit out of function, without changing the status. otherwise, set status as 1, where the status was not 1.



31
32
33
34
35
36
37
38
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/models/auth/transaction/event_holder.rb', line 31

def process
		
	## increment the process count when?
	## everytime?
	## only when the result is abort, due to processing?
	## that also for a particular event?
=begin
	return unless doc_after_process_count_increment =  Auth::Transaction::EventHolder.where(
		{
			"_id" => BSON::ObjectId(self.id.to_s)
		}
	).find_one_and_update({
		"$inc" => {
			"process_count" => 1
		}
	},{
		:return_document => :after
	})
=end
	
	## where the status of the last event is not completed, update as failed, if the count in the returned document is greater than permitted count
	## use the returned doc for that.

	#####################################################
	##
	##
	##
	## ATTRIBUTE ACCESSORS INITIALIZED.
	##
	##
	####################################################

	self.abort_function = false

	self.events_ignored_since_already_completed  = []

	####################################################
	##
	##
	## end
	##
	####################################################


	return if status == 1

	events = get_events

	puts "events size is: #{events.size.to_s}"

	events.each_with_index {|ev,key|

		puts "processing event is :#{ev.id.to_s}"

		puts "event is:"
		puts ev.attributes.to_s

		ev.event_index = key

		if (ev._processing? || ev._failed?)
			#puts "processing or failed."
			self.abort_function = ev._processing? ? "processing:#{ev.id.to_s}" : "failed:#{ev.id.to_s}"  
			break
		end

		unless ev._completed?

			unless ev_updated = mark_event_as_processing(ev)
				self.abort_function = "could_not_mark_as_processing:#{ev.id.to_s}"
				break
			end
			
			ev_updated.event_index = key

			if events_to_commit = ev_updated.process

				unless ev_updated_after_commit = commit_new_events_and_mark_existing_event_as_complete(events_to_commit,ev_updated)
					self.abort_function = "could not commit new events or mark event as completed:#{ev_updated.id.to_s}"
					break
				end
			else
				self.abort_function = "event_processing_returned_nil:#{ev_updated.id.to_s}"
				break
			end

		else

			self.events_ignored_since_already_completed << ev

		end
	}

	return self if self.abort_function

	self.status = 1
	
	## as long as the number of events, has not changed, update the event_holder as completed for processing.

	Auth::Transaction::EventHolder.where({
		"$and" => [
			{
				"_id" => BSON::ObjectId(self.id.to_s)
			},
			{
				"status" => self.status_was
			},
			{
				"events.#{events.count}" => {
					"$exists" => false
				}
			}
		]
	}).find_one_and_update(
		{
			"$set" => {
				"status" => self.status
			}
		},
		{
			:return_document => :after
		}
	)

end

#statusObject

status, can be 0 -> processing/to_be_processed, or 1 -> completed.



15
# File 'app/models/auth/transaction/event_holder.rb', line 15

field :status, type: Integer