Class: CycloneLariat::Outbox::Repo::Sequel::Messages

Inherits:
Object
  • Object
show all
Defined in:
lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb

Constant Summary collapse

LIMIT =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset) ⇒ Messages

Returns a new instance of Messages.



17
18
19
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 17

def initialize(dataset)
  @dataset = dataset
end

Instance Attribute Details

#datasetObject (readonly)

Returns the value of attribute dataset.



15
16
17
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 15

def dataset
  @dataset
end

Instance Method Details

#create(msg) ⇒ Object



21
22
23
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 21

def create(msg)
  dataset.returning.insert(Outbox::Mappers::Messages.to_row(msg)).first[:uuid]
end

#delete(uuid) ⇒ Object



25
26
27
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 25

def delete(uuid)
  dataset.where(uuid: uuid).delete
end

#each_with_errorObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 33

def each_with_error
  dataset
    .where { sending_error !~ nil }
    .order(::Sequel.asc(:created_at))
    .limit(LIMIT)
    .each do |row|
      msg = build Outbox::Mappers::Messages.from_row(row)
      yield(msg)
    end
end

#lock(uuid) ⇒ Object



48
49
50
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 48

def lock(uuid)
  dataset.where(uuid: uuid).for_update.nowait
end

#transaction(&block) ⇒ Object



44
45
46
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 44

def transaction(&block)
  dataset.db.transaction(&block)
end

#update_error(uuid, error_message) ⇒ Object



29
30
31
# File 'lib/cyclone_lariat/plugins/outbox/repo/sequel/messages.rb', line 29

def update_error(uuid, error_message)
  dataset.where(uuid: uuid).update(sending_error: error_message)
end