ActiveJob should support passing of keyword arguments to perform method #18741

Closed
threadhead opened this Issue Jan 30, 2015 · 4 comments

Projects

None yet

3 participants

@threadhead

Since Rails 5+ will required Ruby 2.2+, ActiveJob should support the passing of keyword arguments to the perform method.

class MyAwesomeJob < ActiveJob::Base
  def perform(ginger_ninjas: 1)
    attack!(ginger_ninjas)
  end
end

MyAwesomeJob.perform_later(ginger_ninjas: 5)

Currently (rails 4.2), you get a ArgumentError: wrong number of arguments (1 for 0) if you try to pass keyword arguments.

@sgrif sgrif added the activejob label Jan 30, 2015
@sgrif sgrif added this to the 4.2.1 milestone Jan 30, 2015
@sgrif
Member
sgrif commented Jan 30, 2015

This appears to be due to the hash getting converted to string keys during job serialization/deserialization, while keyword arguments only work with symbol keyed hashes.

@sgrif sgrif added a commit that referenced this issue Jan 30, 2015
@sgrif sgrif Allow keyword arguments to work with ActiveJob
Unfortunately, the HashWithIndifferent access approach is insufficient
for our needs. It's perfectly reasonable to want to use keyword
arguments with Active Job, which we will see as a symbol keyed hash. For
Ruby to convert this back to keyword arguments, it must deserialize to a
symbol keyed hash.

There are two primary changes to the serialization behavior. We first
treat a HWIA separately, and mark it as such so we can convert it back
into a HWIA during deserialization.

For normal hashes, we keep a list of all symbol keys, and convert them
back to symbol keys after deserialization.

Fixes #18741.

Conflicts:
	activejob/CHANGELOG.md
03476a6
@sgrif sgrif added a commit that closed this issue Jan 30, 2015
@sgrif sgrif Allow keyword arguments to work with ActiveJob
Unfortunately, the HashWithIndifferent access approach is insufficient
for our needs. It's perfectly reasonable to want to use keyword
arguments with Active Job, which we will see as a symbol keyed hash. For
Ruby to convert this back to keyword arguments, it must deserialize to a
symbol keyed hash.

There are two primary changes to the serialization behavior. We first
treat a HWIA separately, and mark it as such so we can convert it back
into a HWIA during deserialization.

For normal hashes, we keep a list of all symbol keys, and convert them
back to symbol keys after deserialization.

Fixes #18741.
31085a5
@sgrif sgrif closed this in 31085a5 Jan 30, 2015
@sgrif
Member
sgrif commented Jan 30, 2015

Backported in 31085a5

@threadhead

@sgrif Impressive. I was going to start hacking on it after I got back from lunch, but you beat me to it. Thank you.

@tgxworld tgxworld added a commit to ruby-bench/ruby-bench-web that referenced this issue Feb 13, 2015
@tgxworld tgxworld Use hash till the patch upstream has been released. 92ed97b
@chrisnicola
Contributor

This is awesome. I was testing it out with Ruby 4.2.4 and noticed that if you mix keyword and regular arguments it doesn't work. For example:

perform(message, channel: '#default')

Won't work and gives you a ArgumentError: wrong number of arguments (2 for 1) error.

perform(message:, channel: '#default')

Does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment