Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'cleanup_old_experiments' error when running from non-web (eg. console) #715

Closed
jmccaffrey opened this issue Aug 29, 2023 · 2 comments
Closed
Assignees

Comments

@jmccaffrey
Copy link

jmccaffrey commented Aug 29, 2023

The steps in the instructions in the "outside-of-a-web-session" are not working

I cloned the example project
https://github.com/splitrb/split-rails-example
I have redis running and the sample page and dashboard work as expected.
Hopping into a rails console for that project and following the steps from
#outside-of-a-web-session
it fails on
trial.choose!
with error
NoMethodError (undefined method `cleanup_old_experiments!' for nil:NilClass)
Looking at the code, it assumes that a Split::User is loaded.

It makes sense that some type of context and unique ID would need to be passed along when creating the new Trial instance, but just following the steps listed does not flow as expected. (unless there is something about an adapter that is required to make it work)

To reproduce:
git clone https://github.com/splitrb/split-rails-example
cd split-rails-example
bundle
(start redis if needed)
rails c

#- create a new experiment
experiment = Split::ExperimentCatalog.find_or_create('color', 'red', 'blue')
#- create a new trial
trial = Split::Trial.new(:experiment => experiment)
#- run trial
trial.choose! # << this will fail

@jmccaffrey
Copy link
Author

I had not seen the steps described in the wiki
https://github.com/splitrb/split/wiki/Use-Split-outside-a-web-request

The key part is to pass a user reference (and know how an existing user is identified)
key = "user-#{user.id}"
ab_user = Split::User.new(nil, Split::Persistence::RedisAdapter.new(nil, key))

        trial = Split::Trial.new(user: ab_user, experiment: experiment) # << passing in the ab_user is the key
        trial.choose! # << now this doesn't fail

The readme can just be updated with the part about initing a user reference to pass to Trial.new, then trial.choose! won't fail

@andrehjr
Copy link
Member

andrehjr commented Sep 3, 2023

Hi @jmccaffrey thanks for the issue!

I fixed the readme on #716 it's also possible to load doingSplit::User.find(user_key, :redis) as a shortcut.

@andrehjr andrehjr closed this as completed Sep 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants