Tambourine作業メモ

主にスキル習得のためにやった作業のメモ。他人には基本的に無用のものです。

Herokuのしょぼいアプリが動かなくなっていたので対応した

何年か前の話だが、某所で某F1関係者がFIAが提供しているラップチャートのPDFから、グラフを書くためにExcelに各ドライバーのラップタイムをコピペするのがめんどいと愚痴っているのを聞き、PDFを読んでCSVを返すRubyスクリプトを作って提供することにした。とはいえ、Rubyスクリプトを実行してねというのも乱暴で、どこかでWebツールとして提供するのが良かろうということになり、CSSもなんにもないWeb画面で各グランプリをリストし、クリックするとCSVがダウンロードされるだけの手抜きアプリを作り、Herokuの無料枠で提供していた(事実上、ユーザーが1名しかいないアプリなので、それで十分だった)。Herokuのガイドに従って、sinatraで制御してた。

PDFの入手リンク先を毎戦確認したり、FIAが気分で変えたPDFのフォーマットに対応したりで、ちょいちょいメンテナンスが発生したりはするものの概ね機嫌良く動いてはいた。だが、Rubyのバージョンが古くなって更新しろよと警告がでていたりしたのだが、放置していた。というのも、Herokuは無料枠がなくなってしまうということがあって、どこかに引っ越さなくてはいけないなと思っていたから。しかし、ここのところ仕事に追いまくられて対応出来ず、ついに今週は2023年の開幕戦である。Herokuのまましばらく運用するほかない。

というわけで、去年の最終戦以来、アプリをビルドしてみたところ、動かない。ごちゃごちゃといろいろ対応する必要があったので、メモしておく。

> git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 378 bytes | 378.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: !   Your app is using the Heroku-18 stack, which reaches end-of-life on April 30th, 2023.
remote: !   
remote: !   Builds on Heroku-18 will not be possible from May 1st, 2023.
remote: !   
remote: !   Please upgrade the stack of your app as soon as possible:
remote: !   https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote: !   
remote: !   Heroku-18 EOL FAQ: https://help.heroku.com/X5OE6BCA/heroku-18-end-of-life-faq
remote: !   
remote: !   THIS IS A ONE-OFF NOTICE. THE NEXT DEPLOY WILL FUNCTION WITHOUT INTERRUPTION.
remote: !   
remote: !   The next warning of this kind will occur in: 7 day(s).
remote: 
To https://git.heroku.com/lit-taiga-21087.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lit-taiga-21087.git'

Heroku-18スタックはもう使えないから移行しろということらしい。エラーメッセージに出ているFAQを読むと新しいディレクトリに新しいスタック用にアプリを組み立てて・・・となっていて「おおぅ」となった。

が、HerokuのWeb管理画面にいくと以下のようになっていて、ボタンを押せば更新が走ってしまうようだ。私のしょぼいアプリはこれで問題なかった。

> git push
Enumerating objects: 7, done.
Counting objects: 100% (7/7), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 378 bytes | 378.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-22 stack
remote: -----> Using buildpack: heroku/ruby
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rack
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://heroku-buildpack-ruby.s3.us-east-1.amazonaws.com/heroku-22/ruby-2.7.1.tgz -s -o - | tar zxf - ' failed on attempt 1 of 3.
remote:        Command: 'set -o pipefail; curl -L --fail --retry 5 --retry-delay 1 --connect-timeout 3 --max-time 30 https://heroku-buildpack-ruby.s3.us-east-1.amazonaws.com/heroku-22/ruby-2.7.1.tgz -s -o - | tar zxf - ' failed on attempt 2 of 3.
remote: 
remote:  !
remote:  !     The Ruby version you are trying to install does not exist on this stack.
remote:  !     
remote:  !     You are trying to install ruby-2.7.1 on heroku-22.
remote:  !     
remote:  !     Ruby ruby-2.7.1 is present on the following stacks:
remote:  !     
remote:  !     - heroku-18
remote:  !     - heroku-20
remote:  !     
remote:  !     Heroku recommends you use the latest supported Ruby version listed here:
remote:  !     https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
remote:  !     
remote:  !     For more information on syntax for declaring a Ruby version see:
remote:  !     https://devcenter.heroku.com/articles/ruby-versions
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to lit-taiga-21087.
remote: 
To https://git.heroku.com/lit-taiga-21087.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lit-taiga-21087.git'

次はRubyが古いと言われている。単純にGemfileのバージョンを3.1.3に書き換えて、Gemfile.lockを消して(する必要があるのか理解出来てない)、bundle installする。

> bundle install
rbenv: version `2.7.1' is not installed (set by /Users/tambara/F1_lap_analysis/.ruby-version)

rbenvでRubyバージョンを固定してあった。パソコンの移行後は手元に2.7.1が入ってない状態。普段、PDF入手先を書き換えてPUSHするだけなのでローカルにRubyが入ってなくてもメンテ出来ていたようだ。

> rbenv local 3.1.3

を実行しておいて、

> bundle install
Fetching https://github.com/sinatra/sinatra.git
Fetching gem metadata from https://rubygems.org/.............
Resolving dependencies...
Using rake 13.0.6
Fetching afm 0.2.2
Using bundler 2.3.26
Fetching ffi 1.15.5
Fetching Ascii85 1.1.0
Fetching diff-lcs 1.5.0
Fetching coderay 1.1.3
Fetching formatador 1.1.0
Fetching rb-fsevent 0.11.2
Fetching lumberjack 1.2.8
Installing formatador 1.1.0
Installing Ascii85 1.1.0
Installing afm 0.2.2
Installing lumberjack 1.2.8
Installing diff-lcs 1.5.0
Installing rb-fsevent 0.11.2
Installing coderay 1.1.3
Fetching nenv 0.3.0
Fetching shellany 0.0.1
Fetching method_source 1.0.0
Fetching thor 1.2.1
Fetching guard-compat 1.2.1
Fetching minitest 5.17.0
Fetching hashery 2.1.2
Installing shellany 0.0.1
Fetching multi_json 1.15.0
Installing nenv 0.3.0
Installing method_source 1.0.0
Using ruby2_keywords 0.0.5
Fetching ruby-rc4 0.1.5
Installing guard-compat 1.2.1
Fetching ttfunk 1.7.0
Fetching rack 2.2.6.2
Installing ffi 1.15.5 with native extensions
Installing thor 1.2.1
Fetching rspec-support 3.12.0
Installing hashery 2.1.2
Fetching tilt 2.1.0
Installing minitest 5.17.0
Fetching notiffany 0.1.3
Installing ruby-rc4 0.1.5
Fetching mustermann 3.0.0
Installing multi_json 1.15.0
Fetching pry 0.14.2
Installing ttfunk 1.7.0
Installing tilt 2.1.0
Fetching guard-minitest 2.4.6
Installing notiffany 0.1.3
Fetching pdf-reader 2.11.0
Installing rack 2.2.6.2
Fetching rack-test 2.0.2
Using rack-protection 3.0.5 from https://github.com/sinatra/sinatra.git (at master@4fc73c3)
Installing rspec-support 3.12.0
Fetching rspec-core 3.12.1
Fetching rspec-expectations 3.12.2
Installing mustermann 3.0.0
Fetching rspec-mocks 3.12.3
Installing guard-minitest 2.4.6
Using sinatra 3.0.5 from https://github.com/sinatra/sinatra.git (at master@4fc73c3)
Fetching sinatra-contrib 3.0.5
Installing pry 0.14.2
Installing rack-test 2.0.2
Installing rspec-expectations 3.12.2
Installing pdf-reader 2.11.0
Installing rspec-core 3.12.1
Installing sinatra-contrib 3.0.5
Installing rspec-mocks 3.12.3
Fetching rspec 3.12.0
Installing rspec 3.12.0
Fetching rb-inotify 0.10.1
Installing rb-inotify 0.10.1
Fetching listen 3.8.0
Installing listen 3.8.0
Fetching guard 2.18.0
Installing guard 2.18.0
Bundle complete! 9 Gemfile dependencies, 40 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.

改めて、ビルドするためにgit pushする。

> git push
Enumerating objects: 14, done.
Counting objects: 100% (14/14), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (9/9), 1.23 KiB | 1.23 MiB/s, done.
Total 9 (delta 5), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-22 stack
remote: -----> Using buildpack: heroku/ruby
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rack
remote: -----> Using Ruby version: ruby-3.1.3
remote:        Purging Cache. Changing stack from heroku-18 to heroku-22
remote: -----> Installing dependencies using bundler 2.3.25
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Your bundle only supports platforms ["arm64-darwin-22"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with
remote:        `bundle lock --add-platform x86_64-linux` and try again.
remote:        Bundler Output: Your bundle only supports platforms ["arm64-darwin-22"] but your local platform
remote:        is x86_64-linux. Add the current platform to the lockfile with
remote:        `bundle lock --add-platform x86_64-linux` and try again.
remote: 
remote:  !
remote:  !     Failed to install gems via Bundler.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !   Push rejected to lit-taiga-21087.
remote: 
To https://git.heroku.com/lit-taiga-21087.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/lit-taiga-21087.git'

プラットフォーム設定がおかしいと。親切にコマンドまで提示されているので、その通りやる。

> bundle lock --add-platform x86_64-linux
Fetching gem metadata from https://rubygems.org/............
Resolving dependencies...
Writing lockfile to /Users/tambara/F1_lap_analysis/lit-taiga-21087/Gemfile.lock

Gemfile.lockをコミットして、再ビルド

> git push
Enumerating objects: 19, done.
Counting objects: 100% (19/19), done.
Delta compression using up to 8 threads
Compressing objects: 100% (13/13), done.
Writing objects: 100% (14/14), 1.66 KiB | 1.66 MiB/s, done.
Total 14 (delta 8), reused 0 (delta 0), pack-reused 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-22 stack
remote: -----> Using buildpack: heroku/ruby
remote: -----> Ruby app detected
remote: -----> Installing bundler 2.3.25
remote: -----> Removing BUNDLED WITH version in the Gemfile.lock
remote: -----> Compiling Ruby/Rack
remote: -----> Using Ruby version: ruby-3.1.3
remote:        Purging Cache. Changing stack from heroku-18 to heroku-22
remote: -----> Installing dependencies using bundler 2.3.25
remote:        Running: BUNDLE_WITHOUT='development:test' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4
remote:        Fetching https://github.com/sinatra/sinatra.git
remote:        Fetching gem metadata from https://rubygems.org/............
remote:        Fetching rake 13.0.6
remote:        Installing rake 13.0.6
remote:        Using bundler 2.3.26
remote:        Fetching hashery 2.1.2
remote:        Fetching Ascii85 1.1.0
remote:        Fetching afm 0.2.2
remote:        Fetching multi_json 1.15.0
remote:        Installing Ascii85 1.1.0
remote:        Installing hashery 2.1.2
remote:        Installing multi_json 1.15.0
remote:        Using ruby2_keywords 0.0.5
remote:        Fetching ruby-rc4 0.1.5
remote:        Installing afm 0.2.2
remote:        Fetching ttfunk 1.7.0
remote:        Installing ruby-rc4 0.1.5
remote:        Fetching rack 2.2.6.2
remote:        Fetching tilt 2.1.0
remote:        Installing ttfunk 1.7.0
remote:        Fetching mustermann 3.0.0
remote:        Installing mustermann 3.0.0
remote:        Installing tilt 2.1.0
remote:        Installing rack 2.2.6.2
remote:        Fetching pdf-reader 2.11.0
remote:        Using rack-protection 3.0.5 from https://github.com/sinatra/sinatra.git (at master@4fc73c3)
remote:        Using sinatra 3.0.5 from https://github.com/sinatra/sinatra.git (at master@4fc73c3)
remote:        Fetching sinatra-contrib 3.0.5
remote:        Installing pdf-reader 2.11.0
remote:        Installing sinatra-contrib 3.0.5
remote:        Bundle complete! 9 Gemfile dependencies, 16 gems now installed.
remote:        Gems in the groups 'development' and 'test' were not installed.
remote:        Bundled gems are installed into `./vendor/bundle`
remote:        Bundle completed (3.57s)
remote:        Cleaning up the bundler cache.
remote:        Removing bundler (2.3.25)
remote: -----> Detecting rake tasks
remote: 
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types     -> web
remote:        Default types for buildpack -> console, rake
remote: 
remote: -----> Compressing...
remote:        Done: 57.2M
remote: -----> Launching...
remote:        Released v127
remote:        https://lit-taiga-21087.herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
To https://git.heroku.com/lit-taiga-21087.git
   6cef8e7..515d352  master -> master

とりあえず、これでビルドは通ったっぽい。しかしながらアクセスするとエラー画面がでる。ログを見てみる。

> heroku logs --tail
 (略)
2023-02-28T02:55:40.608799+00:00 heroku[router]: at=error code=H14 desc="No web processes running" method=GET path="/" host=lit-taiga-21087.herokuapp.com request_id=2273cf84-1e3a-48e1-a980-24304729662d fwd="129.41.57.0" dyno= connect= service= status=503 bytes= protocol=https

H14はdynoが起動してないぞということらしい。このコマンドを打てとガイドされているものを素直にやる。

> heroku ps:scale web=1
 ›   Warning: heroku update available from 7.66.4 to 7.68.2.
Scaling dynos... !
 ▸    Item Could not be Updated: The app owner has to subscribe to Eco to scale your dynos. Learn more at https://blog.heroku.com/new-low-cost-plans

金を払えということのようだ。やっとここまでたどり着いた。満を持して、Herokuにクレジットカードを登録して、金を払う。そして再実行。

> heroku ps:scale web=1
 ›   Warning: heroku update available from 7.66.4 to 7.68.2.
Scaling dynos... done, now running web at 1:Eco

これでよさげ。再度、アクセスしてみるがまだエラー。

> heroku logs --tail
(略)
2023-02-28T03:01:54.590932+00:00 app[api]: Scaled to console@0:Eco rake@0:Eco web@1:Eco
2023-02-28T03:01:57.043327+00:00 heroku[web.1]: Starting process with command `bundle exec ruby main.rb -p 7213`
2023-02-28T03:01:58.289070+00:00 app[web.1]: /app/vendor/bundle/ruby/3.1.0/gems/rack-2.2.6.2/lib/rack/handler.rb:45:in `pick': Couldn't find handler for: thin, falcon, puma, HTTP, webrick. (LoadError)
(略)

rackがエラーを出している。ググると、Ruby3.0でwebrickが標準gemじゃなくなったことが原因とのこと。Gemfileにpumaを足して再チャレンジ。

これで復活した。いろいろあったけど、全部だいたいググったら先人がちゃんと記録を残してくれていて、英語をほとんど読むこともなく対応できた。感謝しかない。