From 182faa0289d5be448304cca7ef69c3d4566ba9eb Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Wed, 27 May 2020 11:59:07 -0500 Subject: [PATCH] [deploy] Consolidate Setup Tasks to a Single Rake Task (#8065) --- bin/docker-setup | 6 +++--- bin/setup | 11 ++--------- docs/installation/docker.md | 2 +- lib/tasks/app_setup.rake | 18 ++++++++++++++++++ lib/tasks/data_updates.rake | 8 ++++++-- release-tasks.sh | 4 +--- 6 files changed, 31 insertions(+), 18 deletions(-) create mode 100644 lib/tasks/app_setup.rake diff --git a/bin/docker-setup b/bin/docker-setup index 67e217ef9..116d5cf92 100755 --- a/bin/docker-setup +++ b/bin/docker-setup @@ -9,8 +9,8 @@ echo "== Ensuring Elasticsearch data directory is owned by the correct uid ==" docker-compose run elasticsearch chown elasticsearch:elasticsearch /usr/share/elasticsearch/data echo "== Setting up database ==" -on_db_setup_failed() { - echo "Database setup failed with exit code $1." >&2 +on_app_setup_failed() { + echo "Application setup failed with exit code $1." >&2 echo "" >&2 echo "Check the Docker logs using:" >&2 echo " docker-compose logs" >&2 @@ -20,7 +20,7 @@ on_db_setup_failed() { echo " docker-compose down -v && sudo rm -rf docker_data" >&2 exit $1 } -docker-compose run web sh -c 'yarn global add wait-on && wait-on -t 30000 http-get://elasticsearch:9200/ && rails search:setup db:setup db:migrate data_updates:run' || on_db_setup_failed $? +docker-compose run web sh -c 'yarn global add wait-on && wait-on -t 30000 http-get://elasticsearch:9200/ && rails app_initializer:setup' || on_app_setup_failed $? echo "== Starting app ==" docker-compose up diff --git a/bin/setup b/bin/setup index 9e8267c2e..6c2fad1f6 100755 --- a/bin/setup +++ b/bin/setup @@ -28,17 +28,10 @@ FileUtils.chdir APP_ROOT do FileUtils.cp "config/database.yml.sample", "config/database.yml" end - puts "\n== Preparing Elasticsearch ==" - system! "bin/rails search:setup" + puts "\n== Preparing Test Elasticsearch ==" system! 'RAILS_ENV="test" bin/rails search:setup' - puts "\n== Preparing database ==" - unless system("bin/rails db:migrate") - system! "bin/rails db:setup" - end - - puts "\n== Updating Data ==" - system! "bin/rails data_updates:run" + system! "bin/rails app_initializer:setup" puts "\n== Removing old logs and tempfiles ==" system! "bin/rails log:clear tmp:clear" diff --git a/docs/installation/docker.md b/docs/installation/docker.md index fb69c1bea..a17e5ec28 100644 --- a/docs/installation/docker.md +++ b/docs/installation/docker.md @@ -63,7 +63,7 @@ and start the container again_ The script executes the following steps: 1. `docker-compose build` -2. `docker-compose run web rails db:setup db:migrate search:setup` +2. `docker-compose run web rails app_initializer:setup` 3. `docker-compose up` ## Running the Docker app (advanced) diff --git a/lib/tasks/app_setup.rake b/lib/tasks/app_setup.rake new file mode 100644 index 000000000..55497b8af --- /dev/null +++ b/lib/tasks/app_setup.rake @@ -0,0 +1,18 @@ +namespace :app_initializer do + desc "Prepare Application on Boot Up" + task setup: :environment do + puts "\n== Preparing Elasticsearch ==" + Rake::Task["search:setup"].execute + + puts "\n== Preparing database ==" + begin + Rake::Task["db:migrate"].execute + rescue ActiveRecord::NoDatabaseError + puts "\n== Creating and Seeding database ==" + system("bin/rails db:setup") + end + + puts "\n== Updating Data ==" + Rake::Task["data_updates:enqueue_data_update_worker"].execute + end +end diff --git a/lib/tasks/data_updates.rake b/lib/tasks/data_updates.rake index 3f0ff2b7e..e68a70936 100644 --- a/lib/tasks/data_updates.rake +++ b/lib/tasks/data_updates.rake @@ -1,8 +1,12 @@ namespace :data_updates do desc "Enqueue Sidekiq worker to handle data updates" task enqueue_data_update_worker: :environment do - # Ensure new code has been deployed before we run our update scripts - DataUpdateWorker.perform_in(10.minutes) + if Rails.env.development? + Rake::Task["data_updates:run"].execute + else + # Ensure new code has been deployed before we run our update scripts + DataUpdateWorker.perform_in(10.minutes) + end end desc "Run data updates" diff --git a/release-tasks.sh b/release-tasks.sh index e95c9599b..f85c91fe6 100755 --- a/release-tasks.sh +++ b/release-tasks.sh @@ -16,8 +16,6 @@ set -Eex # runs migration for Postgres, setups/updates Elasticsearch # and boots the app to check there are no errors -STATEMENT_TIMEOUT=180000 bundle exec rails db:migrate +STATEMENT_TIMEOUT=180000 bundle exec rails app_initializer:setup bundle exec rake fastly:update_configs -bundle exec rake search:setup -bundle exec rake data_updates:enqueue_data_update_worker bundle exec rails runner "puts 'app load success'"