{"id":630,"date":"2008-12-03T16:36:35","date_gmt":"2008-12-03T07:36:35","guid":{"rendered":"http:\/\/ecpplus.net\/weblog\/?p=630"},"modified":"2021-07-15T14:31:16","modified_gmt":"2021-07-15T05:31:16","slug":"rails-lock-sequential","status":"publish","type":"post","link":"https:\/\/ecpplus.net\/weblog\/rails-lock-sequential\/","title":{"rendered":"Rails\u3067\u540c\u3058\u51e6\u7406\u304c\u4e26\u884c\u3057\u3066\u8d70\u3089\u306a\u3044\u3088\u3046\u306b\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>Rails \u306e\u30d0\u30c3\u30c1\u51e6\u7406\u306a\u3069\u3067\u3001\u8907\u6570\u30d7\u30ed\u30bb\u30b9\u3092\u307e\u305f\u3044\u3067\u540c\u3058\u51e6\u7406\u3092\u4e26\u884c\u3057\u3066\u8d70\u3089\u305b\u305f\u304f\u306a\u3044\u3068\u304d\u3001\u5358\u4e00\u30b5\u30fc\u30d0\u3067\u3042\u308c\u3070\u30d5\u30a1\u30a4\u30eb\u306e\u30ed\u30c3\u30af\u3092\u4f7f\u3046\u306e\u304c\u7c21\u5358\u3067\u3059\u3002<\/p>\n<h2>\u30bd\u30fc\u30b9<\/h2>\n<p><code>#{RAILS_ROOT}\/lib\/batch_lock.rb<\/code> \u3068\u3044\u3046\u540d\u524d\u3067\u4e0b\u8a18\u3092\u4fdd\u5b58\u3059\u308b\u3002<\/p>\n<pre><code class=\"language-ruby\"># \u540c\u4e00\u51e6\u7406\u3092\u4e26\u884c\u3067\u8d70\u3089\u305b\u306a\u3044\u69d8\u306b\u3059\u308b\u30e2\u30b8\u30e5\u30fc\u30eb\nclass BatchLockedException &lt; Exception; end\n\nmodule BatchLock\n  # \u51e6\u7406\u3092\u6392\u4ed6\u30ed\u30c3\u30af\u3057\u3066\u8d70\u3089\u305b\u308b\n  def self.run(batch_name)\n    FileUtils.mkdir_p(&quot;#{Rails.root}\/tmp\/batch_lock&quot;)\n\n    # batch_name \u3092\u30b9\u30b3\u30fc\u30d7\u3068\u3057\u3066\u6392\u4ed6\u51e6\u7406\u3055\u308c\u308b\u3002\n    File.open(&quot;#{Rails.root}\/tmp\/batch_lock\/#{batch_name}&quot;, &#039;w&#039;) do |f|\n      if f.flock(File::LOCK_EX | File::LOCK_NB)\n        yield\n      else\n        raise BatchLockedException.new\n      end\n    end\n  end\n\n  # \u51e6\u7406\u304c\u8d70\u3063\u3066\u3044\u308b\u304b\u78ba\u8a8d\u3059\u308b\n  def self.running?(batch_name)\n    FileUtils.mkdir_p(&quot;#{Rails.root}\/tmp\/batch_lock&quot;)\n\n    # \u30ed\u30c3\u30af\u7528\u306e\u30d5\u30a1\u30a4\u30eb\u540d\u306f\u3001\u30d0\u30c3\u30c1\u3054\u3068\u306b\u30e6\u30cb\u30fc\u30af\u306b\u3059\u308b\u3002\n    File.open(&quot;#{Rails.root}\/tmp\/batch_lock\/#{batch_name}&quot;, &#039;w&#039;) do |f|\n      !f.flock(File::LOCK_EX | File::LOCK_NB)\n    end\n  end\nend<\/code><\/pre>\n<h2>\u4f7f\u3044\u65b9<\/h2>\n<p>\u540c\u6642\u306b\u8d70\u3089\u305b\u305f\u304f\u306a\u3044\u51e6\u7406\u3092\u30d6\u30ed\u30c3\u30af\u3067\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre><code class=\"language-ruby\">begin\n  BatchLock.run(:go_to_park) do\n    # \u30e6\u30fc\u30b6\u304c\u307f\u3093\u306a\u516c\u5712\u306b\u884c\u304f\n    User.each(&amp;:go_to_park)\n  end\nrescue BatchLockedException\n  Rails.logger.error &quot;\u65e2\u306b\u30d0\u30c3\u30c1\u304c\u8d70\u3063\u3066\u3044\u307e\u3059&quot;\nend<\/code><\/pre>\n<p>\u4eca\u30d0\u30c3\u30c1\u304c\u8d70\u3063\u3066\u308b\u304b\u78ba\u8a8d\u3059\u308b\u3002<\/p>\n<pre><code class=\"language-ruby\">puts BatchLock.running?(:go_to_park) ? &#039;\u5b9f\u884c\u4e2d&#039; : &#039;\u5b9f\u884c\u4e2d\u3067\u306f\u3042\u308a\u307e\u305b\u3093&#039;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Rails \u306e\u30d0\u30c3\u30c1\u51e6\u7406\u306a\u3069\u3067\u3001\u8907\u6570\u30d7\u30ed\u30bb\u30b9\u3092\u307e\u305f\u3044\u3067\u540c\u3058\u51e6\u7406\u3092\u4e26\u884c\u3057\u3066\u8d70\u3089\u305b\u305f\u304f\u306a\u3044\u3068\u304d\u3001\u5358\u4e00\u30b5\u30fc\u30d0\u3067\u3042\u308c\u3070\u30d5\u30a1\u30a4\u30eb\u306e\u30ed\u30c3\u30af\u3092\u4f7f\u3046\u306e\u304c\u7c21\u5358\u3067\u3059\u3002 \u30bd\u30fc\u30b9 #{RAILS_ROOT}\/lib\/batch_lock.rb  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2521,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[10],"class_list":["post-630","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development","tag-rails"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/630","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/comments?post=630"}],"version-history":[{"count":3,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/630\/revisions"}],"predecessor-version":[{"id":4337,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/posts\/630\/revisions\/4337"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media\/2521"}],"wp:attachment":[{"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/media?parent=630"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/categories?post=630"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ecpplus.net\/weblog\/wp-json\/wp\/v2\/tags?post=630"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}