@@ -22,66 +22,72 @@ def self.dangerous_attribute_method?(method_name)
2222 return false if method_name == "errors"
2323 super
2424 end
25+
26+ # See comment above, but since we force allowed `errors` as an
27+ # attribute name, ActiveRecord would otherwise fail to save a row as
28+ # it checked for its own `errors` hash and finding no values.
29+ def errors = { }
2530 end )
2631 end
2732 end
2833
2934 def insert ( insert_params )
35+ to_job_row ( RiverJob . create ( insert_params_to_hash ( insert_params ) ) )
36+ end
37+
38+ def insert_many ( insert_params_many )
39+ RiverJob . insert_all ( insert_params_many . map { |p | insert_params_to_hash ( p ) } )
40+ insert_params_many . count
41+ end
42+
43+ private def insert_params_to_hash ( insert_params )
3044 # the call to `#compact` is important so that we remove nils and table
3145 # default values get picked up instead
32- to_job_row (
33- RiverJob . insert (
34- {
35- args : insert_params . encoded_args ,
36- kind : insert_params . kind ,
37- max_attempts : insert_params . max_attempts ,
38- priority : insert_params . priority ,
39- queue : insert_params . queue ,
40- state : insert_params . state ,
41- scheduled_at : insert_params . scheduled_at ,
42- tags : insert_params . tags
43- } . compact ,
44- returning : Arel . sql ( "*" )
45- ) . first
46- )
46+ {
47+ args : insert_params . encoded_args ,
48+ kind : insert_params . kind ,
49+ max_attempts : insert_params . max_attempts ,
50+ priority : insert_params . priority ,
51+ queue : insert_params . queue ,
52+ state : insert_params . state ,
53+ scheduled_at : insert_params . scheduled_at ,
54+ tags : insert_params . tags
55+ } . compact
4756 end
4857
4958 # Type type injected to this method is not a `RiverJob`, but rather a raw
5059 # hash with stringified keys because we're inserting with the Arel framework
5160 # directly rather than generating a record from a model.
52- private def to_job_row ( raw_job )
53- deserialize = -> ( field ) do
54- RiverJob . _default_attributes [ field ] . type . deserialize ( raw_job [ field ] )
55- end
56-
57- # Errors is `jsonb[]` so the subtype here will decode `jsonb`.
58- errors_subtype = RiverJob . _default_attributes [ "errors" ] . type . subtype
61+ private def to_job_row ( river_job )
62+ # needs to be accessed through values because `errors` is shadowed by both
63+ # ActiveRecord and the patch above
64+ errors = river_job . attributes [ "errors" ]
5965
6066 River ::JobRow . new (
61- id : deserialize . call ( "id" ) ,
62- args : deserialize . call ( " args" ) . yield_self { | a | a ? JSON . parse ( a ) : nil } ,
63- attempt : deserialize . call ( " attempt" ) ,
64- attempted_at : deserialize . call ( " attempted_at" ) ,
65- attempted_by : deserialize . call ( " attempted_by" ) ,
66- created_at : deserialize . call ( " created_at" ) ,
67- errors : deserialize . call ( " errors" ) &.map do |e |
68- deserialized_error = errors_subtype . deserialize ( e )
67+ id : river_job . id ,
68+ args : river_job . args ? JSON . parse ( river_job . args ) : nil ,
69+ attempt : river_job . attempt ,
70+ attempted_at : river_job . attempted_at ,
71+ attempted_by : river_job . attempted_by ,
72+ created_at : river_job . created_at ,
73+ errors : errors &.map { |e |
74+ deserialized_error = JSON . parse ( e , symbolize_names : true )
6975
7076 River ::AttemptError . new (
71- at : Time . parse ( deserialized_error [ "at" ] ) ,
72- attempt : deserialized_error [ " attempt" ] ,
73- error : deserialized_error [ " error" ] ,
74- trace : deserialized_error [ " trace" ]
77+ at : Time . parse ( deserialized_error [ :at ] ) ,
78+ attempt : deserialized_error [ : attempt] ,
79+ error : deserialized_error [ : error] ,
80+ trace : deserialized_error [ : trace]
7581 )
76- end ,
77- finalized_at : deserialize . call ( " finalized_at" ) ,
78- kind : deserialize . call ( " kind" ) ,
79- max_attempts : deserialize . call ( " max_attempts" ) ,
80- priority : deserialize . call ( " priority" ) ,
81- queue : deserialize . call ( " queue" ) ,
82- scheduled_at : deserialize . call ( " scheduled_at" ) ,
83- state : deserialize . call ( " state" ) ,
84- tags : deserialize . call ( " tags" )
82+ } ,
83+ finalized_at : river_job . finalized_at ,
84+ kind : river_job . kind ,
85+ max_attempts : river_job . max_attempts ,
86+ priority : river_job . priority ,
87+ queue : river_job . queue ,
88+ scheduled_at : river_job . scheduled_at ,
89+ state : river_job . state ,
90+ tags : river_job . tags
8591 )
8692 end
8793 end
0 commit comments