スリ飯屋MaLankaのフリーエンジニアな日々

このブログでは、フリーランス5期目となる自身の実体験から、フリーランスエンジニアになるためのノウハウ、ブログや沖縄移住、スリランカの最新情報について発信します。

【2023年1月最新】Herokuから代替のRailway.appへRailsアプリ移行時に出たエラー【無料廃止】

※記事内に提携先企業のアフィリエイト広告(リンク、バナー等)、Google広告を含む場合があります

【無料廃止】Herokuから代替のRailway.appへRailsアプリ移行時に出たエラー


こんにちは、現役沖縄フリーランスエンジニアのmahです。


このブログでは、

僕がIT未経験から約1年でフリーランスエンジニアになるまでの過程、

ノウハウなどを書いていきます。


今回は、


  • 【2023年1月最新】Herokuから代替のRailway.appへRailsアプリ移行時に出たエラー【無料廃止】


についてです。


2022年8月25日、

Herokuが無料枠廃止のアナウンスを出しました。

blog.heroku.com



「Heroku無料枠終了か...代替サービス探さないと」

「Heroku無料廃止は困る...みんな移行先はどこにするのかな」

「無料で使えるHerokuの代用サービスってないのかな」

こんな感じで困っている方が多いと思います。


自分も、

日常的に動かしているスケジューラが結構あるので対応しないといけません...


有料でHerokuを使い続けても良いのですが、

最低でも月16ドルかかります。


円安が激しくなっているのでこれは最後の手段にするとして、

一旦無料で使えるサービスを探すことにしました。


Render、Fly.ioなど色々と代替サービスとなるPassはあったのですが、

スムーズに移行出来そう && 無料枠(月500時間、月5ドルまで無料枠)の内容から Railway.appを採用。


railway.app


docs.railway.app


今回、

HerokuからRailway.appへRailsアプリ移行過程で割とエラーが出たのでメモがてら記事にしました。


「Railway.appに移行したいけどエラーが出まくってイヤになりそう」

という方の参考になれば幸いです。


【2023年1月最新】Herokuから代替のRailway.appへRailsアプリ移行時に出たエラー【無料廃止】



今書いている以外にもエラーはあるのですが、

まとまっていないため随時追記していきます。


Errno::ENOENT: No such file or directory @ rb_sysopen - tmp/pids/server.pid


Railway.appデプロイ時に発生。

Errno::ENOENT: No such file or directory @ rb_sysopen - tmp/pids/server.pid 


build時に tmp/pids ディレクトリを作るようにすればOKです。


Dashboard > Settings > Build Commandに mkdir -p tmp/pids を追記します。

(デフォルトは bundle exec puma -C config/puma.rb のみ )

# mkdir -p tmp/pids を追加

$ mkdir -p tmp/pids && bundle exec puma -C config/puma.rb


railway tmp/pidsディレクトリ追加 start command


To allow requests to xxx, add the following to your environment configuration:


デプロイ後に本番のページを開くと発生。

To allow requests to xxx, add the following to your environment configuration:


Rails6系から追加された DNS rebinding attacks(DNSリバインディング攻撃)への対策です。

github.com


config/environments/xxx.rbの、

config.hostsにデプロイしたアプリのURIを入れればOK。

config.hosts << "web-production-xxx.up.railway.app"


The engine "node" is incompatible with this module. Expected version "v14.0.0". Got "16.17.0"


Railway.appデプロイ時に発生。

#18 1.183 [1/5] Validating package.json...
#18 1.188 error technews@0.1.0: The engine "node" is incompatible with this module. Expected version "v14.0.0". Got "16.17.0"
#18 1.201 info Visit https://yarnpkg.com/en/docs/cli/install for documentation about this command.
#18 1.201 error Found incompatible module.
 
#18 ERROR: executor failed running [/bin/bash -ol pipefail -c yarn install --frozen-lockfile]: exit code: 1
-----
> [stage-0 14/19] RUN --mount=type=cache,id=s/dcf564ae-76f9-4ee8-a6e7-e9f94e909669-/usr/local/share/cache/yarn/v6,target=/usr/local/share/.cache/yarn/v6 yarn install --frozen-lockfile:
-----
executor failed running [/bin/bash -ol pipefail -c yarn install --frozen-lockfile]: exit code: 1
 
Error: Docker build failed


Railway.appのbuilderであるNixpacks内のnodeのバージョンは 16.17.0


プロジェクトのnodeは 14.0.0


この相違が原因なのでプロジェクト側のnodeのバージョンを 16.17.0 に上げてデプロイしなおせばOKです。


/app/node_modules/@rails/webpacker/node_modules/node-sass: Command failed. gyp ERR! stack Error: make failed with exit code: 2


Railway.appデプロイ時に発生。

/app/node_modules/@rails/webpacker/node_modules/node-sass: Command failed.

gyp ERR! stack Error: make failed with exit code: 2 


@rails/webpackerで依存しているnode-gypのバージョンが古いのが原因です。

この時の@rails/webpackerのバージョンは 4.13.1 でした。


@rails/webpackerを入れ直すと解消しました。

$ yarn remove @rails/webpacker

$ yarn add @rails/webpacker


gyp verb check python checking for Python executable "python2" in the PATH


  • checking for Python executable "python2" in the PATH

  • Error: not found: python2

Railway.appデプロイ時に発生。

#21 16.59 gyp verb check python checking for Python executable "python2" in the PATH
#21 16.59 gyp verb `which` failed Error: not found: python2
#21 16.59 gyp verb `which` failed     at getNotFoundError (/app/node_modules/which/which.js:13:12)


python2へのパスが設定されていないのが原因。


Dashboard > variablesから、

環境変数 PYTHON に python2のバイナリファイルのパスを指定すればOK。


railway 環境変数追加 PYTHON バイナリへのpath


  • 参考

blog.csdn.net


docs.railway.app


checking for libpq-fe.h... no Can't find the 'libpq-fe.h header extconf.rb failed


ローカルでbundle install時に発生。

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

libpq へのパスが正しく設定されていないのが原因。


libpq の場所を確認します。

$ brew info libpq
==> libpq: stable 14.5 (bottled) [keg-only]
Postgres C API library
https://www.postgresql.org/docs/14/libpq.html

/usr/local/Cellar/libpq/14.5 (2,340 files, 27.9MB) <= ここ


/usr/local/Cellar/libpq/14.5 にあります。


このパスを bundle config のオプションである build.pg の引数 --with-pg-config へ渡し、/bin/pg_config を付けます。

$ bundle config build.pg --with-pg-config=/usr/local/Cellar/libpq/14.5/bin/pg_config

You are replacing the current global value of build.pg, which is currently "--with-pg-config=/opt/homebrew/Cellar/libpq/14.5/bin/pg_config"

ruby.studio-kingdom.com


Error: Failure while executing; /bin/launchctl bootstrap gui/501 /Users/****/Library/LaunchAgents/homebrew.mxcl.postgresql.plist exited with 5.


ローカルで発生。

Error: Failure while executing; /bin/launchctl bootstrap gui/501 /Users/****/Library/LaunchAgents/homebrew.mxcl.postgresql.plist exited with 5.


postgresqlを再起動するため、postmaster.pidを削除。

$ rm /opt/homebrew/var/postgres/postmaster.pid

or

$ rm /usr/local/var/postgres/postmaster.pid 


restartすると解消しました。

$ brew services restart postgresql


  • 参考

note.com


invalid multibyte char (US-ASCII)


cron実行をするため、wheneverを入れたところ発生。

#23 1.729 /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/job_list.rb:25:in `instance_eval': config/schedule.rb:51: invalid multibyte char (US-ASCII) (SyntaxError)
#23 1.729 config/schedule.rb:51: invalid multibyte char (US-ASCII)

日本語文字列の記載がある場所でエラーが出ていました。


config/schedule.rb の先頭にエンコーディング指定を入れると解消します。

# encoding: utf-8


  • 参考

code-examples-ja.hateblo.jp


github.com


/usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:77:in `popen': No such file or directory - crontab (Errno::ENOENT)


デプロイ時に発生。


crontabがないとのエラー。

/usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:77:in `popen': No such file or directory - crontab (Errno::ENOENT)


#23 8.083 /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:77:in `popen': No such file or directory - crontab (Errno::ENOENT)
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:77:in `write_crontab'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:38:in `run'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/lib/whenever/command_line.rb:6:in `execute'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/gems/whenever-1.0.0/bin/whenever:44:in `<top (required)>'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/bin/whenever:23:in `load'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/bin/whenever:23:in `<main>'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/bin/ruby_executable_hooks:22:in `eval'
#23 8.083    from /usr/local/rvm/gems/ruby-2.6.7/bin/ruby_executable_hooks:22:in `<main>'
 
#23 ERROR: executor failed running [/bin/bash -ol pipefail -c mkdir -p tmp/pids && cron && bundle exec whenever --set environment=production --update-crontab && bundle exec puma -C config/puma.rb]: exit code: 1
-----
> [stage-0 19/19] RUN  mkdir -p tmp/pids && bundle exec whenever --set environment=production --update-crontab && bundle exec puma -C config/puma.rb:
-----
executor failed running [/bin/bash -ol pipefail -c mkdir -p tmp/pids && cron && bundle exec whenever --set environment=production --update-crontab && bundle exec puma -C config/puma.rb]: exit code: 1
 
Error: Docker build failed


build時のコマンドでcronをインストールすればOKです。

# -y オプションを付けないとbuild時の[y/n]を聞かれるタイミングで処理が進まずエラーになります

sudo apt-get -y update && sudo apt-get -y upgrade && sudo apt-get -y install cron


これをDashboard > Settings > Build Commandに追記します。

mkdir -p tmp/pids &&
sudo apt-get -y update &&
sudo apt-get -y upgrade &&
sudo apt-get -y install cron &&
cron &&
bundle exec whenever --set environment=production --update-crontab




Extracting tar content of undefined failed, the file appears to be corrupt: EEXIST: file already exists.


Extracting tar content of undefined failed, the file appears to be corrupt: EEXIST: file already exists, mkdir '/usr/local/share/.cache/yarn/v6/npm-@babel-runtime-7.13.10-47d42a57b6095f4468da440388fdbad8bebf0d7d-integrity/node_modules/@babel/runtime/helpers'

mkdirのコマンドでディレクトリが既に存在するためエラーとなっています。


既存の yarn.lock を削除後に yarn install してpushすると解消。


最後に


下記の表に、

フリーランスエンジニア向けの人気エージェントをまとめてみました。


エージェント選びで悩んでいる方は是非参考にしてみてくださいね★


エージェント 詳細 最高単価/月 設立
フリエン(furien) 評判をチェック 180万円/月 2011年
ギークスジョブ 評判をチェック 180万円/月 2011年
フォスターフリーランス 評判をチェック 230万円/月 1996年
pe-bank(pebank) 評判をチェック 200万円/月 1989年
Midworks(ミッドワークス) 評判をチェック 200万円/月 2013年
レバテックフリーランス 評判をチェック 140万円/月 2005年