{"action":"closed","issue":{"url":"https://api.github.com/repos/splitrb/split/issues/410","repository_url":"https://api.github.com/repos/splitrb/split","labels_url":"https://api.github.com/repos/splitrb/split/issues/410/labels{/name}","comments_url":"https://api.github.com/repos/splitrb/split/issues/410/comments","events_url":"https://api.github.com/repos/splitrb/split/issues/410/events","html_url":"https://github.com/splitrb/split/issues/410","id":159036713,"number":410,"title":"Run experiment outside context of request","user":{"login":"ndelage","id":547853,"avatar_url":"https://avatars.githubusercontent.com/u/547853?v=3","gravatar_id":"","url":"https://api.github.com/users/ndelage","html_url":"https://github.com/ndelage","followers_url":"https://api.github.com/users/ndelage/followers","following_url":"https://api.github.com/users/ndelage/following{/other_user}","gists_url":"https://api.github.com/users/ndelage/gists{/gist_id}","starred_url":"https://api.github.com/users/ndelage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ndelage/subscriptions","organizations_url":"https://api.github.com/users/ndelage/orgs","repos_url":"https://api.github.com/users/ndelage/repos","events_url":"https://api.github.com/users/ndelage/events{/privacy}","received_events_url":"https://api.github.com/users/ndelage/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2016-06-07T22:10:22Z","updated_at":"2016-06-08T17:55:36Z","closed_at":"2016-06-08T17:55:36Z","body":"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.\r\n\r\nFortunately, 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.\r\n\r\nI made a few tweaks, but I'm still not there:\r\n\r\n```ruby\r\n# My \"user\" is a Lead, so I need to set a custom key prefix to prevent clashing with experiments made against users \r\nab_user = Split::User.new(Split::Persistence.adapter.new(nil, \"lead_id_#{lead.id}\")\r\n\r\nexperiment = Split::ExperimentCatalog.find_or_create(:welcome_message)\r\ntrial = Split::Trial.new(user: ab_user, experiment: experiment)\r\nputs trial.choose!\r\n```\r\n\r\nThe last line, `trial.choose!` fails because my `ab_user` isn't a `Split::User` and doesn't implement the method `. cleanup_old_experiments!`.\r\n\r\n```\r\nNoMethodError: undefined method `cleanup_old_experiments' for #<Split::Persistence::RedisAdapter:0x007f9980bbb450>\r\n```\r\n\r\nThe 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:\r\n\r\n```ruby\r\nmodule Split\r\n  class User\r\n    def initialize(context)\r\n      @user = Split::Persistence.adapter.new(context)\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nI can only provide the context which is useless to me, since I'm not in the context of a HTTP request.\r\n\r\nAny 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.\r\n\r\nFor now, I'm using this monkey patch:\r\n\r\n```ruby\r\nmodule Split\r\n  class User\r\n    def initialize(context, key=nil)\r\n      @user = Split::Persistence.adapter.new(context, key)\r\n    end\r\n  end\r\nend\r\n```"}}
{"actor":{"display_login":"andrew"}}