{"action":"created","issue":{"url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/140","repository_url":"https://api.github.com/repos/yiisoft/yii2-queue","labels_url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/140/labels{/name}","comments_url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/140/comments","events_url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/140/events","html_url":"https://github.com/yiisoft/yii2-queue/issues/140","id":260319182,"number":140,"title":"DB Driver: id and attempt properties not accessible during job execution","user":{"login":"vercotux","id":2930526,"avatar_url":"https://avatars1.githubusercontent.com/u/2930526?v=4","gravatar_id":"","url":"https://api.github.com/users/vercotux","html_url":"https://github.com/vercotux","followers_url":"https://api.github.com/users/vercotux/followers","following_url":"https://api.github.com/users/vercotux/following{/other_user}","gists_url":"https://api.github.com/users/vercotux/gists{/gist_id}","starred_url":"https://api.github.com/users/vercotux/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/vercotux/subscriptions","organizations_url":"https://api.github.com/users/vercotux/orgs","repos_url":"https://api.github.com/users/vercotux/repos","events_url":"https://api.github.com/users/vercotux/events{/privacy}","received_events_url":"https://api.github.com/users/vercotux/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2017-09-25T15:30:03Z","updated_at":"2017-11-30T06:27:48Z","closed_at":null,"author_association":"NONE","body":"### Problem\r\n\r\nWhen a job is being executed all it gets is the [`$queue` instance](https://github.com/yiisoft/yii2-queue/blob/7a8db9a3a934924fe49601c45b31849323898794/src/Queue.php#L190). It would be nice if the job could also access its own `id`, `attempt` number, and possibly other metadata during `execute()`. The metadata is there, it is simply not reachable from within a running job.\r\n\r\n### Use Case Examples\r\n\r\n * Logging. If we want to do logging during execution we might want to know exactly which job ID each log record belongs to.\r\n * Conditional execution based on the attempt number. For example if we are interacting with an API service we might wish to increase certain request delays after a previously failed job execution attempt.\r\n\r\n### Workaround\r\n\r\nA clumsy workaround for this is to attach an event handler to `Queue::EVENT_BEFORE_EXEC` which copies the $id and other relevant metadata from `ExecEvent` into the $job instance. Very inconvenient and not backwards-compatible with stock job interface, because we must ensure the job instances we use are able to receive these properties.\r\n\r\n### Ideas\r\n\r\n 1. Store current $id, $attempt, etc. in `Queue`. (probably bad for any kind of parallelism)\r\n 2. Add more parameters to call (eg. `$event->job->execute($this, $extra);`). Possible without modifying the job interface, but kind of hacky.\r\n 3. Same as above, but also modify the job interface to predict a second parameter for `execute()`. (breaks BC)\r\n"},"comment":{"url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/comments/348094788","html_url":"https://github.com/yiisoft/yii2-queue/issues/140#issuecomment-348094788","issue_url":"https://api.github.com/repos/yiisoft/yii2-queue/issues/140","id":348094788,"user":{"login":"zhuravljov","id":1656851,"avatar_url":"https://avatars0.githubusercontent.com/u/1656851?v=4","gravatar_id":"","url":"https://api.github.com/users/zhuravljov","html_url":"https://github.com/zhuravljov","followers_url":"https://api.github.com/users/zhuravljov/followers","following_url":"https://api.github.com/users/zhuravljov/following{/other_user}","gists_url":"https://api.github.com/users/zhuravljov/gists{/gist_id}","starred_url":"https://api.github.com/users/zhuravljov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/zhuravljov/subscriptions","organizations_url":"https://api.github.com/users/zhuravljov/orgs","repos_url":"https://api.github.com/users/zhuravljov/repos","events_url":"https://api.github.com/users/zhuravljov/events{/privacy}","received_events_url":"https://api.github.com/users/zhuravljov/received_events","type":"User","site_admin":false},"created_at":"2017-11-30T06:27:48Z","updated_at":"2017-11-30T06:27:48Z","author_association":"MEMBER","body":"One worker executes one job at once (by design). Therefore job execution always related with before and after exec events. Each worker creates own queue component instance, that will contain its behavior instance. Proposed example will work correctly.\r\n\r\n@rob006, yes, it is a hack. You are doing something wrong, if you forced to use it. I just suggest universal solution. But you can make ID property inside the job object and use it. It doesn't require using of individual behavior."}}
{"actor":{"display_login":"zhuravljov"}}