riverqueue-sequel 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/driver.rb +120 -0
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44b425525ec34fe8f01454e40059d2df25d71b45c7ed8cc8d443cac6c2d57392
4
- data.tar.gz: '01409f01a17830e9bdc189e7dd66f1235abe4481960d35151ce4216c685fa7a2'
3
+ metadata.gz: be1e2d14a88044372151be6e4847bd9a2de21ba73fd58a20d4734896ede240b7
4
+ data.tar.gz: 442331e31b711d7e2697bd0a75bde161a77ef604b26eed70404f5aca3ede4da1
5
5
  SHA512:
6
- metadata.gz: 15df123a1d9ae7313f47cb7df408200e91cdc72b357a4a1c69b7fbf1b819134bda6985aceed1afcd95d23b5b9ef2f1cc1468d6df50c58438809d99e7d6af3808
7
- data.tar.gz: 1ccb057569cdaed47588b91a69454a015c8f3ebd23f7685864dd8993b098693c9bb30772272c52ce3fb9e90624ef7bea1e47220af8a29bb3e9fa3d662639aa72
6
+ metadata.gz: cd06a5f8cc1dec3eb84cedc0a97b2dc1faefe36845936c4147b8fd95262ff4885ec8c0e063566ba469cf89c2ff6dd57bf5e5bb89f48117e6e8b849d77ad80198
7
+ data.tar.gz: f50260109abb7f2edd09d15ca68edee77c44a0f79570d654d9d4aea522298cf6169d5632fddc983455cd3789e9c3101ee2dd6e0e00ac6f5cb3a1aff08d8c9730
data/lib/driver.rb ADDED
@@ -0,0 +1,120 @@
1
+ module River::Driver
2
+ # Provides a Sequel driver for River.
3
+ #
4
+ # Used in conjunction with a River client like:
5
+ #
6
+ # DB = Sequel.connect("postgres://...")
7
+ # client = River::Client.new(River::Driver::Sequel.new(DB))
8
+ #
9
+ class Sequel
10
+ def initialize(db)
11
+ @db = db
12
+ @db.extension(:pg_array)
13
+ @db.extension(:pg_json)
14
+ end
15
+
16
+ def advisory_lock(key)
17
+ @db.fetch("SELECT pg_advisory_xact_lock(?)", key).first
18
+ nil
19
+ end
20
+
21
+ def advisory_lock_try(key)
22
+ @db.fetch("SELECT pg_try_advisory_xact_lock(?)", key).first[:pg_try_advisory_xact_lock]
23
+ end
24
+
25
+ def job_get_by_id(id)
26
+ data_set = @db[:river_job].where(id: id)
27
+ data_set.first ? to_job_row(data_set.first) : nil
28
+ end
29
+
30
+ def job_get_by_kind_and_unique_properties(get_params)
31
+ data_set = @db[:river_job].where(kind: get_params.kind)
32
+ data_set = data_set.where(::Sequel.lit("tstzrange(?, ?, '[)') @> created_at", get_params.created_at[0], get_params.created_at[1])) if get_params.created_at
33
+ data_set = data_set.where(args: get_params.encoded_args) if get_params.encoded_args
34
+ data_set = data_set.where(queue: get_params.queue) if get_params.queue
35
+ data_set = data_set.where(state: get_params.state) if get_params.state
36
+ data_set.first ? to_job_row(data_set.first) : nil
37
+ end
38
+
39
+ def job_insert(insert_params)
40
+ to_job_row(@db[:river_job].returning.insert_select(insert_params_to_hash(insert_params)))
41
+ end
42
+
43
+ def job_insert_unique(insert_params, unique_key)
44
+ values = @db[:river_job]
45
+ .insert_conflict(
46
+ target: [:kind, :unique_key],
47
+ conflict_where: ::Sequel.lit("unique_key IS NOT NULL"),
48
+ update: {kind: ::Sequel[:excluded][:kind]}
49
+ )
50
+ .returning(::Sequel.lit("*, (xmax != 0) AS unique_skipped_as_duplicate"))
51
+ .insert_select(
52
+ insert_params_to_hash(insert_params).merge(unique_key: ::Sequel.blob(unique_key))
53
+ )
54
+
55
+ [to_job_row(values), values[:unique_skipped_as_duplicate]]
56
+ end
57
+
58
+ def job_insert_many(insert_params_many)
59
+ @db[:river_job].multi_insert(insert_params_many.map { |p| insert_params_to_hash(p) })
60
+ insert_params_many.count
61
+ end
62
+
63
+ def job_list
64
+ data_set = @db[:river_job].order_by(:id)
65
+ data_set.all.map { |job| to_job_row(job) }
66
+ end
67
+
68
+ def rollback_exception
69
+ ::Sequel::Rollback
70
+ end
71
+
72
+ def transaction(&)
73
+ @db.transaction(savepoint: true, &)
74
+ end
75
+
76
+ private def insert_params_to_hash(insert_params)
77
+ # the call to `#compact` is important so that we remove nils and table
78
+ # default values get picked up instead
79
+ {
80
+ args: insert_params.encoded_args,
81
+ kind: insert_params.kind,
82
+ max_attempts: insert_params.max_attempts,
83
+ priority: insert_params.priority,
84
+ queue: insert_params.queue,
85
+ state: insert_params.state,
86
+ scheduled_at: insert_params.scheduled_at,
87
+ tags: insert_params.tags ? ::Sequel.pg_array(insert_params.tags) : nil
88
+ }.compact
89
+ end
90
+
91
+ private def to_job_row(river_job)
92
+ River::JobRow.new(
93
+ id: river_job[:id],
94
+ args: river_job[:args].to_h,
95
+ attempt: river_job[:attempt],
96
+ attempted_at: river_job[:attempted_at]&.getutc,
97
+ attempted_by: river_job[:attempted_by],
98
+ created_at: river_job[:created_at].getutc,
99
+ errors: river_job[:errors]&.map { |deserialized_error|
100
+ River::AttemptError.new(
101
+ at: Time.parse(deserialized_error["at"]),
102
+ attempt: deserialized_error["attempt"],
103
+ error: deserialized_error["error"],
104
+ trace: deserialized_error["trace"]
105
+ )
106
+ },
107
+ finalized_at: river_job[:finalized_at]&.getutc,
108
+ kind: river_job[:kind],
109
+ max_attempts: river_job[:max_attempts],
110
+ metadata: river_job[:metadata],
111
+ priority: river_job[:priority],
112
+ queue: river_job[:queue],
113
+ scheduled_at: river_job[:scheduled_at].getutc,
114
+ state: river_job[:state],
115
+ tags: river_job[:tags].to_a,
116
+ unique_key: river_job[:unique_key]&.to_s
117
+ )
118
+ end
119
+ end
120
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riverqueue-sequel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Blake Gentry
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-07-07 00:00:00.000000000 Z
12
+ date: 2024-08-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg
@@ -58,6 +58,7 @@ executables: []
58
58
  extensions: []
59
59
  extra_rdoc_files: []
60
60
  files:
61
+ - lib/driver.rb
61
62
  - lib/riverqueue-sequel.rb
62
63
  homepage: https://riverqueue.com
63
64
  licenses: