Class: M365ActiveStorage::PendingDelete

Inherits:
Object
  • Object
show all
Defined in:
lib/m365_active_storage/pending_delete.rb

Overview

Pending Deletion Storage

Manages filename mapping during blob deletion to support deferred deletion from SharePoint.

Purpose

When an ActiveStorage blob is destroyed, the blob record is deleted from the database immediately. However, the actual file deletion from SharePoint may need to happen asynchronously. This class provides thread-safe storage to maintain the mapping between blob keys and filenames after the blob is deleted so that background workers can retrieve the filename and delete the file from SharePoint.

Thread Safety

All operations are protected by a Mutex to ensure thread-safe access in concurrent environments (e.g., multiple Sidekiq workers).

Architecture

PendingDelete acts as a temporary in-memory registry. When a blob is destroyed:

  1. The before_destroy callback stores the key -> filename mapping

  2. A background job retrieves the filename and deletes from SharePoint

  3. The mapping is removed from storage

Note: This is an in-memory store and will be lost if the process restarts. For production use, consider backing this with Redis or a database table.

See Also:

Class Method Summary collapse

Class Method Details

.get(key) ⇒ String?

Retrieve and remove a filename from the pending deletion registry

Thread-safe method to get a filename by key and remove it from storage. Called by background deletion workers to get the filename for SharePoint deletion.

Examples:

filename = M365ActiveStorage::PendingDelete.get("abc123")
if filename
  delete_from_sharepoint(filename)
end

Parameters:

  • key (String)

    The blob’s storage key

Returns:

  • (String, nil)

    The stored filename, or nil if not found



71
72
73
74
75
# File 'lib/m365_active_storage/pending_delete.rb', line 71

def self.get(key)
  @mutex.synchronize do
    @pending_deletes.delete(key)
  end
end

.store(key, filename) ⇒ void

This method returns an undefined value.

Store a blob key and filename for later retrieval during deletion

Thread-safe method to add a key-filename pair to the pending deletion registry. Called by the Railtie before_destroy callback when a blob is deleted.

Examples:

M365ActiveStorage::PendingDelete.store("abc123", "document.pdf")

Parameters:

  • key (String)

    The blob’s storage key

  • filename (String)

    The blob’s filename



52
53
54
55
56
# File 'lib/m365_active_storage/pending_delete.rb', line 52

def self.store(key, filename)
  @mutex.synchronize do
    @pending_deletes[key] = filename
  end
end