suzy1031 / everyday-study-app

0 stars 0 forks source link

Heroku Redis導入 #27

Closed suzy1031 closed 3 years ago

suzy1031 commented 3 years ago

ユーザー認証にてRedisを使っている為、HerokuでもRedisを導入したので手順をメモ

$ heroku addons:create heroku-redis:hobby-dev

ちょっと待つ

$ heroku config | grep REDIS
REDIS_TLS_URL:  rediss://:pce139369159a42747870f60b254273009a2483e8f464b055c4fb9294e81e43a4@ec2-34-205-120-58.compute-1.amazonaws.com:14260
REDIS_URL: redis://:pce139369159a42747870f60b254273009a2483e8f464b055c4fb9294e81e43a4@ec2-34-205-120-58.compute-1.amazonaws.com:14259

アプリにRedisのaddonが追加されたか確認

$ heroku redis:info -a still-dusk-87527
=== redis-fluffy-51974 (REDIS_TLS_URL, REDIS_URL)
Plan:                   Hobby Dev
Status:                 available
Created:                2020-12-26 01:32 
Version:                6.0.9
Timeout:                300
Maxmemory:              noeviction
Maintenance:            not required
Maintenance window:     Thursdays 21:30 to Fridays 01:30 UTC
Persistence:            None
HA Status:              Unavailable
Requires TLS:           Yes
Keyspace Notifications: Disabled

公式に沿ってredis.rbを作成

# initializers/redis.rb
+$redis = Redis.new(url: ENV["REDIS_URL"])

変更をプッシュしてデプロイ

git push heroku master
# 一度、db:reset
# db:migrate
# usersテーブルを作成

URLへアクセス=>動作確認完了

テーブル変更がある度にマイグレートするのは手間なので自動化

Procfile
web: bundle exec puma -p $PORT
+release: bin/rails db:migrate

参考 rubyでの接続 Herokuにデプロイした時に自動でマイグレーションを走らせる

suzy1031 commented 3 years ago

500 internal server errorを解消するまで 公式見ずに進める

# production.rb
+config.cache_store = :redis_store, servers: ENV['REDIS_URL'], { expires_in: 90.minutes }
# デプロイしたらエラー
Precompiling assets failed.

修正

# production.rb
+Rails.application.config.session_store :redis_store, {
+servers: ENV['REDIS_URL'],
+expire_after: 1.week
+}

Caused by: remote: NameError: uninitialized constant ActionDispatch::Session::RedisStore


```diff
# gemfile
+gem 'redis-actionpack'

参考 uninitialized constant ActionDispatch::Session::RedisStore (NameError)エラーを解消する github

=>デプロイできた サインアップ internal server error

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

redisが動いていない?

initializers/session_store.rb作成

# config/initializers/session_store.rb
if Rails.env.production?

  # Production では session_store に Redis を利用する
  Rails.application.config.session_store :redis_store, {
    servers: ENV['REDIS_URL'],
    expire_after: 1.week
  }
end

=>デプロイ 500 errorのまま

修正

# production.rb
+Rails.application.config.session_store = :redis_store, 'redis://:pce139369159a42747870f60b254273009a2483e8f464b055c4fb9294e81e43a4@ec2-34-205-120-58.compute-1.amazonaws.com:14259', { expires_in: 90.minutes }

=>デプロイ 500 errorのまま

公式見て進めてみる

heroku addons:create heroku-redis:hobby-dev -a still-dusk-87527

間違ってredisインスタンス2つ作成してしまった =>デプロイ 500 errorのまま

修正

# gemfile
-redis-actionpack

=>デプロイ 500 errorのまま

修正

cors.rb
-origins 'http://localhost:8080', 'http://localhost:3000', 'http://localhost:5000'
+origins 'http://localhost:8080', 'http://localhost:3000', 'http://localhost:5000', 'https://still-dusk-87527.herokuapp.com/'

=>デプロイ 500 errorのまま

そもそもusersテーブルが作成されているか疑問

$ heroku pg:psql -c "\d" 

usersテーブルが存在していない リセットしてマイグレート

$ heroku pg:reset DATABASE
$ heroku run rake db:migrate

サインアップ・ログインできた!createもできた!

suzy1031 commented 3 years ago

migrateを自動で実行したい試行錯誤

参考 HerokuにDeployしたタイミングでdb:migrateを実行したい github

$ heroku buildpacks:set https://github.com/heroku/heroku-buildpack-ruby
$ heroku buildpacks:add https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks
$ heroku config:set DEPLOY_TASKS='db:migrate cache:clear'

デプロイエラー

Don't know how to build task 'cache:clear'

config確認する

$ heroku config
~~省略~~
DEPLOY_TASKS: db:migrate cache:clear
~~省略~~

DEPLOY_TASKを編集

heroku config:set DEPLOY_TASKS="db:migrate"

=>デプロイ通った 参考 [heroku] 環境変数の操作](https://qiita.com/colorrabbit/items/18db3c97734f32ebdfde)

動いているか分からない(古くてメンテンナンスされていない可能性有り)

こっちを試してみる Herokuにデプロイした時に自動でマイグレーションを走らせる 競合しないようようにconfigから削除

heroku config:unset DEPLOY_TASKS

=>デプロイできた デプロイ時にDEPLOY_TASKが無いよって言われている

$ heroku buildpacks
=== still-dusk-87527 Buildpack URLs
1. https://github.com/heroku/heroku-buildpack-ruby
2. heroku/ruby
3. https://github.com/gunpowderlabs/buildpack-ruby-rake-deploy-tasks

bulldpackの全削除

$ heroku buildpacks:clear -a still-dusk-87527
$ heroku buildpacks
still-dusk-87527 has no Buildpack URL set.
# 消えた

参考 Herokuのbuildpackについて