{"action":"created","issue":{"url":"https://api.github.com/repos/agronholm/apscheduler/issues/331","repository_url":"https://api.github.com/repos/agronholm/apscheduler","labels_url":"https://api.github.com/repos/agronholm/apscheduler/issues/331/labels{/name}","comments_url":"https://api.github.com/repos/agronholm/apscheduler/issues/331/comments","events_url":"https://api.github.com/repos/agronholm/apscheduler/issues/331/events","html_url":"https://github.com/agronholm/apscheduler/issues/331","id":350502446,"node_id":"MDU6SXNzdWUzNTA1MDI0NDY=","number":331,"title":"Flask application context is lost when using a persistent job store","user":{"login":"petargyurov","id":35771636,"node_id":"MDQ6VXNlcjM1NzcxNjM2","avatar_url":"https://avatars0.githubusercontent.com/u/35771636?v=4","gravatar_id":"","url":"https://api.github.com/users/petargyurov","html_url":"https://github.com/petargyurov","followers_url":"https://api.github.com/users/petargyurov/followers","following_url":"https://api.github.com/users/petargyurov/following{/other_user}","gists_url":"https://api.github.com/users/petargyurov/gists{/gist_id}","starred_url":"https://api.github.com/users/petargyurov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/petargyurov/subscriptions","organizations_url":"https://api.github.com/users/petargyurov/orgs","repos_url":"https://api.github.com/users/petargyurov/repos","events_url":"https://api.github.com/users/petargyurov/events{/privacy}","received_events_url":"https://api.github.com/users/petargyurov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-08-14T16:24:28Z","updated_at":"2018-08-15T10:35:05Z","closed_at":null,"author_association":"NONE","body":"## Expected Behavior\r\nBe able to store jobs whose functions are wrapped with the `app.app_context()` in a persistent job store like `SQLAlchemyJobStore`. **Note:** this all works fine for `MemoryJobStore`.\r\n\r\n## Current Behavior\r\nFunction reference cannot be resolved when providing a function that is wrapped in another; providing the textual reference does not aleviate the issue because then the application context is lost.\r\n\r\n## Detailed Description\r\nMy Flask application already utilises APScheduler's `MemoryJobStore` to run various tasks. I have a custom class of the scheduler that simply adds the application context to the equation:\r\n\r\n```\r\nclass CustomScheduler(object):\r\n\tdef __init__(self, app=None):\r\n\t\tself._scheduler = BackgroundScheduler()\r\n\t\tself.app = None\r\n\t\tif app:\r\n\t\tself.init_app(app)\r\n\t\r\n\t# stuff ...\r\n\r\n\tdef add_job(self, func, trigger=None, job_id=None, args=None, kwargs=None,\r\n                    replace_existing=False, **trigger_args):\r\n\t\t\r\n                    def wrapped(*args, **kwargs):\r\n\t\t\twith self.app.app_context():\r\n\t\t\t\tfunc(*args, **kwargs)\r\n\t\t\r\n\t\treturn self._scheduler.add_job(wrapped, id=job_id, trigger=trigger, args=args,\r\n\t\t\t\t\t       kwargs=kwargs, replace_existing=replace_existing,\r\n\t\t\t\t\t       **trigger_args)\r\n```\r\n\r\nThis works fine with the in-memory jobstore. In my views I can schedule jobs as expected. However, when I configure the scheduler with a `SQLAlchemyJobStore`, the behaviour changes. I get the error:\r\n\r\n`Error resolving reference my_app.custom.custom_scheduler:wrapped: error looking up object`\r\n\r\nOk, weird. Then trying with a textual reference will work right (i.e.: passing my function reference instead of `wrapped`)? Well, yes and no. The function is found and thus added successfully to the job store, but when the job runs it is missing the Flask application context and doesn't work properly.\r\n\r\nIt kind of makes sense to me that it works for `MemoryJobStore` and not for a persistent job store but I am not sure exactly why. \r\n\r\nAny ideas on how to reconcile this?"},"comment":{"url":"https://api.github.com/repos/agronholm/apscheduler/issues/comments/413159135","html_url":"https://github.com/agronholm/apscheduler/issues/331#issuecomment-413159135","issue_url":"https://api.github.com/repos/agronholm/apscheduler/issues/331","id":413159135,"node_id":"MDEyOklzc3VlQ29tbWVudDQxMzE1OTEzNQ==","user":{"login":"agronholm","id":130003,"node_id":"MDQ6VXNlcjEzMDAwMw==","avatar_url":"https://avatars0.githubusercontent.com/u/130003?v=4","gravatar_id":"","url":"https://api.github.com/users/agronholm","html_url":"https://github.com/agronholm","followers_url":"https://api.github.com/users/agronholm/followers","following_url":"https://api.github.com/users/agronholm/following{/other_user}","gists_url":"https://api.github.com/users/agronholm/gists{/gist_id}","starred_url":"https://api.github.com/users/agronholm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/agronholm/subscriptions","organizations_url":"https://api.github.com/users/agronholm/orgs","repos_url":"https://api.github.com/users/agronholm/repos","events_url":"https://api.github.com/users/agronholm/events{/privacy}","received_events_url":"https://api.github.com/users/agronholm/received_events","type":"User","site_admin":false},"created_at":"2018-08-15T10:35:05Z","updated_at":"2018-08-15T10:35:05Z","author_association":"OWNER","body":"Because MemoryJobStore does not serialize the jobs."}}
{"actor":{"display_login":"agronholm"}}