Class: FastMcpPubsub::MessageStore

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_mcp_pubsub/message_store.rb

Constant Summary collapse

TABLE_NAME =
"fast_mcp_pubsub_messages"

Class Method Summary collapse

Class Method Details

.cleanup(older_than: Time.now - 300) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/fast_mcp_pubsub/message_store.rb', line 24

def cleanup(older_than: Time.now - 300)
  return unless @table_exists

  ActiveRecord::Base.connection.execute(
    "DELETE FROM #{TABLE_NAME} WHERE created_at < #{quote(older_than.iso8601)}"
  )
end

.ensure_table_existsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fast_mcp_pubsub/message_store.rb', line 32

def ensure_table_exists
  return if @table_exists

  unless ActiveRecord::Base.connection.table_exists?(TABLE_NAME)
    ActiveRecord::Base.connection.execute(<<~SQL)
      CREATE TABLE IF NOT EXISTS #{TABLE_NAME} (
        id UUID PRIMARY KEY,
        payload TEXT NOT NULL,
        created_at TIMESTAMP NOT NULL DEFAULT NOW()
      )
    SQL
  end
  @table_exists = true
end

.fetch(id) ⇒ Object



17
18
19
20
21
22
# File 'lib/fast_mcp_pubsub/message_store.rb', line 17

def fetch(id)
  ensure_table_exists
  ActiveRecord::Base.connection.select_value(
    "SELECT payload FROM #{TABLE_NAME} WHERE id = #{quote(id)}"
  )
end

.store(payload) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/fast_mcp_pubsub/message_store.rb', line 8

def store(payload)
  ensure_table_exists
  id = SecureRandom.uuid
  ActiveRecord::Base.connection.execute(
    "INSERT INTO #{TABLE_NAME} (id, payload, created_at) VALUES (#{quote(id)}, #{quote(payload)}, NOW())"
  )
  id
end