|
| 1 | +# rbs_inline: enabled |
| 2 | + |
| 3 | +require "digest" |
| 4 | + |
| 5 | +module SolidObjects |
| 6 | + class RedriveManager |
| 7 | + RUNNING = "running" |
| 8 | + COMPLETED = "completed" |
| 9 | + CANCELLED = "cancelled" |
| 10 | + |
| 11 | + # @rbs (scope: DeadLetterScope, filters: Hash[String, untyped], authorization_context: untyped) -> RedriveTask |
| 12 | + def start(scope:, filters:, authorization_context:) |
| 13 | + active_scope = active_scope_for(kind: scope.kind, filters:) |
| 14 | + running = Redrive.find_by(active_scope:) |
| 15 | + return task_for(running) if running |
| 16 | + |
| 17 | + task_for(open_task(scope:, filters:, active_scope:, authorization_context:)) |
| 18 | + rescue ActiveRecord::RecordNotUnique |
| 19 | + task_for(Redrive.find_by!(active_scope:)) |
| 20 | + end |
| 21 | + |
| 22 | + # @rbs (String, ?authorization_context: untyped) -> RedriveTask |
| 23 | + def find(id, authorization_context: nil) |
| 24 | + authorize!(:inspect, authorization_context:, resource_id: id) |
| 25 | + task_for(Redrive.find(id)) |
| 26 | + end |
| 27 | + |
| 28 | + # @rbs (?status: Symbol | String | nil, ?authorization_context: untyped) -> Array[RedriveTask] |
| 29 | + def all(status: nil, authorization_context: nil) |
| 30 | + authorize!(:inspect, authorization_context:) |
| 31 | + relation = Redrive.order(started_at: :desc, id: :desc) |
| 32 | + relation = relation.where(status: status.to_s) if status |
| 33 | + relation.map { |record| task_for(record) } |
| 34 | + end |
| 35 | + |
| 36 | + # @rbs (String, ?authorization_context: untyped) -> RedriveTask |
| 37 | + def cancel(id, authorization_context: nil) |
| 38 | + authorize!(:cancel, authorization_context:, resource_id: id) |
| 39 | + record = Redrive.find(id) |
| 40 | + return task_for(record) unless record.status == RUNNING |
| 41 | + |
| 42 | + close(record, status: CANCELLED) |
| 43 | + audit(record, action: "redrive.cancel") |
| 44 | + task_for(record) |
| 45 | + end |
| 46 | + |
| 47 | + # @rbs (Redrive, status: String) -> void |
| 48 | + def close(record, status:) |
| 49 | + record.update!( |
| 50 | + status:, |
| 51 | + active_scope: nil, |
| 52 | + finished_at: SolidObjects.database_adapter.database_now |
| 53 | + ) |
| 54 | + end |
| 55 | + |
| 56 | + # @rbs (Redrive, action: String) -> void |
| 57 | + def audit(record, action:) |
| 58 | + AdministrationAudit.record( |
| 59 | + action:, |
| 60 | + kind: record.kind, |
| 61 | + subject_id: record.id, |
| 62 | + filters: record.filters, |
| 63 | + actor: record.actor |
| 64 | + ) |
| 65 | + end |
| 66 | + |
| 67 | + # @rbs (Redrive) -> RedriveTask |
| 68 | + def task_for(record) |
| 69 | + RedriveTask.new( |
| 70 | + id: record.id, |
| 71 | + kind: record.kind, |
| 72 | + filters: Serialization.readonly_copy(record.filters), |
| 73 | + status: record.status, |
| 74 | + moved: record.moved, |
| 75 | + remaining: remaining_for(record), |
| 76 | + started_at: record.started_at, |
| 77 | + finished_at: record.finished_at |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + private |
| 82 | + |
| 83 | + # @rbs (scope: DeadLetterScope, filters: Hash[String, untyped], active_scope: String, authorization_context: untyped) -> Redrive |
| 84 | + def open_task(scope:, filters:, active_scope:, authorization_context:) |
| 85 | + record = Redrive.create!( |
| 86 | + id: "redrive_#{SecureRandom.uuid}", |
| 87 | + kind: scope.kind, |
| 88 | + filters:, |
| 89 | + status: RUNNING, |
| 90 | + active_scope:, |
| 91 | + moved: 0, |
| 92 | + move_limit: filters["limit"], |
| 93 | + actor: AdministrationAudit.identity(authorization_context), |
| 94 | + started_at: SolidObjects.database_adapter.database_now |
| 95 | + ) |
| 96 | + audit(record, action: "redrive.start") |
| 97 | + record |
| 98 | + end |
| 99 | + |
| 100 | + # A running task reports what is left to move rather than a stored |
| 101 | + # estimate, because rows die and are retried while it runs. |
| 102 | + # @rbs (Redrive) -> Integer |
| 103 | + def remaining_for(record) |
| 104 | + return 0 unless record.status == RUNNING |
| 105 | + |
| 106 | + matching = DeadLetterScope.for_kind(record.kind).matching(record.filters).count |
| 107 | + limit = record.move_limit |
| 108 | + return matching unless limit |
| 109 | + |
| 110 | + [ matching, limit - record.moved ].min |
| 111 | + end |
| 112 | + |
| 113 | + # @rbs (kind: String, filters: Hash[String, untyped]) -> String |
| 114 | + def active_scope_for(kind:, filters:) |
| 115 | + "#{kind}:#{Digest::SHA256.hexdigest(filters.to_json)}" |
| 116 | + end |
| 117 | + |
| 118 | + # @rbs (Symbol, authorization_context: untyped, ?resource_id: String?) -> void |
| 119 | + def authorize!(action, authorization_context:, resource_id: nil) |
| 120 | + authorized = SolidObjects.configuration.authorize_administration.call( |
| 121 | + action: action.to_s, |
| 122 | + resource: "redrives", |
| 123 | + resource_id:, |
| 124 | + authorization_context: |
| 125 | + ) |
| 126 | + return if authorized |
| 127 | + |
| 128 | + raise Unauthorized, "actor administration is not authorized" |
| 129 | + end |
| 130 | + end |
| 131 | +end |
0 commit comments