{"action":"created","comment":{"url":"https://api.github.com/repos/crest-cassia/oacis/pulls/comments/45029385","id":45029385,"diff_hunk":"@@ -132,6 +135,21 @@ def figure_files\n     list\n   end\n \n+  def destroyable?\n+    if runs.unscoped.empty?\n+      azr_ids = analyzers.unscoped.map {|azr| azr.id }\n+      Analysis.unscoped.where(:analyzer_id.in => azr_ids).empty?\n+    else\n+      false\n+    end\n+  end\n+\n+  def set_lower_submittable_to_be_destroyed","path":"app/models/simulator.rb","position":29,"original_position":29,"commit_id":"3de2a5a5b6c859600f856e9a8f8e8ba79aeaed9f","original_commit_id":"3de2a5a5b6c859600f856e9a8f8e8ba79aeaed9f","user":{"login":"yohm","id":718731,"avatar_url":"https://avatars.githubusercontent.com/u/718731?v=3","gravatar_id":"","url":"https://api.github.com/users/yohm","html_url":"https://github.com/yohm","followers_url":"https://api.github.com/users/yohm/followers","following_url":"https://api.github.com/users/yohm/following{/other_user}","gists_url":"https://api.github.com/users/yohm/gists{/gist_id}","starred_url":"https://api.github.com/users/yohm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yohm/subscriptions","organizations_url":"https://api.github.com/users/yohm/orgs","repos_url":"https://api.github.com/users/yohm/repos","events_url":"https://api.github.com/users/yohm/events{/privacy}","received_events_url":"https://api.github.com/users/yohm/received_events","type":"User","site_admin":false},"body":"方針は配下のSubmittableドキュメント (Run or Analysis) にto_be_destroyedフラグを立てるかどうかで判定しています。\r\nなので、PSやAnalyzerにはフラグをセットしていません。","created_at":"2015-11-17T07:38:22Z","updated_at":"2015-11-17T07:38:22Z","html_url":"https://github.com/crest-cassia/oacis/pull/394#discussion_r45029385","pull_request_url":"https://api.github.com/repos/crest-cassia/oacis/pulls/394","_links":{"self":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/comments/45029385"},"html":{"href":"https://github.com/crest-cassia/oacis/pull/394#discussion_r45029385"},"pull_request":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/394"}}},"pull_request":{"url":"https://api.github.com/repos/crest-cassia/oacis/pulls/394","id":50893557,"html_url":"https://github.com/crest-cassia/oacis/pull/394","diff_url":"https://github.com/crest-cassia/oacis/pull/394.diff","patch_url":"https://github.com/crest-cassia/oacis/pull/394.patch","issue_url":"https://api.github.com/repos/crest-cassia/oacis/issues/394","number":394,"state":"open","locked":false,"title":"Avoid conflict when destroying","user":{"login":"yohm","id":718731,"avatar_url":"https://avatars.githubusercontent.com/u/718731?v=3","gravatar_id":"","url":"https://api.github.com/users/yohm","html_url":"https://github.com/yohm","followers_url":"https://api.github.com/users/yohm/followers","following_url":"https://api.github.com/users/yohm/following{/other_user}","gists_url":"https://api.github.com/users/yohm/gists{/gist_id}","starred_url":"https://api.github.com/users/yohm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yohm/subscriptions","organizations_url":"https://api.github.com/users/yohm/orgs","repos_url":"https://api.github.com/users/yohm/repos","events_url":"https://api.github.com/users/yohm/events{/privacy}","received_events_url":"https://api.github.com/users/yohm/received_events","type":"User","site_admin":false},"body":"Runのステータスの更新のタイミングによってRunが適切にdestroyされない問題の修正\r\n\r\n### 問題点１\r\nRunのstatusを複数のプロセス（UIまたはCLIとworker）で同時に更新している。cancelledを使ってユーザーによって破棄リクエストが来たという情報を保持しているが、statusの更新のタイミングによって挙動が不安定になる。\r\n解決策としてdestroyのリクエストがあったことをstatusには保持しないようにする。代わりにto_be_destroyed: true というフラグをRunに付与するようにしてUIからはそのフラグをtrueに変更する処理だけを行う。workerでto_be_destroyedのRunを見て、実際にdestroyの処理を行う。\r\nRunのstatusに応じて、それぞれsubmitter, observer, service workerがdestroyするようにする。\r\n実際にdestroy処理をするのはworkerだけに限定することで複数プロセスの競合を防ぐようにする。 \r\n\r\n### 問題点２\r\n階層が上のドキュメントがdestroyされた時の挙動について。\r\nPSが削除された時にcallbackでRunやさらに配下のAnalysisのdestroyメソッドが呼ばれる。\r\n現状の仕様ではRunはcancelされた段階でparameter_set: nilをセットし、run_dirを削除している。そのようにしてdirectoryをクリアにした後に、workerによって削除処理をするようにしている。\r\nしかし、ここでも複数プロセスの競合の問題が発生しうる。\r\nworkerでRunを処理中にParameterSetが削除された場合、parameter_setを取得しようとしたところで例外が発生する。さらにディレクトリも削除された状態になり、ダウンロード中だった場合に不整合が起きうる。\r\n解決策としてParameterSet, Simulatorなどの上位の要素にもto_be_destroyedフラグをつけて、workerで処理させる。配下のRunまたはAnalysisが削除されたのちに上位の要素を削除する。\r\n\r\n## 仕様\r\n\r\n### 原則\r\nsubmittableのstatusに応じて状態を更新できるworkerを分ける。\r\n- created -> Submitter\r\n- submitted, running -> Observer\r\n- finished -> Service Worker\r\nUI,CLIのプロセスはドキュメントの削除を直接行うことはできない。to_be_destroyedをセットして後はworkerに任せる。\r\nstatusは必ずcreated -> submitted -> running -> finished or failed の順に更新されていくことになる。\r\n\r\nsubmittable を保持する上位のドキュメント(Simulator,PS,Analyzer,Run) については下層のsubmittableドキュメントが全て削除されてからdestroyを呼ぶ\r\nこれらのドキュメントにdestroyable? メソッドを追加し、trueの場合のみ削除。\r\nSimulator,PS,Analyzerの削除はservice workerの仕事とする。\r\n\r\nto_be_destroyedフラグをSimulator,ParameterSet,Run,Analysisにつける。削除できるdocumentをまとめると\r\n- submitter : Run(created)\r\n- observer : Run(submitted, running)\r\n- service : Run(finished, failed), ParameterSet, Simulator, Analyzer\r\nとなる。\r\n\r\n### 処理フロー\r\n- Simulator(PS,Analyzer)の削除がリクエストされた時\r\n  - Controller\r\n    - Simulatorのto_be_destroyedをtrueにする\r\n    - 配下のRun,Analysisが存在すればto_be_destroyedをtrueにする\r\n  - workerが配下のRun,Analysisを順番に削除していく\r\n  - 配下のRun,Analysisが全て削除し終わった段階で、workerがSimulator#destroyで削除。\r\n    - 配下のPSやAnalyzerも削除される。\r\n- Run(Analysis)の削除がリクエストされた時\r\n  - Controllerが\r\n    - Runのto_be_destroyedをtrueにする\r\n    - Analysisのto_be_destroyedもtrueにする\r\n  - 各workerが自分の担当するRunがto_be_destroyedかどうかを判定して削除する\r\n\r\n### 実装詳細\r\n- `default_scope ->{ where(:to_be_destroyed.in => [nil,false]) }` を各モデルに導入。\r\n  - 既存のコードから見ると、to_be_destroyed=trueの要素はすでに削除されたように見える\r\n- ある要素の配下の要素すべてにフラグをセットする時は、 `uppdate_all` を使って最適化\r\n","created_at":"2015-11-17T01:41:03Z","updated_at":"2015-11-17T07:38:22Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e97cc4cc79bb74f6958d93c2049493caf76b74d8","assignee":{"login":"takeshi-uchitane","id":5600507,"avatar_url":"https://avatars.githubusercontent.com/u/5600507?v=3","gravatar_id":"","url":"https://api.github.com/users/takeshi-uchitane","html_url":"https://github.com/takeshi-uchitane","followers_url":"https://api.github.com/users/takeshi-uchitane/followers","following_url":"https://api.github.com/users/takeshi-uchitane/following{/other_user}","gists_url":"https://api.github.com/users/takeshi-uchitane/gists{/gist_id}","starred_url":"https://api.github.com/users/takeshi-uchitane/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/takeshi-uchitane/subscriptions","organizations_url":"https://api.github.com/users/takeshi-uchitane/orgs","repos_url":"https://api.github.com/users/takeshi-uchitane/repos","events_url":"https://api.github.com/users/takeshi-uchitane/events{/privacy}","received_events_url":"https://api.github.com/users/takeshi-uchitane/received_events","type":"User","site_admin":false},"milestone":{"url":"https://api.github.com/repos/crest-cassia/oacis/milestones/17","html_url":"https://github.com/crest-cassia/oacis/milestones/v2.1.1","labels_url":"https://api.github.com/repos/crest-cassia/oacis/milestones/17/labels","id":1415016,"number":17,"title":"v2.1.1","description":"","creator":{"login":"yohm","id":718731,"avatar_url":"https://avatars.githubusercontent.com/u/718731?v=3","gravatar_id":"","url":"https://api.github.com/users/yohm","html_url":"https://github.com/yohm","followers_url":"https://api.github.com/users/yohm/followers","following_url":"https://api.github.com/users/yohm/following{/other_user}","gists_url":"https://api.github.com/users/yohm/gists{/gist_id}","starred_url":"https://api.github.com/users/yohm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/yohm/subscriptions","organizations_url":"https://api.github.com/users/yohm/orgs","repos_url":"https://api.github.com/users/yohm/repos","events_url":"https://api.github.com/users/yohm/events{/privacy}","received_events_url":"https://api.github.com/users/yohm/received_events","type":"User","site_admin":false},"open_issues":2,"closed_issues":0,"state":"open","created_at":"2015-11-17T01:41:23Z","updated_at":"2015-11-17T05:59:10Z","due_on":"2015-11-18T15:00:00Z","closed_at":null},"commits_url":"https://api.github.com/repos/crest-cassia/oacis/pulls/394/commits","review_comments_url":"https://api.github.com/repos/crest-cassia/oacis/pulls/394/comments","review_comment_url":"https://api.github.com/repos/crest-cassia/oacis/pulls/comments{/number}","comments_url":"https://api.github.com/repos/crest-cassia/oacis/issues/394/comments","statuses_url":"https://api.github.com/repos/crest-cassia/oacis/statuses/3de2a5a5b6c859600f856e9a8f8e8ba79aeaed9f","head":{"label":"crest-cassia:avoid_conflict_when_destroying","ref":"avoid_conflict_when_destroying","sha":"3de2a5a5b6c859600f856e9a8f8e8ba79aeaed9f","user":{"login":"crest-cassia","id":5803360,"avatar_url":"https://avatars.githubusercontent.com/u/5803360?v=3","gravatar_id":"","url":"https://api.github.com/users/crest-cassia","html_url":"https://github.com/crest-cassia","followers_url":"https://api.github.com/users/crest-cassia/followers","following_url":"https://api.github.com/users/crest-cassia/following{/other_user}","gists_url":"https://api.github.com/users/crest-cassia/gists{/gist_id}","starred_url":"https://api.github.com/users/crest-cassia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crest-cassia/subscriptions","organizations_url":"https://api.github.com/users/crest-cassia/orgs","repos_url":"https://api.github.com/users/crest-cassia/repos","events_url":"https://api.github.com/users/crest-cassia/events{/privacy}","received_events_url":"https://api.github.com/users/crest-cassia/received_events","type":"Organization","site_admin":false},"repo":{"id":13269212,"name":"oacis","full_name":"crest-cassia/oacis","owner":{"login":"crest-cassia","id":5803360,"avatar_url":"https://avatars.githubusercontent.com/u/5803360?v=3","gravatar_id":"","url":"https://api.github.com/users/crest-cassia","html_url":"https://github.com/crest-cassia","followers_url":"https://api.github.com/users/crest-cassia/followers","following_url":"https://api.github.com/users/crest-cassia/following{/other_user}","gists_url":"https://api.github.com/users/crest-cassia/gists{/gist_id}","starred_url":"https://api.github.com/users/crest-cassia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crest-cassia/subscriptions","organizations_url":"https://api.github.com/users/crest-cassia/orgs","repos_url":"https://api.github.com/users/crest-cassia/repos","events_url":"https://api.github.com/users/crest-cassia/events{/privacy}","received_events_url":"https://api.github.com/users/crest-cassia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crest-cassia/oacis","description":"","fork":false,"url":"https://api.github.com/repos/crest-cassia/oacis","forks_url":"https://api.github.com/repos/crest-cassia/oacis/forks","keys_url":"https://api.github.com/repos/crest-cassia/oacis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crest-cassia/oacis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crest-cassia/oacis/teams","hooks_url":"https://api.github.com/repos/crest-cassia/oacis/hooks","issue_events_url":"https://api.github.com/repos/crest-cassia/oacis/issues/events{/number}","events_url":"https://api.github.com/repos/crest-cassia/oacis/events","assignees_url":"https://api.github.com/repos/crest-cassia/oacis/assignees{/user}","branches_url":"https://api.github.com/repos/crest-cassia/oacis/branches{/branch}","tags_url":"https://api.github.com/repos/crest-cassia/oacis/tags","blobs_url":"https://api.github.com/repos/crest-cassia/oacis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crest-cassia/oacis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crest-cassia/oacis/git/refs{/sha}","trees_url":"https://api.github.com/repos/crest-cassia/oacis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crest-cassia/oacis/statuses/{sha}","languages_url":"https://api.github.com/repos/crest-cassia/oacis/languages","stargazers_url":"https://api.github.com/repos/crest-cassia/oacis/stargazers","contributors_url":"https://api.github.com/repos/crest-cassia/oacis/contributors","subscribers_url":"https://api.github.com/repos/crest-cassia/oacis/subscribers","subscription_url":"https://api.github.com/repos/crest-cassia/oacis/subscription","commits_url":"https://api.github.com/repos/crest-cassia/oacis/commits{/sha}","git_commits_url":"https://api.github.com/repos/crest-cassia/oacis/git/commits{/sha}","comments_url":"https://api.github.com/repos/crest-cassia/oacis/comments{/number}","issue_comment_url":"https://api.github.com/repos/crest-cassia/oacis/issues/comments{/number}","contents_url":"https://api.github.com/repos/crest-cassia/oacis/contents/{+path}","compare_url":"https://api.github.com/repos/crest-cassia/oacis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crest-cassia/oacis/merges","archive_url":"https://api.github.com/repos/crest-cassia/oacis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crest-cassia/oacis/downloads","issues_url":"https://api.github.com/repos/crest-cassia/oacis/issues{/number}","pulls_url":"https://api.github.com/repos/crest-cassia/oacis/pulls{/number}","milestones_url":"https://api.github.com/repos/crest-cassia/oacis/milestones{/number}","notifications_url":"https://api.github.com/repos/crest-cassia/oacis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crest-cassia/oacis/labels{/name}","releases_url":"https://api.github.com/repos/crest-cassia/oacis/releases{/id}","created_at":"2013-10-02T11:20:13Z","updated_at":"2015-10-13T05:04:47Z","pushed_at":"2015-11-17T05:59:10Z","git_url":"git://github.com/crest-cassia/oacis.git","ssh_url":"git@github.com:crest-cassia/oacis.git","clone_url":"https://github.com/crest-cassia/oacis.git","svn_url":"https://github.com/crest-cassia/oacis","homepage":"http://crest-cassia.github.io/oacis/","size":7182,"stargazers_count":11,"watchers_count":11,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":25,"forks":1,"open_issues":25,"watchers":11,"default_branch":"Development"}},"base":{"label":"crest-cassia:Development","ref":"Development","sha":"01099d403aee4eeb51e1626f5dd9207beef8cc8b","user":{"login":"crest-cassia","id":5803360,"avatar_url":"https://avatars.githubusercontent.com/u/5803360?v=3","gravatar_id":"","url":"https://api.github.com/users/crest-cassia","html_url":"https://github.com/crest-cassia","followers_url":"https://api.github.com/users/crest-cassia/followers","following_url":"https://api.github.com/users/crest-cassia/following{/other_user}","gists_url":"https://api.github.com/users/crest-cassia/gists{/gist_id}","starred_url":"https://api.github.com/users/crest-cassia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crest-cassia/subscriptions","organizations_url":"https://api.github.com/users/crest-cassia/orgs","repos_url":"https://api.github.com/users/crest-cassia/repos","events_url":"https://api.github.com/users/crest-cassia/events{/privacy}","received_events_url":"https://api.github.com/users/crest-cassia/received_events","type":"Organization","site_admin":false},"repo":{"id":13269212,"name":"oacis","full_name":"crest-cassia/oacis","owner":{"login":"crest-cassia","id":5803360,"avatar_url":"https://avatars.githubusercontent.com/u/5803360?v=3","gravatar_id":"","url":"https://api.github.com/users/crest-cassia","html_url":"https://github.com/crest-cassia","followers_url":"https://api.github.com/users/crest-cassia/followers","following_url":"https://api.github.com/users/crest-cassia/following{/other_user}","gists_url":"https://api.github.com/users/crest-cassia/gists{/gist_id}","starred_url":"https://api.github.com/users/crest-cassia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/crest-cassia/subscriptions","organizations_url":"https://api.github.com/users/crest-cassia/orgs","repos_url":"https://api.github.com/users/crest-cassia/repos","events_url":"https://api.github.com/users/crest-cassia/events{/privacy}","received_events_url":"https://api.github.com/users/crest-cassia/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/crest-cassia/oacis","description":"","fork":false,"url":"https://api.github.com/repos/crest-cassia/oacis","forks_url":"https://api.github.com/repos/crest-cassia/oacis/forks","keys_url":"https://api.github.com/repos/crest-cassia/oacis/keys{/key_id}","collaborators_url":"https://api.github.com/repos/crest-cassia/oacis/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/crest-cassia/oacis/teams","hooks_url":"https://api.github.com/repos/crest-cassia/oacis/hooks","issue_events_url":"https://api.github.com/repos/crest-cassia/oacis/issues/events{/number}","events_url":"https://api.github.com/repos/crest-cassia/oacis/events","assignees_url":"https://api.github.com/repos/crest-cassia/oacis/assignees{/user}","branches_url":"https://api.github.com/repos/crest-cassia/oacis/branches{/branch}","tags_url":"https://api.github.com/repos/crest-cassia/oacis/tags","blobs_url":"https://api.github.com/repos/crest-cassia/oacis/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/crest-cassia/oacis/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/crest-cassia/oacis/git/refs{/sha}","trees_url":"https://api.github.com/repos/crest-cassia/oacis/git/trees{/sha}","statuses_url":"https://api.github.com/repos/crest-cassia/oacis/statuses/{sha}","languages_url":"https://api.github.com/repos/crest-cassia/oacis/languages","stargazers_url":"https://api.github.com/repos/crest-cassia/oacis/stargazers","contributors_url":"https://api.github.com/repos/crest-cassia/oacis/contributors","subscribers_url":"https://api.github.com/repos/crest-cassia/oacis/subscribers","subscription_url":"https://api.github.com/repos/crest-cassia/oacis/subscription","commits_url":"https://api.github.com/repos/crest-cassia/oacis/commits{/sha}","git_commits_url":"https://api.github.com/repos/crest-cassia/oacis/git/commits{/sha}","comments_url":"https://api.github.com/repos/crest-cassia/oacis/comments{/number}","issue_comment_url":"https://api.github.com/repos/crest-cassia/oacis/issues/comments{/number}","contents_url":"https://api.github.com/repos/crest-cassia/oacis/contents/{+path}","compare_url":"https://api.github.com/repos/crest-cassia/oacis/compare/{base}...{head}","merges_url":"https://api.github.com/repos/crest-cassia/oacis/merges","archive_url":"https://api.github.com/repos/crest-cassia/oacis/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/crest-cassia/oacis/downloads","issues_url":"https://api.github.com/repos/crest-cassia/oacis/issues{/number}","pulls_url":"https://api.github.com/repos/crest-cassia/oacis/pulls{/number}","milestones_url":"https://api.github.com/repos/crest-cassia/oacis/milestones{/number}","notifications_url":"https://api.github.com/repos/crest-cassia/oacis/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/crest-cassia/oacis/labels{/name}","releases_url":"https://api.github.com/repos/crest-cassia/oacis/releases{/id}","created_at":"2013-10-02T11:20:13Z","updated_at":"2015-10-13T05:04:47Z","pushed_at":"2015-11-17T05:59:10Z","git_url":"git://github.com/crest-cassia/oacis.git","ssh_url":"git@github.com:crest-cassia/oacis.git","clone_url":"https://github.com/crest-cassia/oacis.git","svn_url":"https://github.com/crest-cassia/oacis","homepage":"http://crest-cassia.github.io/oacis/","size":7182,"stargazers_count":11,"watchers_count":11,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":true,"forks_count":1,"mirror_url":null,"open_issues_count":25,"forks":1,"open_issues":25,"watchers":11,"default_branch":"Development"}},"_links":{"self":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/394"},"html":{"href":"https://github.com/crest-cassia/oacis/pull/394"},"issue":{"href":"https://api.github.com/repos/crest-cassia/oacis/issues/394"},"comments":{"href":"https://api.github.com/repos/crest-cassia/oacis/issues/394/comments"},"review_comments":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/394/comments"},"review_comment":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/crest-cassia/oacis/pulls/394/commits"},"statuses":{"href":"https://api.github.com/repos/crest-cassia/oacis/statuses/3de2a5a5b6c859600f856e9a8f8e8ba79aeaed9f"}}}}