From 8a11cd88f71bb59699c6a321cdab8fb438a1fc5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 7 Oct 2026 17:17:27 +0200 Subject: [PATCH] Create search indices during deploy, import in background Mastodon 4.4+ can create/upgrade the Elasticsearch indices and mappings without importing data. Do that synchronously during the deployment so search does not fail on a missing index, then populate the (potentially very long) import via a systemd unit started with --no-block so the maintenance window is not extended. --- .../kosmos-mastodon/recipes/default.rb | 40 +++++++++++++++++-- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/site-cookbooks/kosmos-mastodon/recipes/default.rb b/site-cookbooks/kosmos-mastodon/recipes/default.rb index 75f04ba..c96e242 100644 --- a/site-cookbooks/kosmos-mastodon/recipes/default.rb +++ b/site-cookbooks/kosmos-mastodon/recipes/default.rb @@ -214,18 +214,26 @@ execute "rake db:migrate (post-deployment)" do command "bundle exec rake db:migrate" timeout 21_600 action :nothing - notifies :run, "execute[tootctl search deploy accounts mapping]", :immediately + notifies :run, "execute[tootctl search deploy (create indices)]", :immediately end -# Mastodon 4.4 changed the Elasticsearch `accounts` index mappings -execute "tootctl search deploy accounts mapping" do +# Create or upgrade the Elasticsearch indices and mappings without importing +# data, so that search does not fail on a missing index. The (potentially very +# long) import is deferred to a systemd unit started in the background below. +execute "tootctl search deploy (create indices)" do environment deploy_env user mastodon_user group mastodon_user cwd mastodon_path - command "#{bundle_path} exec bin/tootctl search deploy --only-mapping --only=accounts" + command "#{bundle_path} exec bin/tootctl search deploy --no-import" timeout 3_600 action :nothing + notifies :run, "execute[start mastodon search deploy]", :immediately +end + +execute "start mastodon search deploy" do + command "systemctl start --no-block mastodon-search-deploy.service" + action :nothing end service "mastodon-web" do @@ -282,6 +290,30 @@ systemd_unit 'mastodon-delete-old-media-cache.timer' do action [:create, :enable, :start] end +# +# Populate the Elasticsearch indices in the background. The indices and +# mappings are created synchronously by the recipe above; this unit only does +# the (potentially very long) import, so it is started without blocking. +# + +systemd_unit 'mastodon-search-deploy.service' do + content({ + Unit: { + Description: 'Populate the Mastodon search index' + }, + Service: { + Type: "oneshot", + User: mastodon_user, + WorkingDirectory: mastodon_path, + Environment: "RAILS_ENV=#{rails_env}", + ExecStart: "#{bundle_path} exec bin/tootctl search deploy", + TimeoutStartSec: "21600", + } + }) + triggers_reload true + action [:create] +end + firewall_rule "mastodon_app" do port node['kosmos-mastodon']['app_port'] source "10.1.1.0/24"