Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move precompile task before migration task #892

Merged
merged 4 commits into from
Aug 21, 2023

Conversation

Matt-Yorkley
Copy link
Collaborator

@Matt-Yorkley Matt-Yorkley commented Aug 15, 2023

Description

Closes #891

This change minimises the amount of time between the database migration and switching the old code for the new code, which should minimise errors during deployment.

This change minimises the amount of time between the database migration and switching the old code for the new code, whcih should minimise errors during deployment.
@Matt-Yorkley Matt-Yorkley self-assigned this Aug 15, 2023
@Matt-Yorkley
Copy link
Collaborator Author

Matt-Yorkley commented Aug 15, 2023

Okay, I've done a bit of testing and as predicted; it works fine on regular deployments to existing servers but there are some issues when deploying to a fresh server for the first time, and this kicks in during the precompile step:

INFO -- : Writing /home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/public/assets/admin_minimal-40f864c398f7f6917c612aa6dc3810c3fc7c89a41420586ecd4ea1eb9487ef13.js
I, [2023-08-15T13:09:16.845500 #75866]  INFO -- : Writing /home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/public/assets/admin_minimal-40f864c398f7f6917c612aa6dc3810c3fc7c89a41420586ecd4ea1eb9487ef13.js.gz
I, [2023-08-15T13:09:18.713173 #75866]  INFO -- : Writing /home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/public/assets/web/all-1da1cc6fe6a50c600a4552a2cc1a45297623e3d517b0e95219ed51cd7f798342.js
I, [2023-08-15T13:09:18.713264 #75866]  INFO -- : Writing /home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/public/assets/web/all-1da1cc6fe6a50c600a4552a2cc1a45297623e3d517b0e95219ed51cd7f798342.js.gz
W, [2023-08-15T13:09:20.114670 #75866]  WARN -- : Removed sourceMappingURL comment for missing asset 'angular-file-upload.js.map' from /home/openfoodnetwork/.gem/ruby/3.1.0/gems/angularjs-file-upload-rails-2.4.1/app/assets/javascripts/angularjs-file-upload.js
rake aborted!
NoMethodError: undefined method `iso' for nil:NilClass

    country.iso
           ^^^^
/home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/app/services/default_country.rb:9:in `code'
/home/openfoodnetwork/apps/openfoodnetwork/releases-old/2023-08-15-130817/app/assets/javascripts/darkswarm/services/gmaps_geo.js.erb.coffee:16:in `__tilt_7160'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/tilt-2.1.0/lib/tilt/template.rb:191:in `bind_call'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/tilt-2.1.0/lib/tilt/template.rb:191:in `evaluate'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/tilt-2.1.0/lib/tilt/template.rb:109:in `render'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/legacy_tilt_processor.rb:25:in `call'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/bundle.rb:23:in `block in call'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/utils.rb:200:in `dfs'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/bundle.rb:24:in `call'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:75:in `call_processor'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `reverse_each'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/processor_utils.rb:56:in `call_processors'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:134:in `load_from_unloaded'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:60:in `block in load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/loader.rb:44:in `load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:20:in `block in initialize'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/cached_environment.rb:47:in `load'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:66:in `find_asset'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/base.rb:73:in `find_all_linked_assets'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:134:in `block in find'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `each'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:133:in `find'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/sprockets/manifest.rb:186:in `compile'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:67:in `block (3 levels) in define'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-3.7.2/lib/rake/sprocketstask.rb:147:in `with_logger'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/sprockets-rails-3.4.2/lib/sprockets/rails/task.rb:66:in `block (2 levels) in define'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/bugsnag-6.26.0/lib/bugsnag/integrations/rake.rb:20:in `execute'
/home/openfoodnetwork/.gem/ruby/3.1.0/gems/rake-13.0.6/exe/rake:27:in `<top (required)>'
/home/openfoodnetwork/.rbenv/versions/3.1.4/bin/bundle:25:in `load'
/home/openfoodnetwork/.rbenv/versions/3.1.4/bin/bundle:25:in `<main>'

So we can see from the backtrace that precompiling runs into some legacy assets that use ERB, which call a service, which calls the database and expects there to be some data present (default country) but the data isn't there yet on the first deployment, so it throws a fatal error.

Hmmmm okay well we can probably fix this...

This subset of tasks runs only on the first deployment, and runs before the precompile step (because precompiling will fail unless this has been done at least once). On subsequent deployments, we skip this setup and precompiling is done before the migration step to minimise the time where the database and code are out of sync during the deployment process.
@Matt-Yorkley
Copy link
Collaborator Author

Matt-Yorkley commented Aug 15, 2023

Alright, I've added a little fix for first deployments and it seems to be working as intended 🎉

@Matt-Yorkley Matt-Yorkley marked this pull request as ready for review August 15, 2023 13:53
Comment on lines 4 to 28
- name: load database schema
command: bash -lc "bundle exec rake db:schema:load RAILS_ENV={{ rails_env }} I_AM_SURE=1"
args:
chdir: "{{ build_path }}"
tags:
- rake
- skip_ansible_lint

- name: migrate database
command: bash -lc "bundle exec rake db:migrate RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ build_path }}"
tags:
- rake
- skip_ansible_lint
notify: restart puma

- name: seed database
command: bash -lc "bundle exec rake db:seed RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ build_path }}"
tags:
- seed
- skip_ansible_lint
notify: restart puma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we can replace these three commands with one:

rails db:setup # Creates all databases, loads all schemas, and initializes with the seed

There shouldn't be any pending migrations but if there were then the migration step is run after the asset compilation anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a good idea, and will be faster with less ansible commands and one less rake task.

But more importantly, it means less code, which will remove the need for a new file :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm that sounds nice and simple, but I'm trying to figure out if we can remove that I_AM_SURE=1 on line 5, or why it's there in the first place. Ironically I am not sure what it does...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see it's related to the rails_safe_tasks gem 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It protects from overriding a production db, right? So, we don't actually want that in our deployment. We only want to do safe things during a normal deploy.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to do safe things during a normal deploy.

I think that's a very good point! We do have a safety check beforehand, but it's checking for a specific table, so not foolproof (eg what if we removed the table one day?).

I agree and think that we should remove I_AM_SURE=1 from deploy.yml, so I think will open a new PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, I think the db:setup is non-destructive (it only creates if missing). In that case, the I_AM_SURE=1 confirmation should have no effect. I don't know how the old rails_safe_tasks gem handles this, but in any case this isn't dangerous so I won't bother updating it for now.

Copy link
Member

@dacook dacook left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, this solves the problem and should make a big difference, so I technically approve this PR as it is!

But I also think Maikel's suggestion is too good to pass up..

Comment on lines 4 to 28
- name: load database schema
command: bash -lc "bundle exec rake db:schema:load RAILS_ENV={{ rails_env }} I_AM_SURE=1"
args:
chdir: "{{ build_path }}"
tags:
- rake
- skip_ansible_lint

- name: migrate database
command: bash -lc "bundle exec rake db:migrate RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ build_path }}"
tags:
- rake
- skip_ansible_lint
notify: restart puma

- name: seed database
command: bash -lc "bundle exec rake db:seed RAILS_ENV={{ rails_env }}"
args:
chdir: "{{ build_path }}"
tags:
- seed
- skip_ansible_lint
notify: restart puma
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a good idea, and will be faster with less ansible commands and one less rake task.

But more importantly, it means less code, which will remove the need for a new file :D

@mkllnk mkllnk merged commit 47e8430 into openfoodfoundation:master Aug 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve deployment setup
3 participants