mumuki-domain 9.3.0 → 9.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b78b493d9f7e6314a746e22d17fe33c140dada6e77a9af84da850098180a1677
4
- data.tar.gz: 9ed9017804d844c8a24540dcdf5c32d4ad78d8a28b186e3df1dbd83b845ff5cd
3
+ metadata.gz: ddd0081f4ed25d268fe17c327be9b5b343a21b2491eb737db4ee837fb16eead8
4
+ data.tar.gz: 7e34398bd300598d749d628af5ae31a49985ff62f8dda2ce2598169197cacc2d
5
5
  SHA512:
6
- metadata.gz: 84faddd371cc851bbb08155cddb140dd6a17eaa3920f73cf235950d3321f07be4dd4f44325708b582904ee4d1eac2b5c426be3afb7b82e566834d87686eec3b2
7
- data.tar.gz: 93cf6080fba52bffbe3d7fafd4a3ed0121f55accaa8b1c7950e9512a4f409b9031288b51998ab7caff22ac161fd4c0d4c9fb663f369cafb00a42afcfe6ad5732
6
+ metadata.gz: 87e8fbb20f42949df631f2696c1d10a0c40744b7f84a1c7b705e525a8f2120a1e1ce5a58231a9bb9c2faa4987bd031eba01d3eb01a3e407f7202c4469eaeda72
7
+ data.tar.gz: 9196ae6c744c474853ffb027cf86f015546539ef994f83c62f27199123e54dbda3372fbd141a22f86c349270a0d14b1e6cdf3fdda47f3476bc6cd594ceb6e742
@@ -0,0 +1,19 @@
1
+ module WithPgLock
2
+ ##
3
+ # Lock PG table, reload model and execute callback if
4
+ # criterion is met
5
+ #
6
+ def with_pg_lock(callback, criterion = proc { true })
7
+ # Some notes:
8
+ #
9
+ # * nowait is a postgre specific option and may not work with other databases
10
+ # * nowait will raise an exception if the lock can not be acquired
11
+ # * we are using a double check lock pattern to reduce lock acquisition
12
+ with_lock('for update nowait') do
13
+ reload
14
+ callback.call if criterion.call
15
+ end if criterion.call
16
+ rescue
17
+ nil
18
+ end
19
+ end
@@ -1,5 +1,6 @@
1
1
  module WithReminders
2
2
  extend ActiveSupport::Concern
3
+ include WithPgLock
3
4
 
4
5
  def build_reminder
5
6
  mailer = UserMailer.new
@@ -18,23 +19,13 @@ module WithReminders
18
19
  end
19
20
 
20
21
  # Try to send a reminder, by acquiring a database lock for update
21
- # the aproppriate record. This object can't be updated as long as
22
+ # the appropriate record. This object can't be updated as long as
22
23
  # the reminder is being sent.
23
24
  #
24
25
  # This method is aimed to be sent across multiple servers or processed concurrently
25
26
  # and still not send duplicate mails
26
27
  def try_remind_with_lock!
27
- # Some notes:
28
- #
29
- # * nowait is a postgre specific option and may not work with other databases
30
- # * nowait will raise an exception if the lock can not be acquired
31
- # * we are using a double check lock pattern to reduce lock acquisition
32
- with_lock('for update nowait') do
33
- reload
34
- remind! if should_remind?
35
- end if should_remind?
36
- rescue
37
- nil
28
+ with_pg_lock proc { remind! }, proc { should_remind? }
38
29
  end
39
30
 
40
31
  private
@@ -21,7 +21,7 @@ class ExamAuthorizationRequest < ApplicationRecord
21
21
  def icon
22
22
  case status.to_sym
23
23
  when :pending
24
- { class: 'info-circle', type: 'info' }
24
+ { class: 'hourglass', type: 'info' }
25
25
  when :approved
26
26
  { class: 'check-circle', type: 'success' }
27
27
  when :rejected
@@ -1,4 +1,5 @@
1
1
  class ExamRegistration < ApplicationRecord
2
+ include WithPgLock
2
3
  include WithTimedEnablement
3
4
  include TerminalNavigation
4
5
 
@@ -16,6 +17,8 @@ class ExamRegistration < ApplicationRecord
16
17
 
17
18
  alias_attribute :name, :description
18
19
 
20
+ scope :should_process, -> { where(processed: false).where(arel_table[:end_time].lt(Time.now)) }
21
+
19
22
  def authorization_criterion
20
23
  @authorization_criterion ||= ExamRegistration::AuthorizationCriterion.parse(authorization_criterion_type, authorization_criterion_value)
21
24
  end
@@ -40,11 +43,21 @@ class ExamRegistration < ApplicationRecord
40
43
  registrees.where.not(id: Notification.notified_users_ids_for(self, self.organization))
41
44
  end
42
45
 
46
+ # Try to process authorization request, by acquiring a database lock for update
47
+ # the appropriate record.
48
+ #
49
+ # This method is aimed to be sent across multiple servers or processed concurrently
50
+ # and still not send duplicate mails
43
51
  def process_requests!
52
+ with_pg_lock proc { process_authorization_requests! }, proc { !processed? }
53
+ end
54
+
55
+ def process_authorization_requests!
44
56
  authorization_requests.each do |it|
45
57
  process_request! it
46
58
  it.try_authorize!
47
59
  end
60
+ update! processed: true
48
61
  end
49
62
 
50
63
  def authorization_request_for(user)
@@ -0,0 +1,5 @@
1
+ class AddProcessedFlagToExamRegistration < ActiveRecord::Migration[5.1]
2
+ def change
3
+ add_column :exam_registrations, :processed, :boolean, default: false
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Mumuki
2
2
  module Domain
3
- VERSION = '9.3.0'
3
+ VERSION = '9.4.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mumuki-domain
3
3
  version: !ruby/object:Gem::Version
4
- version: 9.3.0
4
+ version: 9.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franco Leonardo Bulgarelli
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-13 00:00:00.000000000 Z
11
+ date: 2021-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -291,6 +291,7 @@ files:
291
291
  - app/models/concerns/with_name.rb
292
292
  - app/models/concerns/with_notifications.rb
293
293
  - app/models/concerns/with_number.rb
294
+ - app/models/concerns/with_pg_lock.rb
294
295
  - app/models/concerns/with_preferences.rb
295
296
  - app/models/concerns/with_profile.rb
296
297
  - app/models/concerns/with_progress.rb
@@ -666,6 +667,7 @@ files:
666
667
  - db/migrate/20210318194559_add_dates_to_certificate_program.rb
667
668
  - db/migrate/20210318195238_rename_certificate_dates.rb
668
669
  - db/migrate/20210330175706_create_exam_registration_user_join_table.rb
670
+ - db/migrate/20210512200453_add_processed_flag_to_exam_registration.rb
669
671
  - lib/mumuki/domain.rb
670
672
  - lib/mumuki/domain/area.rb
671
673
  - lib/mumuki/domain/engine.rb