Commit a06ad87
authored
Enable job insertion on both transactions and top level pool (#9)
A problem with the API currently is that is that inserts on a top-level
SQLAlchemy engine aren't supported. In the test suite, the driver is
always initialized with a transaction:
with engine.begin() as tx:
yield riversqlalchemy.Driver(tx)
But if trying to initialize the driver with the engine itself, we'd see
failures because it doesn't support the necessary SQL execution methods
required to insert a job. Additionally, when checking for a unique job,
the driver always opens a transaction with `self.conn.begin_nested()`,
which will error unless in a transaction already.
In the Ruby client there's only a single `River#insert` method because
convention in that language is to have a lot of global stuff going on,
and the current transaction is added to local thread storage by Sequel
or ActiveRecord so that we can determine whether or not we're already in
a transaction from within the client.
SQLAlchemy is a little different. After opening a transaction, you're
expected to track the session object for perform operations in it:
with session.begin():
session.add(some_object())
session.add(some_other_object())
So here I'm proposing a client API that looks a little more like the Go
API, with a pair of insert functions for `#insert` and `#insert_tx`, the
former doing an insert on the connection originally passed to driver,
and the latter doing an insert on a transaction sent as argument.
So this style of top-level insertion is now possible:
engine = sqlalchemy.create_engine(database_url)
client = riverqueue.Client(riversqlalchemy.Driver(engine))
insert_res = client.insert(
MyJobArgs(),
insert_opts=riverqueue.InsertOpts(
unique_opts=riverqueue.UniqueOpts(by_period=900)
),
)
The client opens a new transaction on the driver's engine, and inserts
the job there.
Insertion on a particular transaction continues to be possible, and now
without having to reinitialize the client/driver:
with engine.begin() as session:
insert_res = client.insert_tx(
session,
MyJobArgs(),
insert_opts=riverqueue.InsertOpts(
unique_opts=riverqueue.UniqueOpts(by_period=900)
),
)
print(insert_res)1 parent 52b6850 commit a06ad87
File tree
6 files changed
+192
-69
lines changed- src/riverqueue
- driver
- riversqlalchemy
- tests
- driver/riversqlalchemy
6 files changed
+192
-69
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
| 5 | + | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
| |||
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
56 | 70 | | |
57 | 71 | | |
58 | 72 | | |
59 | 73 | | |
60 | 74 | | |
61 | | - | |
62 | | - | |
| 75 | + | |
63 | 76 | | |
64 | | - | |
| 77 | + | |
65 | 78 | | |
66 | 79 | | |
67 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
68 | 95 | | |
69 | 96 | | |
70 | 97 | | |
71 | 98 | | |
72 | 99 | | |
73 | 100 | | |
74 | 101 | | |
75 | | - | |
76 | 102 | | |
77 | 103 | | |
78 | 104 | | |
| 105 | + | |
79 | 106 | | |
80 | 107 | | |
81 | 108 | | |
| |||
125 | 152 | | |
126 | 153 | | |
127 | 154 | | |
128 | | - | |
| 155 | + | |
129 | 156 | | |
130 | 157 | | |
131 | 158 | | |
132 | 159 | | |
133 | 160 | | |
134 | 161 | | |
135 | 162 | | |
136 | | - | |
| 163 | + | |
137 | 164 | | |
138 | | - | |
| 165 | + | |
139 | 166 | | |
140 | 167 | | |
141 | 168 | | |
142 | 169 | | |
143 | 170 | | |
144 | 171 | | |
145 | 172 | | |
146 | | - | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
147 | 176 | | |
148 | 177 | | |
149 | 178 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | 3 | | |
3 | | - | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
34 | 35 | | |
35 | 36 | | |
36 | 37 | | |
37 | | - | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
41 | | - | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
53 | 63 | | |
Lines changed: 44 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
3 | 5 | | |
4 | | - | |
| 6 | + | |
5 | 7 | | |
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | | - | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
14 | 16 | | |
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
18 | | - | |
19 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
20 | 27 | | |
21 | 28 | | |
22 | 29 | | |
23 | 30 | | |
24 | 31 | | |
25 | 32 | | |
26 | 33 | | |
27 | | - | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
28 | 40 | | |
29 | 41 | | |
30 | 42 | | |
31 | | - | |
32 | | - | |
33 | | - | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
0 commit comments