Make sure cursor is a keyword argument#35
Conversation
cf95f9b to
4ffd907
Compare
cursor is a keyword argumentcursor is a keyword argument
| def valid_cursor_parameter?(parameters) | ||
| # this condition is when people use the splat operator. | ||
| # def build_enumerator(*) | ||
| return true if parameters == [[:rest]] |
| # this condition is when people use the splat operator. | ||
| # def build_enumerator(*) | ||
| return true if parameters == [[:rest]] | ||
|
|
There was a problem hiding this comment.
Wouldn't this be slightly simpler?
parameters.each do |parameter_type, parameter_name|
next unless parameter_name == :cursor
return true if parameter_type == :keyreq
end
false
end
| work_one_job | ||
| end | ||
|
|
||
| def test_jobs_that_do_not_define_build_enumerator_or_each_iteration_will_not_raise |
There was a problem hiding this comment.
each_iteration_will_not_raise but the test has an assert_raises(ArgumentError)
4ffd907 to
c572cfb
Compare
kirs
left a comment
There was a problem hiding this comment.
Thanks for making it a better experience!
|
|
||
| unless respond_to?(:build_enumerator, true) | ||
| if respond_to?(:build_enumerator, true) | ||
| parameters = method(:build_enumerator).parameters |
There was a problem hiding this comment.
This is not strictly related to your PR or needs addressing, but it's somewhat pointless to check for arguments and the method signature on every perform of a job. Once a job class has been validated, we know it implements methods right. This makes me believe that we should be skipping assert_implements_methods! for classes that has already been validated.
There was a problem hiding this comment.
That is a great point, we could have a local cache of checked classes and avoid having to call assert_implements_methods! unnecessary. I will explore this on another PR.
| parameters = method(:build_enumerator).parameters | ||
| unless valid_cursor_parameter?(parameters) | ||
| raise ArgumentError, "Iteration job (#{self.class}) #build_enumerator " \ | ||
| "expects the keyword argument `cursor` please make sure that this condition is met" |
There was a problem hiding this comment.
Nitpick: please make sure that this condition is met may not be the language that's typically used in exception messages. "expects the keyword argument cursor" is probably enough for developers to understand that's a condition that they have to satisfy :)
|
Looks like |
a keyword argument. This can lead to unxpected behaviour. This change introiduce an extra level of checking so when running the job, we make sure the signature of build_enumerator has the `cursor` keyword argument
c572cfb to
bc02e96
Compare
We found cases of people using
cursorargument, not as a keyword argument.This can lead to unexpected behaviour.
Discourse ticket
This change introduces an extra level of checking so when running the
job, we make sure the signature of build_enumerator has the
cursorkeywordargument
@Shopify/job-patterns