class ActiveValidation::Internal::Observers::Manifest

Constants

ALREADY_INSTALLED
DISABLED_VERIFIER
INSTALLED
NOT_FOUND
RECENT_FAILURE
UNINSTALLED

Attributes

installed_manifests[R]
last_failed_attempt_time[R]
lock[R]
verifier[R]

Public Class Methods

new(verifier) click to toggle source
# 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

Public Instance Methods

current_manifest() click to toggle source

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.

@return [Internal::Manifest]

# 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) click to toggle source

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.

@return Symbol

# 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:) click to toggle source

The actual install process

@return Symbol

# 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_time() click to toggle source

We need this method for been able to restore manifest lookup

@return nil

# 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:) click to toggle source

Uninstall the manifest.

@return Symbol

# 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

Private Instance Methods

attempt_to_was_failed_recently?() click to toggle source
# File lib/active_validation/internal/observers/manifest.rb, line 106
def attempt_to_was_failed_recently?
  return unless last_failed_attempt_time

  Time.now < @last_failed_attempt_time + failed_attempt_retry_time
end
find(manifest_id: nil) click to toggle source
# File lib/active_validation/internal/observers/manifest.rb, line 98
def find(manifest_id: nil)
  return unless manifest_id

  @installed_manifests.detect do |manifest|
    manifest.id == manifest_id
  end
end