-
-
Notifications
You must be signed in to change notification settings - Fork 368
Description
I'd like to use Split outside an rack request, as part of a background job. To complicate matters even further, my "user" isn't a user at all, it's a different model, so I need to set a custom persistence key.
Fortunately, it appears most of the work to make this possible was accomplished in this #286 by @joshdover. Unfortunately, some things have changed and the example provided in #286 doesn't work anymore.
I made a few tweaks, but I'm still not there:
# My "user" is a Lead, so I need to set a custom key prefix to prevent clashing with experiments made against users
ab_user = Split::User.new(Split::Persistence.adapter.new(nil, "lead_id_#{lead.id}")
experiment = Split::ExperimentCatalog.find_or_create(:welcome_message)
trial = Split::Trial.new(user: ab_user, experiment: experiment)
puts trial.choose!
The last line, trial.choose!
fails because my ab_user
isn't a Split::User
and doesn't implement the method . cleanup_old_experiments!
.
NoMethodError: undefined method `cleanup_old_experiments' for #<Split::Persistence::RedisAdapter:0x007f9980bbb450>
The obvious solution might be to instantiate a new Split::User
instead of treating the persistence adapter as a user. But when instantiating a Split::User
I can't provide a custom adapter or key:
module Split
class User
def initialize(context)
@user = Split::Persistence.adapter.new(context)
end
end
end
I can only provide the context which is useless to me, since I'm not in the context of a HTTP request.
Any ideas on how to best support running experiments outside a request? Supporting an optional adapter
or key
argument to Split::User.new
feels like the easy solution here.
For now, I'm using this monkey patch:
module Split
class User
def initialize(context, key=nil)
@user = Split::Persistence.adapter.new(context, key)
end
end
end