Class: ActiveValidation::Internal::Observers::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/active_validation/internal/observers/manifest.rb

Constant Summary collapse

DISABLED_VERIFIER =
:verifier_is_not_enabled
RECENT_FAILURE =
:it_was_failed_too_recently
NOT_FOUND =
:not_found
ALREADY_INSTALLED =
:already_installed
INSTALLED =
:installed
UNINSTALLED =
:uninstalled

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(verifier) ⇒ Manifest

Returns a new instance of Manifest.



18
19
20
21
22
23
# File 'lib/active_validation/internal/observers/manifest.rb', line 18

def initialize(verifier)
  @verifier = verifier
  @installed_manifests = Concurrent::Set.new
  @lock = Concurrent::ReadWriteLock.new
  @last_failed_attempt_time = nil
end

Instance Attribute Details

#installed_manifestsObject (readonly)

Returns the value of attribute installed_manifests.



14
15
16
# File 'lib/active_validation/internal/observers/manifest.rb', line 14

def installed_manifests
  @installed_manifests
end

#last_failed_attempt_timeObject (readonly)

Returns the value of attribute last_failed_attempt_time.



14
15
16
# File 'lib/active_validation/internal/observers/manifest.rb', line 14

def last_failed_attempt_time
  @last_failed_attempt_time
end

#verifierObject (readonly)

Returns the value of attribute verifier.



14
15
16
# File 'lib/active_validation/internal/observers/manifest.rb', line 14

def verifier
  @verifier
end

Instance Method Details

#current_manifestInternal::Manifest

We omit the error and only save the error time to prevent DB flooding, when there is no any manifest.

We do not know all possible errors so we can not be more specific here.

Returns:

  • (Internal::Manifest)


67
68
69
70
71
72
# File 'lib/active_validation/internal/observers/manifest.rb', line 67

def current_manifest
  verifier.current_manifest.to_internal_manifest
rescue StandardError => _e
  @last_failed_attempt_time = Time.now
  nil
end

#install(manifest_id: nil) ⇒ Object

Install manifest and store the result. Load priority:

* `manifest_id` from the arguments
* `verifier.manifest` - defined by user in verifier block
* `current_manifest` - the latest manifest with current version

The process will be skipped if:

- `verifier` not `enabled?`
- The last failed attempt was too recent.

Returns:

  • Symbol



35
36
37
38
39
40
41
42
43
44
# File 'lib/active_validation/internal/observers/manifest.rb', line 35

def install(manifest_id: nil)
  return DISABLED_VERIFIER unless enabled?
  return RECENT_FAILURE if attempt_to_was_failed_recently?

  found = lock.with_read_lock { find(manifest_id: manifest_id) } || current_manifest
  return  NOT_FOUND unless found
  return ALREADY_INSTALLED if found.installed?

  install! internal_manifest: found
end

#install!(internal_manifest:) ⇒ Object

The actual install process

Returns:

  • Symbol



84
85
86
87
88
89
90
91
92
# File 'lib/active_validation/internal/observers/manifest.rb', line 84

def install!(internal_manifest:)
  lock.with_write_lock do
    return ALREADY_INSTALLED if find(manifest_id: internal_manifest.id)&.installed?

    internal_manifest.install
    installed_manifests << internal_manifest
  end
  INSTALLED
end

#reset_last_failed_attempt_timeObject

We need this method for been able to restore manifest lookup

Returns:

  • nil



77
78
79
# File 'lib/active_validation/internal/observers/manifest.rb', line 77

def reset_last_failed_attempt_time
  @last_failed_attempt_time = nil
end

#uninstall(manifest_id:) ⇒ Object

Uninstall the manifest.

Returns:

  • Symbol



49
50
51
52
53
54
55
56
57
58
# File 'lib/active_validation/internal/observers/manifest.rb', line 49

def uninstall(manifest_id:)
  lock.with_write_lock do
    internal_manifest = find(manifest_id: manifest_id)
    return NOT_FOUND unless internal_manifest&.installed?

    internal_manifest.uninstall
    installed_manifests.delete internal_manifest
  end
  UNINSTALLED
end