@@ -13,51 +13,34 @@ def initialize(db)
1313 @db . extension ( :pg_json )
1414 end
1515
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-
2516 def job_get_by_id ( id )
2617 data_set = @db [ :river_job ] . where ( id : id )
2718 data_set . first ? to_job_row ( data_set . first ) : nil
2819 end
2920
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-
3921 def job_insert ( insert_params )
40- to_job_row ( @db [ :river_job ] . returning . insert_select ( insert_params_to_hash ( insert_params ) ) )
22+ results = insert_jobs ( [ insert_params ] )
23+ [ to_job_row ( results . first ) , results . first [ :unique_skipped_as_duplicate ] ]
24+ end
25+
26+ def job_insert_many ( insert_params_many )
27+ results = insert_jobs ( insert_params_many )
28+ # Count the number of jobs actually inserted (not skipped)
29+ inserted_count = results . count { |row | !row [ :unique_skipped_as_duplicate ] }
30+ inserted_count
4131 end
4232
43- def job_insert_unique ( insert_params , unique_key )
44- values = @db [ :river_job ]
33+ private def insert_jobs ( insert_params_array )
34+ @db [ :river_job ]
4535 . insert_conflict (
46- target : [ :kind , :unique_key ] ,
47- conflict_where : ::Sequel . lit ( "unique_key IS NOT NULL" ) ,
36+ target : [ :unique_key ] ,
37+ conflict_where : ::Sequel . lit (
38+ "unique_key IS NOT NULL AND unique_states IS NOT NULL AND river_job_state_in_bitmask(unique_states, state)"
39+ ) ,
4840 update : { kind : ::Sequel [ :excluded ] [ :kind ] }
4941 )
5042 . 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
43+ . multi_insert ( insert_params_array . map { |p | insert_params_to_hash ( p ) } )
6144 end
6245
6346 def job_list
@@ -76,6 +59,7 @@ def transaction(&)
7659 private def insert_params_to_hash ( insert_params )
7760 # the call to `#compact` is important so that we remove nils and table
7861 # default values get picked up instead
62+ # TODO: but I had to remove it for bulk unique inserts...
7963 {
8064 args : insert_params . encoded_args ,
8165 kind : insert_params . kind ,
@@ -84,8 +68,10 @@ def transaction(&)
8468 queue : insert_params . queue ,
8569 state : insert_params . state ,
8670 scheduled_at : insert_params . scheduled_at ,
87- tags : insert_params . tags ? ::Sequel . pg_array ( insert_params . tags ) : nil
88- } . compact
71+ tags : insert_params . tags ? ::Sequel . pg_array ( insert_params . tags , :text ) : nil ,
72+ unique_key : insert_params . unique_key ? ::Sequel . blob ( insert_params . unique_key ) : nil ,
73+ unique_states : insert_params . unique_states
74+ }
8975 end
9076
9177 private def to_job_row ( river_job )
@@ -113,7 +99,8 @@ def transaction(&)
11399 scheduled_at : river_job [ :scheduled_at ] . getutc ,
114100 state : river_job [ :state ] ,
115101 tags : river_job [ :tags ] . to_a ,
116- unique_key : river_job [ :unique_key ] &.to_s
102+ unique_key : river_job [ :unique_key ] &.to_s ,
103+ unique_states : river_job [ :unique_states ] &.to_i ( 2 )
117104 )
118105 end
119106 end
0 commit comments