Skip to content

Commit

Permalink
Change transaction constructors to accept parent store instance
Browse files Browse the repository at this point in the history
  • Loading branch information
akadusei committed Aug 5, 2024
1 parent 4f183ff commit e2cf621
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased] -

### Changed
- Change transaction constructors to accept parent store instance as argument

## [0.21.0] - 2024-08-02

### Added
Expand Down
14 changes: 7 additions & 7 deletions src/mel/memory.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module Mel
end

def transaction(& : Transaction -> _)
yield Transaction.new(queue, tasks, progress)
yield Transaction.new(self)
end

def truncate
Expand Down Expand Up @@ -139,24 +139,24 @@ module Mel
struct Transaction
include Store::Transaction

def initialize(@queue : Queue, @tasks : Tasks, @progress : Progress)
def initialize(@memory : Memory)
end

def create(task : Task)
@queue[task.id] ||= task.time.to_unix
@tasks[task.id] ||= task.to_json
@memory.queue[task.id] ||= task.time.to_unix
@memory.tasks[task.id] ||= task.to_json
end

def update(task : Task)
time = task.retry_time || task.time

@queue[task.id] = time.to_unix
@tasks[task.id] = task.to_json
@memory.queue[task.id] = time.to_unix
@memory.tasks[task.id] = task.to_json
end

def set_progress(id : String, value : Int, description : String)
report = Mel::Progress::Report.new(id, description, value)
@progress[id] = ProgressEntry.new(report.to_json)
@memory.progress[id] = ProgressEntry.new(report.to_json)
end
end

Expand Down
30 changes: 17 additions & 13 deletions src/redis.cr
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ module Mel

def transaction(& : Transaction -> _)
@client.multi do |redis|
yield Transaction.new(redis, key)
yield Transaction.new(self, redis)
end
end

Expand Down Expand Up @@ -161,38 +161,42 @@ module Mel
struct Transaction
include Store::Transaction

def initialize(@client : ::Redis::Transaction, @key : Key)
def initialize(@redis : Redis, @client : ::Redis::Transaction)
end

def self.new(url : String, namespace = :mel)
new URI.parse(url), namespace
def self.new(redis : Redis, url : String)
new redis, URI.parse(url)
end

def self.new(url : URI, namespace = :mel)
new ::Redis::Connection.new(url), namespace
def self.new(redis : Redis, url : URI)
new redis, ::Redis::Connection.new(url)
end

def self.new(connection : Redis::Connection, namespace = :mel)
new Redis::Transaction.new(connection), namespace
def self.new(redis : Redis, connection : Redis::Connection)
new redis, ::Redis::Transaction.new(connection)
end

def create(task : Task)
@client.zadd(@key.name, {"NX", task.time.to_unix.to_s, task.id})
@client.set(@key.name(task.id), task.to_json, nx: true)
@client.zadd(
@redis.key.name,
{"NX", task.time.to_unix.to_s, task.id}
)

@client.set(@redis.key.name(task.id), task.to_json, nx: true)
end

def update(task : Task)
time = task.retry_time || task.time

@client.zadd(@key.name, time.to_unix.to_s, task.id)
@client.set(@key.name(task.id), task.to_json)
@client.zadd(@redis.key.name, time.to_unix.to_s, task.id)
@client.set(@redis.key.name(task.id), task.to_json)
end

def set_progress(id : String, value : Int, description : String)
report = Progress::Report.new(id, description, value)

@client.set(
@key.progress(id),
@redis.key.progress(id),
report.to_json,
ex: Mel.settings.progress_expiry
)
Expand Down

0 comments on commit e2cf621

Please sign in to comment.