mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 18:10:06 -05:00
A feature's guide now sits under the menu it is used from, read from the templates rather than from where somebody once put it: Header-Bar, All-Boards, Pages, Board-Header (search and every Board View), Board-Sidebar (Board Settings, Filter, Members, Labels, Archive), Rules, Swimlanes, Lists, Cards, Member-Settings, Admin-Panel and Login-Page. Topics used from several menus (editor, history, theme, translations, email, troubleshooting) are in General. - Each area has a README mapping its menu entries to their guides and to the template or server module that implements them. Board-Header/README.md lists all 40 Board View entries with the template boardBody.jade renders; five views (Table, Calendar Mode, Time, Timeline, Map) have no guide of their own yet and say so. - Board-View.md listed 25 views while BOARD_VIEWS has 40; it now lists all of them with their English labels. - Pages keep their file names. Where a released changelog links an old path, a "# Moved" page stays there and points to the new one; test-menu.sh skips folders holding only those. - Undo was filed under Login; it is General/Undo now. - Every link in docs, code comments, scripts and tests follows the move; docsLinksResolve checks them all. Tests that read a doc follow it to its new place. boardViewSettings now fails when Board-View.md or Board-Header/README.md differs from BOARD_VIEWS, or names a template boardBody.jade does not render. Thanks to xet7 !
761 lines
44 KiB
Bash
Executable File
761 lines
44 KiB
Bash
Executable File
#!/bin/bash
|
||
|
||
if [[ "$PATH" != *"$SNAP_INSTANCE_NAME"* ]]; then
|
||
export PATH="${PATH/\/snap\/wekan\//\/snap\/$SNAP_INSTANCE_NAME\/}"
|
||
fi
|
||
|
||
# get wekan/mongo settings
|
||
source $SNAP/bin/wekan-read-settings
|
||
|
||
# WHICH DATABASE. There is no `database` setting any more - WeKan runs on
|
||
# FerretDB, and MongoDB exists in this snap to be READ while a migration is
|
||
# owed. bin/database-role answers from the data itself (is there a FerretDB with
|
||
# something in it, and has the migration finished?), so the answer cannot
|
||
# disagree with what is on disk the way a setting could. Everything below that
|
||
# reads "$DATABASE" is unchanged: it is the same two values, decided better.
|
||
DATABASE="$(bash "$SNAP/bin/database-role")"
|
||
export DATABASE
|
||
|
||
# Sibling snap service prefix ("wekan", or the instance name for a parallel install).
|
||
WEKAN_SNAP_SVC="${SNAP_INSTANCE_NAME:-${SNAP_NAME:-wekan}}"
|
||
|
||
# Maintenance mode: if $SNAP_COMMON/.wekan-maintenance exists, do NOT start WeKan or touch the
|
||
# databases. Instead serve an "under maintenance" page (HTTP 503) on the WeKan port for ALL
|
||
# URLs, so end users see a maintenance message while an admin runs MongoDB or FerretDB by hand
|
||
# (on DB port 27019) for data access over the MongoDB wire protocol. Toggle with
|
||
# `snap run wekan.maintenance on|off`.
|
||
# #6458: run bundled binaries through cpu-exec, which exec's them directly when
|
||
# the CPU has every instruction-set feature they need and falls back to bundled
|
||
# qemu-user when it does not (AVX masked by a hypervisor, an old CPU, ARMv8.0).
|
||
# node and the WeKan bundle declare no required features, so this is a plain exec
|
||
# today; it makes the launch feature-safe if that ever changes. mongodb-control
|
||
# already does this for mongod and ferretdb-control for FerretDB. Empty (direct
|
||
# exec) when cpu-exec is missing, e.g. an older snap revision.
|
||
CPU_EXEC="$SNAP/bin/cpu-exec"
|
||
[ -x "$CPU_EXEC" ] || CPU_EXEC=""
|
||
|
||
# #6471: the database files are from a MongoDB this snap has no reader for, so no
|
||
# database is ever going to answer. Waiting for one means 502 Bad Gateway with the
|
||
# reason only in `snap logs`; serve a page that says what happened, that nothing was
|
||
# changed, and what the two ways forward are. Cleared by removing the marker (or by
|
||
# reverting the snap, which is one of the two).
|
||
# #6471: a marker from a DIFFERENT revision is not an answer about this one, so
|
||
# it must not keep the explanatory page up either. mongodb-control removes it and
|
||
# retries; this service can start first, so it makes the same judgement rather
|
||
# than serving "your data is too old for this snap" on a snap that has not tried.
|
||
if [ -f "$SNAP_COMMON/.mongodb-data-too-old" ] && \
|
||
bash "$SNAP/bin/stale-marker" "$SNAP_COMMON/.mongodb-data-too-old"; then
|
||
echo "The 'database too old' marker was written by a different snap revision; ignoring it so this one can try."
|
||
rm -f "$SNAP_COMMON/.mongodb-data-too-old" "$SNAP_COMMON/.mongodb-data-too-old.revision"
|
||
fi
|
||
# #6585: the marker says the OLD MongoDB files cannot be read by this snap, and that
|
||
# stops WeKan only when MongoDB is the database WeKan needs. An instance running on
|
||
# FerretDB, with its data in it, is not waiting for those files at all - they are
|
||
# what it was migrated FROM, still on disk because the migration never deletes them.
|
||
# Serving the page there hides a working site behind an explanation of something
|
||
# that does not matter to it: "It somehow tries to access Mongodb again instead of
|
||
# Ferretdb. I don't even have an old version, but just this [page]."
|
||
#
|
||
# The marker is left in place rather than deleted. It is true - those files really
|
||
# are unreadable by this snap - and it belongs to MongoDB, so if the admin switches
|
||
# back to a snap that cannot read the data, the page is right to return.
|
||
SERVING_FERRETDB=false
|
||
if [ "ferretdb" = "$DATABASE" ] && \
|
||
bash "$SNAP/bin/ferretdb-has-data" "$SNAP_COMMON/files/db" 2>/dev/null; then
|
||
SERVING_FERRETDB=true
|
||
fi
|
||
if [ -f "$SNAP_COMMON/.mongodb-data-too-old" ] && [ "true" = "$SERVING_FERRETDB" ]; then
|
||
echo "The 'database too old' marker is about the old MongoDB files; this snap runs on FerretDB, which has the data. Starting WeKan normally."
|
||
elif [ -f "$SNAP_COMMON/.mongodb-data-too-old" ]; then
|
||
echo "DATABASE TOO OLD FOR THIS SNAP (.mongodb-data-too-old present): serving the explanatory page instead of WeKan."
|
||
export PORT="${PORT:-80}"
|
||
exec ${CPU_EXEC:+bash "$CPU_EXEC"} env WEKAN_MAINTENANCE_REASON=data-too-old "$SNAP/bin/node" "$SNAP/bin/wekan-maintenance-page.mjs"
|
||
fi
|
||
|
||
if [ -f "$SNAP_COMMON/.wekan-maintenance" ]; then
|
||
echo "MAINTENANCE MODE (.wekan-maintenance present): serving the maintenance page instead of WeKan."
|
||
export PORT="${PORT:-80}"
|
||
# The maintenance page reads the product name from $SNAP_COMMON/.product-name (cached during
|
||
# normal operation) so it shows a custom Admin Panel product name, not "WeKan".
|
||
exec ${CPU_EXEC:+bash "$CPU_EXEC"} "$SNAP/bin/node" "$SNAP/bin/wekan-maintenance-page.mjs"
|
||
fi
|
||
|
||
# ---------------------------------------------------------------------------------------
|
||
# Which database does WeKan use? The `database` snap setting is AUTHORITATIVE:
|
||
# WeKan runs on FerretDB. MongoDB runs only while a migration is still owed - it is
|
||
# the source that gets READ, and it keeps the site up until the migration finishes.
|
||
# There is no `database` setting to choose between them any more: bin/database-role
|
||
# decides from the data, so the answer cannot contradict what is on disk, and the
|
||
# safety it exists for is built in - never start an EMPTY FerretDB while real data
|
||
# still lives in MongoDB (that would look like all data was lost).
|
||
# ---------------------------------------------------------------------------------------
|
||
FERRETDB_SQLITE_DIR="$SNAP_COMMON/files/db"
|
||
ferretdb_has_data=false
|
||
if bash "$SNAP/bin/ferretdb-has-data" "$FERRETDB_SQLITE_DIR"; then ferretdb_has_data=true; fi
|
||
mongodb_has_data=false
|
||
if [ -f "$SNAP_COMMON/WiredTiger" ] || ls "$SNAP_COMMON"/collection-*.wt >/dev/null 2>&1; then mongodb_has_data=true; fi
|
||
echo "Database selection: setting='${DATABASE:-mongodb}' ferretdb_has_data=${ferretdb_has_data} mongodb_has_data=${mongodb_has_data}"
|
||
|
||
# #6585 (comment 5290402146): `mongodb_has_data` above answers "are the files
|
||
# there", not "can anything here open them". On an instance whose MongoDB data
|
||
# was written by a MongoDB older than any mongod in this snap, those two are
|
||
# different answers - and every comparison below treats the older files as a
|
||
# candidate for holding the newest work:
|
||
#
|
||
# Database selection: setting='ferretdb' ferretdb_has_data=true mongodb_has_data=true
|
||
# WeKan: BOTH databases have been written to since the migration.
|
||
# [autopick] reading both databases to see which one holds the work ...
|
||
#
|
||
# ...at every single start, before the site comes up. And it cannot end well:
|
||
# autopick starts a mongod that cannot open the data (slowly, under emulation on
|
||
# some CPUs), then a SECOND FerretDB against the same SQLite directory the
|
||
# running one already holds.
|
||
#
|
||
# A copy nothing can read is not the copy holding your work. `migration-control`
|
||
# has already tried every reader in the snap and left .mongodb-data-too-old
|
||
# behind when none of them could open the files, so that marker is the answer -
|
||
# and the comparisons below use THIS variable instead.
|
||
mongodb_readable="$mongodb_has_data"
|
||
if [ "$mongodb_has_data" = "true" ] && [ -f "$SNAP_COMMON/.mongodb-data-too-old" ]; then
|
||
mongodb_readable=false
|
||
echo "The MongoDB files in $SNAP_COMMON cannot be opened by any mongod in this snap (.mongodb-data-too-old),"
|
||
echo "so they are not compared with FerretDB: a copy nothing can read is not the one holding your work."
|
||
echo "They are left untouched, and the explanation stands if you move back to a snap that can read them."
|
||
fi
|
||
|
||
# SAFETY: never start an EMPTY FerretDB while real data still lives in MongoDB - that would
|
||
# look like all data was lost. Revert to MongoDB (which still has the data) and tell the admin
|
||
# to migrate first. Only fires when database=ferretdb AND no SQLite exists AND MongoDB has data.
|
||
if [ "ferretdb" = "$DATABASE" ] && [ "$ferretdb_has_data" != "true" ] && [ "$mongodb_has_data" = "true" ]; then
|
||
echo "========================================================================"
|
||
echo "WeKan: database=ferretdb, but no FerretDB SQLite database exists in"
|
||
echo " $FERRETDB_SQLITE_DIR"
|
||
echo "while MongoDB data IS present. Refusing to start an EMPTY FerretDB - reverting to"
|
||
echo "database=mongodb so your data stays available. Run 'snap run wekan.migrate' to"
|
||
echo "migrate, or 'snap set wekan database=ferretdb' again once it is migrated."
|
||
echo "========================================================================"
|
||
export DATABASE="mongodb"
|
||
fi
|
||
|
||
# SAFETY, the second half (#6583): never serve a FerretDB copy that is OLDER than the
|
||
# MongoDB data sitting beside it. The guard above catches an EMPTY FerretDB; this
|
||
# catches a FULL but out-of-date one, which looks worse - WeKan comes up, boards and
|
||
# cards are all there, and only the last weeks of work are missing, so it reads as
|
||
# data loss rather than as a misconfiguration.
|
||
#
|
||
# It gets that way because the migration is a snapshot and nothing keeps it in step.
|
||
# `snap revert` to a revision that runs mongod, carry on using WeKan for a month, and
|
||
# the finished SQLite is a month behind; refresh forward and it would be served.
|
||
# Nothing is lost either way - $SNAP_COMMON is shared across revisions and is not
|
||
# rolled back by a revert, so the MongoDB data is still there - so the right move is
|
||
# simply to serve the newer of the two and say why.
|
||
#
|
||
# It has to be the NEWER of the two, not simply "not FerretDB". The first version
|
||
# of this guard compared MongoDB against the migration marker and nothing else,
|
||
# and starting mongod at all rewrites its WiredTiger files - so one service start
|
||
# during a refresh made a two-week-old MongoDB look newer than the FerretDB that
|
||
# had been serving all along, and this guard switched the admin onto it:
|
||
# "my database suddenly reverted to an old version from what looks like weeks
|
||
# ago ... upgrading to 10.83 did not fix the problem automatically" (#6583). It
|
||
# did not, because this ran again at every start. The detector now answers about
|
||
# both copies and says AMBIGUOUS (2) when both have been written to, which is not
|
||
# something to resolve by guessing - so that case switches nothing and says so.
|
||
# THE WEB PORT IS THE FIRST THING SOMEBODY LOOKS AT, so anything that can hold up
|
||
# the start has to be able to say so THERE, not only in `snap logs`. These are
|
||
# defined here rather than beside the database wait further down because the
|
||
# database comparison below runs earlier and can take longer.
|
||
WEKAN_DB_WAIT_PAGE_SECONDS="${WEKAN_DB_WAIT_PAGE_SECONDS:-30}"
|
||
_db_wait_page_pid=""
|
||
start_db_wait_page() {
|
||
[ -n "$_db_wait_page_pid" ] && return 0
|
||
[ -r "$SNAP/bin/wekan-maintenance-page.mjs" ] || return 0
|
||
echo "WeKan: still waiting for ${1}; serving the \"waiting for its database\" page on port ${PORT:-80} until it answers."
|
||
WEKAN_MAINTENANCE_REASON=waiting-for-database WEKAN_WAITING_DB="$1" PORT="${PORT:-80}" \
|
||
${CPU_EXEC:+bash "$CPU_EXEC"} "$SNAP/bin/node" "$SNAP/bin/wekan-maintenance-page.mjs" &
|
||
_db_wait_page_pid=$!
|
||
}
|
||
stop_db_wait_page() {
|
||
[ -n "$_db_wait_page_pid" ] || return 0
|
||
kill "$_db_wait_page_pid" 2>/dev/null || true
|
||
wait "$_db_wait_page_pid" 2>/dev/null || true
|
||
_db_wait_page_pid=""
|
||
}
|
||
|
||
migration_stale_rc=0
|
||
migration_stale_why="$(bash "$SNAP/bin/ferretdb-migration-stale" "$SNAP_COMMON" 2>&1)" \
|
||
|| migration_stale_rc=$?
|
||
#
|
||
# Note there is NO `database=ferretdb` condition on this one, and that is the
|
||
# point: an instance the old guard already moved to database=mongodb is sitting on
|
||
# the wrong copy right now, and saying nothing would leave it there forever. The
|
||
# admin has to hear that the other database holds writes of its own whichever one
|
||
# is currently selected.
|
||
if [ "$mongodb_readable" = "true" ] && [ "$migration_stale_rc" -eq 2 ]; then
|
||
echo "========================================================================"
|
||
echo "WeKan: BOTH databases have been written to since the migration."
|
||
echo "$migration_stale_why"
|
||
echo ""
|
||
# A timestamp cannot answer this, but the DATABASES can: bin/database-autopick
|
||
# starts each one on a temporary port, counts what is in it and finds the newest
|
||
# moment its data carries, and serves the copy that holds the work. Where both
|
||
# hold something the other does not, the documents that exist only in the other
|
||
# one are copied across - inserting what is missing, overwriting nothing - so
|
||
# WeKan's append-only history (docs/Features/General/History/History.md) carries
|
||
# the work done on the copy that is not being served into the card History of
|
||
# the one that is, instead of leaving it in a database nobody opens.
|
||
#
|
||
# Nothing is deleted on either side and the other copy stays on disk, so the
|
||
# admin can still switch, and now it is a choice rather than a repair. When the
|
||
# two cannot be told apart from their contents either, autopick changes nothing
|
||
# and the explanation below stands - that case is a decision about somebody's
|
||
# work, and #6583 is what guessing at it costs.
|
||
#
|
||
# THE COMPARISON HOLDS UP THE START, so it gets a page and a bound.
|
||
#
|
||
# It reads both copies, which means starting each database on a temporary
|
||
# port; on a large instance that is minutes, and until it returns NOTHING is
|
||
# listening on the web port. That is #6592 again, one step earlier: the
|
||
# browser gets a timeout and the reason is in `snap logs`, which is the last
|
||
# place somebody whose site is down thinks to look. Worse, before the mtime
|
||
# fix in bin/database-autopick this ran at EVERY start, so every restart took
|
||
# the site down for the length of a comparison that could never conclude.
|
||
#
|
||
# So: the same waiting page the database wait uses, and a timeout after which
|
||
# WeKan starts on the database already selected. Stopping the comparison is
|
||
# safe - it never deletes anything on either side, and the merge it may be in
|
||
# the middle of only INSERTS what is missing, so a partial one is simply
|
||
# fewer documents copied and the next start finishes the job. Serving the
|
||
# site beats finishing the comparison.
|
||
autopick_rc=1
|
||
if [ "${WEKAN_AUTOPICK:-true}" = true ] && \
|
||
{ [ -x "$SNAP/bin/database-autopick" ] || [ -f "$SNAP/bin/database-autopick" ]; }; then
|
||
_autopick_timeout="${WEKAN_AUTOPICK_TIMEOUT:-900}"
|
||
case "$_autopick_timeout" in
|
||
''|*[!0-9]*|0) _autopick_timeout=900 ;;
|
||
esac
|
||
_autopick_rc=0
|
||
bash "$SNAP/bin/database-autopick" &
|
||
_autopick_pid=$!
|
||
# The page only after a grace period, the same way the database wait does
|
||
# it: a comparison that finishes in a few seconds should not flash a page
|
||
# up, and a page that appears on every restart would make proxies cache a
|
||
# 503 for a healthy site.
|
||
_autopick_waited=0
|
||
_autopick_page_started=false
|
||
_autopick_timed_out=false
|
||
while kill -0 "$_autopick_pid" 2>/dev/null; do
|
||
if [ "$_autopick_waited" -ge "$_autopick_timeout" ]; then
|
||
_autopick_timed_out=true
|
||
kill -TERM "$_autopick_pid" 2>/dev/null || true
|
||
break
|
||
fi
|
||
if [ "$_autopick_page_started" = false ] && \
|
||
[ "$_autopick_waited" -ge "${WEKAN_DB_WAIT_PAGE_SECONDS:-30}" ]; then
|
||
start_db_wait_page "the two copies of the database to be compared"
|
||
_autopick_page_started=true
|
||
fi
|
||
sleep 1
|
||
_autopick_waited=$((_autopick_waited + 1))
|
||
done
|
||
# database-autopick traps TERM and stops the temporary database readers.
|
||
# Give that cleanup a short bound of its own, then ensure startup cannot
|
||
# remain stuck on a child which ignored TERM.
|
||
if [ "$_autopick_timed_out" = true ]; then
|
||
_autopick_stop_waited=0
|
||
while kill -0 "$_autopick_pid" 2>/dev/null && \
|
||
[ "$_autopick_stop_waited" -lt 5 ]; do
|
||
sleep 1
|
||
_autopick_stop_waited=$((_autopick_stop_waited + 1))
|
||
done
|
||
kill -KILL "$_autopick_pid" 2>/dev/null || true
|
||
fi
|
||
wait "$_autopick_pid" || _autopick_rc=$?
|
||
if [ "$_autopick_timed_out" = true ]; then
|
||
_autopick_rc=124
|
||
fi
|
||
# BEFORE anything else binds the port: the page is a child process
|
||
# holding $PORT.
|
||
stop_db_wait_page
|
||
if [ "$_autopick_rc" -eq 0 ]; then
|
||
autopick_rc=0
|
||
elif [ "$_autopick_rc" -eq 124 ]; then
|
||
echo "WeKan: the comparison of the two database copies did not finish within"
|
||
echo "${_autopick_timeout}s and was stopped. NOTHING was changed or switched, and WeKan"
|
||
echo "starts on the database already selected. Compare them by hand with"
|
||
echo "'snap run ${WEKAN_SNAP_SVC:-wekan}.database-compare', or raise the limit with"
|
||
echo "'snap set ${WEKAN_SNAP_SVC:-wekan} autopick-timeout=1800', or turn the"
|
||
echo "comparison off with 'snap set ${WEKAN_SNAP_SVC:-wekan} autopick=false'."
|
||
fi
|
||
fi
|
||
if [ "$autopick_rc" -eq 0 ]; then
|
||
# The setting has just changed underneath us; re-read it so this start
|
||
# brings up the database that was chosen rather than the previous one.
|
||
DATABASE="$(bash "$SNAP/bin/database-role")"
|
||
echo "WeKan: continuing on database=${DATABASE}, chosen from what the databases hold."
|
||
else
|
||
echo "Staying on database=${DATABASE} - the setting you already have - because"
|
||
echo "switching on a guess is how the wrong one gets served. To compare them,"
|
||
echo "start each in turn and look:"
|
||
echo " sudo snap set ${WEKAN_SNAP_SVC:-wekan} database=mongodb # then look at the boards"
|
||
echo " sudo snap set ${WEKAN_SNAP_SVC:-wekan} database=ferretdb # then look again"
|
||
echo "Neither is deleted by choosing the other, and both live in $SNAP_COMMON."
|
||
fi
|
||
echo "========================================================================"
|
||
fi
|
||
if [ "ferretdb" = "$DATABASE" ] && [ "$mongodb_readable" = "true" ] && [ "$migration_stale_rc" -eq 0 ]; then
|
||
echo "========================================================================"
|
||
echo "WeKan: the FerretDB database in $FERRETDB_SQLITE_DIR was migrated from MongoDB,"
|
||
echo "but the MongoDB data has been written to SINCE that migration - so the migrated"
|
||
echo "copy is missing everything added after it. Those changes are not lost: they are"
|
||
echo "in the MongoDB data in $SNAP_COMMON, which 'snap revert' does not roll back."
|
||
echo ""
|
||
# This used to answer by switching the snap to database=mongodb, and that is
|
||
# what the reports were: "WeKan changed to old MongoDB data". Even when the
|
||
# copy it switched to really was the newer one, it left the site on the
|
||
# database the snap is migrating AWAY from, with no way back except an admin
|
||
# typing commands - and when the detector had guessed wrong (#6583), it put
|
||
# the site on a database that was weeks behind.
|
||
#
|
||
# FerretDB is where a WeKan snap runs, on every platform; MongoDB is bundled
|
||
# to be READ during a migration. So the repair is the merge, not a switch:
|
||
# bring the documents MongoDB has and FerretDB does not into FerretDB -
|
||
# inserting what is missing, overwriting nothing - and carry on there. WeKan's
|
||
# history is append-only (docs/Features/General/History/History.md), so the
|
||
# work done on MongoDB after the migration lands in the card History rather
|
||
# than staying in a database nobody opens.
|
||
merged=false
|
||
if [ -f "$SNAP/bin/database-autopick" ]; then
|
||
echo "Bringing the newer MongoDB data into FerretDB (inserting what is missing,"
|
||
echo "overwriting nothing), so WeKan can stay on FerretDB with all of it ..."
|
||
if bash "$SNAP/bin/database-autopick" --to-ferretdb; then merged=true; fi
|
||
fi
|
||
if [ "$merged" = true ]; then
|
||
DATABASE="$(bash "$SNAP/bin/database-role")"
|
||
export DATABASE
|
||
echo "WeKan: continuing on database=${DATABASE}, with the MongoDB data merged into it."
|
||
echo "The MongoDB data is untouched in $SNAP_COMMON; nothing was deleted."
|
||
else
|
||
# The merge is what makes staying on FerretDB honest. Without it, FerretDB
|
||
# is a copy that is behind, and serving it IS the "old data" complaint -
|
||
# so this falls back to the database that demonstrably holds the newest
|
||
# writes, says so, and tries the merge again on the next start.
|
||
echo "The merge could not run this time (see 'snap logs ${WEKAN_SNAP_SVC:-wekan}'), and serving a"
|
||
echo "FerretDB copy that is behind would be showing old data. Serving MongoDB for"
|
||
echo "this start - it has the newest writes - and trying the merge again on the next"
|
||
echo "one. To do it now: snap run ${WEKAN_SNAP_SVC:-wekan}.migrate"
|
||
# For THIS start only. Nothing is written down: the next start asks the
|
||
# data again, and the merge that failed here is what has to succeed.
|
||
export DATABASE="mongodb"
|
||
fi
|
||
echo "========================================================================"
|
||
fi
|
||
|
||
# Bring up the chosen database service and stop the other, so exactly one runs on the
|
||
# shared port and WeKan does not wait forever for a stopped service. snapctl start --enable
|
||
# = snap enable + snap start; targets the snap INSTANCE name so parallel snaps hit their own.
|
||
if [ "ferretdb" = "$DATABASE" ]; then
|
||
snapctl start --enable "${WEKAN_SNAP_SVC}.ferretdb" 2>/dev/null || true
|
||
snapctl stop --disable "${WEKAN_SNAP_SVC}.mongodb" 2>/dev/null || true
|
||
elif [ "true" != "${DISABLE_MONGODB}" ]; then
|
||
snapctl start --enable "${WEKAN_SNAP_SVC}.mongodb" 2>/dev/null || true
|
||
snapctl stop --disable "${WEKAN_SNAP_SVC}.ferretdb" 2>/dev/null || true
|
||
fi
|
||
|
||
export NODE_PATH=$SNAP/bin
|
||
# MONGO_URL_FILE (#5724): a file holding the whole MongoDB URL, password
|
||
# included (Docker / Kubernetes secrets). Read here because Meteor connects
|
||
# with MONGO_URL before any WeKan code runs. MONGO_URL wins when both are set.
|
||
if [ -z "${MONGO_URL:-}" ] && [ -n "${MONGO_URL_FILE:-}" ]; then
|
||
if [ ! -r "$MONGO_URL_FILE" ]; then
|
||
echo "ERROR: MONGO_URL_FILE=$MONGO_URL_FILE cannot be read." >&2
|
||
exit 1
|
||
fi
|
||
MONGO_URL="$(cat "$MONGO_URL_FILE")"
|
||
if [ -z "$MONGO_URL" ]; then
|
||
echo "ERROR: MONGO_URL_FILE=$MONGO_URL_FILE is empty." >&2
|
||
exit 1
|
||
fi
|
||
export MONGO_URL
|
||
echo "Using MONGO_URL from MONGO_URL_FILE"
|
||
fi
|
||
# Use user-provided MONGO_URL if set, otherwise build from components
|
||
if [ -n "$MONGO_URL" ]; then
|
||
echo "Using user-provided MONGO_URL"
|
||
elif [ "nill" != "$MONGODB_BIND_UNIX_SOCKET" ] && [ "x" != "x$MONGODB_BIND_UNIX_SOCKET" ]; then
|
||
if [ -d $MONGODB_BIND_UNIX_SOCKET ]; then
|
||
export MONGO_URL="mongodb://$MONGODB_BIND_UNIX_SOCKET/mongodb-${MONGODB_PORT}.sock/wekan"
|
||
else
|
||
export MONGO_URL="mongodb://$SNAP_DATA/shared/$MONGODB_BIND_UNIX_SOCKET/wekan"
|
||
fi
|
||
else
|
||
[ "x" == "x$MONGODB_BIND_IP" ] && MONGODB_BIND_IP="127.0.0.1"
|
||
# An IPv6 LITERAL has to be in brackets in a MongoDB URI - `mongodb://::1:27019/`
|
||
# is not a URL and the driver refuses it, so an IPv6-only database could not be
|
||
# reached at all (#6550). A hostname or an IPv4 address is left as it is: the
|
||
# test is "contains a colon", which no hostname and no IPv4 address does.
|
||
MONGODB_BIND_HOST="$MONGODB_BIND_IP"
|
||
case "$MONGODB_BIND_HOST" in
|
||
\[*\]) : ;; # already bracketed
|
||
*:*) MONGODB_BIND_HOST="[$MONGODB_BIND_HOST]" ;;
|
||
esac
|
||
export MONGO_URL="mongodb://$MONGODB_BIND_HOST:$MONGODB_PORT/wekan"
|
||
# FerretDB v1 has no replica-set oplog, so only advertise MONGO_OPLOG_URL for
|
||
# real MongoDB; with FerretDB, WeKan falls back to polling automatically.
|
||
if [ -z "$MONGO_OPLOG_URL" ] && [ "ferretdb" != "${DATABASE}" ]; then
|
||
export MONGO_OPLOG_URL="mongodb://$MONGODB_BIND_HOST:$MONGODB_PORT/local?replicaSet=rs0"
|
||
fi
|
||
fi
|
||
|
||
# #6503/#6480/#6481: FerretDB v1 CAN tail an OpLog (auto-created capped
|
||
# local.oplog.rs + replica-set hello handshake — see bin/ferretdb-control), but
|
||
# on the SQLite backend Meteor's tailable+awaitData OpLog tail keeps FerretDB CPU
|
||
# pinned (reporters saw ~190–390% even idle; deleting local.sqlite* dropped it to
|
||
# ~10%), and a struggling tail also shows up as "oplog catching up took too long"
|
||
# and stalls board/login loading. So the DEFAULT is now POLLING ONLY. With the
|
||
# calmer polling defaults (#6467/#6468) and the SQLite pragma tuning, poll-and-diff
|
||
# is cheap. Opt into OpLog tailing with WEKAN_FERRETDB_OPLOG=true (snap set) if you
|
||
# specifically want it and have verified it stays calm on your data.
|
||
#
|
||
# The polling settings below apply either way (they are also the OpLog fallback).
|
||
# #6467/#6468: the Meteor defaults (re-poll 50 ms after any write, at least every
|
||
# 10 s) hammered FerretDB; calmer defaults re-poll at most every 2 s / 30 s. Own
|
||
# writes still show instantly (local cache); only OTHER users' changes wait ~2 s.
|
||
if [ "ferretdb" = "${DATABASE}" ]; then
|
||
WEKAN_FERRETDB_OPLOG="${WEKAN_FERRETDB_OPLOG:-true}"
|
||
if [ "true" = "${WEKAN_FERRETDB_OPLOG}" ]; then
|
||
if [ -z "${MONGO_OPLOG_URL}" ]; then
|
||
REPL_SET_NAME="${WEKAN_FERRETDB_REPL_SET:-rs0}"
|
||
export MONGO_OPLOG_URL="mongodb://${MONGODB_BIND_IP:-127.0.0.1}:${MONGODB_PORT:-27019}/local?replicaSet=${REPL_SET_NAME}"
|
||
fi
|
||
# Prefer OpLog but ALWAYS keep polling as the final fallback: Meteor tries
|
||
# the drivers in METEOR_REACTIVITY_ORDER left-to-right and uses OpLog only
|
||
# when OpLog tailing actually works, otherwise polling — so a broken/absent
|
||
# OpLog never stops WeKan from starting. Admin Panel / Version ("Reactivity
|
||
# mode") shows which one is live.
|
||
#
|
||
# #6492: FerretDB v1 does NOT implement MongoDB change streams. If Meteor is
|
||
# told to try `changeStreams` (the snap's general default is
|
||
# "changeStreams,oplog,polling", so METEOR_REACTIVITY_ORDER arrives already
|
||
# set here and the `:-oplog,polling` fallback would NOT apply), it issues
|
||
# $changeStream aggregates that FerretDB rejects as "not implemented" thousands
|
||
# of times per second — a busy-loop that pins FerretDB CPU (100–390%) and
|
||
# stalls board/card loading. So force `changeStreams` OUT of the order for
|
||
# FerretDB, whatever it was set to, leaving oplog + polling.
|
||
_reactivity="${METEOR_REACTIVITY_ORDER:-oplog,polling}"
|
||
_reactivity="$(printf '%s' "${_reactivity}" | tr ',' '\n' | grep -vixE 'changeStreams?' | tr '\n' ',' | sed 's/,,*/,/g; s/^,//; s/,$//')"
|
||
[ -z "${_reactivity}" ] && _reactivity="oplog,polling"
|
||
export METEOR_REACTIVITY_ORDER="${_reactivity}"
|
||
export DEFAULT_METEOR_REACTIVITY_ORDER="oplog,polling"
|
||
echo "FerretDB OpLog enabled (polling fallback): MONGO_OPLOG_URL=${MONGO_OPLOG_URL} METEOR_REACTIVITY_ORDER=${METEOR_REACTIVITY_ORDER}"
|
||
else
|
||
# Polling only — never change streams (FerretDB does not implement them, #6492).
|
||
# #6498: also UNSET MONGO_OPLOG_URL. Merely having it set makes Meteor start an
|
||
# OpLog tail at boot (a tailable+awaitData cursor on local.oplog.rs) regardless of
|
||
# the reactivity order, and that tail polls FerretDB continuously — pinning CPU
|
||
# even with no clients. Clearing it guarantees "polling only" truly does no OpLog
|
||
# tailing.
|
||
unset MONGO_OPLOG_URL
|
||
export METEOR_REACTIVITY_ORDER="polling"
|
||
export DEFAULT_METEOR_REACTIVITY_ORDER="polling"
|
||
echo "FerretDB OpLog disabled (WEKAN_FERRETDB_OPLOG=false): polling only, MONGO_OPLOG_URL cleared"
|
||
fi
|
||
export METEOR_POLLING_THROTTLE_MS="${METEOR_POLLING_THROTTLE_MS:-2000}"
|
||
export METEOR_POLLING_INTERVAL_MS="${METEOR_POLLING_INTERVAL_MS:-30000}"
|
||
echo "FerretDB polling settings: METEOR_POLLING_THROTTLE_MS=${METEOR_POLLING_THROTTLE_MS} METEOR_POLLING_INTERVAL_MS=${METEOR_POLLING_INTERVAL_MS}"
|
||
fi
|
||
|
||
# If WRITABLE_PATH is not set, use $SNAP_COMMON/files
|
||
# Wekan will store attachments there.
|
||
if [ -z "$WRITABLE_PATH" ]; then
|
||
export WRITABLE_PATH="$SNAP_COMMON/files"
|
||
fi
|
||
# Create attachments and avatars directories
|
||
mkdir -p "$WRITABLE_PATH/attachments"
|
||
mkdir -p "$WRITABLE_PATH/avatars"
|
||
echo -e "Using WRITABLE_PATH: $WRITABLE_PATH"
|
||
|
||
# Never print a password: one written in MONGO_URL (or read from MONGO_URL_FILE)
|
||
# is masked in the log.
|
||
echo -e "MONGO_URL=$(printf '%s' "$MONGO_URL" | sed -E 's#(://[^:/@]*):[^@/]*@#\1:***@#')"
|
||
|
||
# #6546: WeKan listens where BIND_IP says. Meteor reads BIND_IP itself, so this
|
||
# only has to pass it on - and only when it is set, so the default (0.0.0.0, IPv4)
|
||
# is unchanged for everyone who does not ask for something else. `::` is the
|
||
# IPv6 wildcard, which on a dual-stack host also accepts IPv4.
|
||
if [ -n "$BIND_IP" ]; then
|
||
export BIND_IP
|
||
echo -e "BIND_IP=$BIND_IP"
|
||
fi
|
||
|
||
# The readiness checks below use db-eval (the bundled Node.js 26 + the `mongodb`
|
||
# driver from the WeKan bundle), so there is no mongosh binary — nothing to install
|
||
# and nothing phoning home. db-eval speaks the wire protocol to both FerretDB v1
|
||
# and MongoDB 6/7.
|
||
DB_EVAL="$SNAP/bin/db-eval"
|
||
# #6746: socket_denied / socket_denied_hint - tell a connection the KERNEL refused
|
||
# (AppArmor, snapd 2.77.1 on some kernels) from a database that is not up yet.
|
||
# shellcheck source=/dev/null
|
||
[ -f "$SNAP/bin/socket-denied" ] && source "$SNAP/bin/socket-denied"
|
||
# Prints the AppArmor explanation once per wait loop, as soon as the first
|
||
# refused connection shows it - not after the two-minute timeout, and not in
|
||
# a log that otherwise only says "not ready yet".
|
||
show_socket_denied() {
|
||
declare -F socket_denied >/dev/null || return 1
|
||
socket_denied "$2" || return 1
|
||
echo ""
|
||
echo "========================================================================"
|
||
echo "WeKan: cannot talk to ${1} on ${MONGO_HOST}:${MONGO_PORT}:"
|
||
echo " $2"
|
||
echo ""
|
||
socket_denied_hint
|
||
echo "========================================================================"
|
||
echo ""
|
||
return 0
|
||
}
|
||
MONGO_PORT="${MONGODB_PORT:-27019}"
|
||
MONGO_HOST="${MONGODB_BIND_IP:-127.0.0.1}"
|
||
# Bracketed for the same reason as MONGO_URL above (#6550): the readiness probe
|
||
# builds a URI too, and an unbracketed IPv6 literal makes it wait forever on a
|
||
# URL the driver cannot parse.
|
||
case "$MONGO_HOST" in
|
||
\[*\]) : ;;
|
||
*:*) MONGO_HOST="[$MONGO_HOST]" ;;
|
||
esac
|
||
# directConnection so admin commands reach this exact node (also before a replica
|
||
# set is initiated).
|
||
DB_URL="mongodb://${MONGO_HOST}:${MONGO_PORT}/?directConnection=true"
|
||
|
||
# #6592: "a reload of wekan got a timeout (loading forever)" after a refresh from
|
||
# 10.85. WeKan does not open its web port until the database answers, and the two
|
||
# waits below are deliberately endless - a database can take minutes to come up
|
||
# after an update, and giving up on it would be worse than waiting. But while they
|
||
# wait, NOTHING is listening on the web port: the browser gets a timeout, and the
|
||
# reason is in `snap logs`, which is the last place somebody whose site is down
|
||
# thinks to look. It is also indistinguishable from WeKan itself being broken.
|
||
#
|
||
# So the wait says so, on the web port, with the commands that name the real
|
||
# reason. After a grace period, not immediately: an ordinary restart passes
|
||
# through here in a few seconds and a page that flashes up during every restart
|
||
# would be noise (and would make proxies cache a 503 for a healthy site).
|
||
#
|
||
# The page is a child process holding $PORT, so it MUST be stopped before WeKan -
|
||
# or another wekan-control after an exec - tries to bind that port.
|
||
|
||
if [ "ferretdb" = "${DATABASE}" ]; then
|
||
# FerretDB v1 has no replica set / oplog — just wait until it accepts
|
||
# connections, then start WeKan (which uses polling for reactivity).
|
||
# Make sure the FerretDB service is actually enabled and running first; if it was
|
||
# left stopped/disabled (failed migration hand-off, manual stop, crash) enable and
|
||
# start it now (snapctl start --enable = snap enable + snap start) so WeKan does not
|
||
# wait here forever.
|
||
snapctl start --enable "${WEKAN_SNAP_SVC}.ferretdb" 2>/dev/null || true
|
||
echo "Waiting for FerretDB to accept connections on ${MONGO_HOST}:${MONGO_PORT}..."
|
||
# #6476: after WEKAN_DB_WAIT_TIMEOUT seconds (default 120) print diagnostics
|
||
# once, then keep retrying. Without this the loop was silent and infinite:
|
||
# WeKan does NOT open its web port until FerretDB answers, so if FerretDB
|
||
# never starts listening (a crashed/incompatible per-arch binary, a locked or
|
||
# corrupt SQLite file, a failed migration hand-off) WeKan blocks HERE forever
|
||
# and the only symptom is "port not open, no clue why". Mirrors the MongoDB
|
||
# branch below.
|
||
WEKAN_DB_WAIT_TIMEOUT=${WEKAN_DB_WAIT_TIMEOUT:-120}
|
||
db_waited=0
|
||
db_hint_shown=false
|
||
db_denied_shown=false
|
||
# #6746: keep the driver's error from every attempt, so a refusal by the
|
||
# kernel is recognised on the first try instead of after the timeout.
|
||
until _ping_err="$("$DB_EVAL" ping "$DB_URL" 2>&1 >/dev/null)"; do
|
||
if [ "$db_denied_shown" != "true" ] && show_socket_denied FerretDB "$_ping_err"; then
|
||
db_denied_shown=true
|
||
fi
|
||
if [ "$db_hint_shown" != "true" ] && [ "$db_waited" -ge "$WEKAN_DB_WAIT_TIMEOUT" ]; then
|
||
echo ""
|
||
echo "========================================================================"
|
||
echo "WeKan: FerretDB still not accepting connections on ${MONGO_HOST}:${MONGO_PORT}."
|
||
echo ""
|
||
echo "WeKan will NOT open its web port until FerretDB answers, so it is"
|
||
echo "waiting here. The FerretDB service can show as 'active' yet have"
|
||
echo "crashed or never started listening."
|
||
echo ""
|
||
echo "Check the FerretDB log for the real reason:"
|
||
echo " snap logs ${WEKAN_SNAP_SVC}.ferretdb"
|
||
echo ""
|
||
echo "Common causes after a snap update:"
|
||
echo " - the bundled FerretDB binary cannot run on this CPU/architecture"
|
||
echo " ('exec format error' in the log) - report your 'snap version' and"
|
||
echo " CPU architecture to WeKan."
|
||
echo " - a locked or corrupt SQLite database under:"
|
||
echo " $FERRETDB_SQLITE_DIR"
|
||
echo " - FerretDB left disabled/stopped (a failed migration hand-off):"
|
||
echo " snap start --enable ${WEKAN_SNAP_SVC}.ferretdb"
|
||
echo ""
|
||
# #6746: the log used to say only "not ready" - "permission denied"
|
||
# and "connection refused" looked the same. Show what the driver
|
||
# said on the last attempt. (The `database` setting this used to
|
||
# suggest is gone: the configure hook unsets it, so that advice
|
||
# changed nothing.)
|
||
if [ -n "$_ping_err" ]; then
|
||
echo "The connection attempt says:"
|
||
echo " $_ping_err"
|
||
echo ""
|
||
fi
|
||
echo "Attachments and avatars are stored ON DISK under WRITABLE_PATH, NOT"
|
||
echo "in the database ($SNAP_COMMON/files), so they are safe."
|
||
echo "========================================================================"
|
||
echo ""
|
||
db_hint_shown=true
|
||
fi
|
||
if [ "$db_waited" -ge "$WEKAN_DB_WAIT_PAGE_SECONDS" ]; then
|
||
start_db_wait_page ferretdb
|
||
fi
|
||
echo "FerretDB not ready yet, retrying in 5 seconds..."
|
||
sleep 5
|
||
db_waited=$((db_waited + 5))
|
||
done
|
||
stop_db_wait_page
|
||
echo "FerretDB is ready."
|
||
# #6473: SELF-HEALING attachments. Older MongoDB -> FerretDB migrations silently
|
||
# lost attachments/avatars (CollectionFS copies.<bucket>.key was never resolved;
|
||
# gridFsFileId-only Meteor-Files records were skipped). The MongoDB source data
|
||
# was never modified, so everything is recoverable: run an automatic, INCREMENTAL
|
||
# files-only repair in the background — it verifies what is already migrated,
|
||
# migrates only what is missing, never touches text data, runs once (marker
|
||
# $SNAP_COMMON/.attachments-files-v2-done) and never blocks WeKan from starting.
|
||
bash "$SNAP/bin/attachment-repair" > "$SNAP_COMMON/attachment-repair.log" 2>&1 &
|
||
elif [ "true" != "${DISABLE_MONGODB}" ]; then
|
||
# Make sure the MongoDB service is enabled and running first; if it was left
|
||
# stopped/disabled, enable and start it now so WeKan does not wait here forever.
|
||
snapctl start --enable "${WEKAN_SNAP_SVC}.mongodb" 2>/dev/null || true
|
||
# Wait for MongoDB replica set primary to be ready before starting WeKan.
|
||
# With changeStreams, Meteor requires the replica set primary to be elected.
|
||
# Large databases can take minutes to replay the WiredTiger journal after a snap update.
|
||
echo "Waiting for MongoDB replica set primary..."
|
||
# After WEKAN_DB_WAIT_TIMEOUT seconds (default 120) print the English upgrade
|
||
# guidance once, then keep retrying (large databases can take minutes to
|
||
# replay the WiredTiger journal after a snap update).
|
||
WEKAN_DB_WAIT_TIMEOUT=${WEKAN_DB_WAIT_TIMEOUT:-120}
|
||
db_waited=0
|
||
db_hint_shown=false
|
||
db_denied_shown=false
|
||
until _primary_err="$("$DB_EVAL" primary "$DB_URL" 2>&1 >/dev/null)"; do
|
||
if [ "$db_denied_shown" != "true" ] && show_socket_denied MongoDB "$_primary_err"; then
|
||
db_denied_shown=true
|
||
fi
|
||
if [ "$db_hint_shown" != "true" ] && [ "$db_waited" -ge "$WEKAN_DB_WAIT_TIMEOUT" ]; then
|
||
echo ""
|
||
echo "========================================================================"
|
||
echo "WeKan: still cannot connect to MongoDB."
|
||
echo ""
|
||
echo "If you just upgraded WeKan or MongoDB, the existing database may have"
|
||
echo "been created by an OLDER MongoDB version that the new MongoDB cannot"
|
||
echo "open, so MongoDB never finishes starting and WeKan keeps waiting here."
|
||
echo ""
|
||
echo "Upgrade the database - dump with the old version, restore with the new:"
|
||
echo " 1. Start the OLD, previously-working MongoDB version."
|
||
echo " 2. Back up: mongodump --archive=wekan.archive --gzip"
|
||
echo " 3. Stop old MongoDB. Start the NEW MongoDB on an EMPTY data dir."
|
||
echo " 4. Restore: mongorestore --archive=wekan.archive --gzip --drop"
|
||
echo " 5. Start WeKan again."
|
||
echo ""
|
||
echo "Attachments and avatars are stored ON DISK under WRITABLE_PATH, NOT"
|
||
echo "in MongoDB, so when moving to a new server ALSO copy that whole"
|
||
echo "directory (it contains 'files', 'attachments' and 'avatars'):"
|
||
echo " - Snap: $SNAP_COMMON/files"
|
||
echo " - Docker: the 'wekan-files' volume mounted at /data"
|
||
echo " - Source: the WRITABLE_PATH you configured"
|
||
echo ""
|
||
echo "Also check the MongoDB log (snap logs ${SNAP_NAME:-wekan}.mongodb) for"
|
||
echo "the real reason: incompatible featureCompatibilityVersion, wrong mongod"
|
||
echo "version, or an unclean shutdown needing 'mongod --repair'."
|
||
echo "========================================================================"
|
||
echo ""
|
||
db_hint_shown=true
|
||
fi
|
||
# #6525: the database may have been switched to FerretDB WHILE we wait here.
|
||
# A migration that finishes mid-wait stops MongoDB for good, so continuing to
|
||
# wait for it is waiting for something that is never coming back — the site
|
||
# stays on 503 until an admin runs `snap restart wekan` by hand. The
|
||
# migration does restart us, but that restart is the LAST step of a script
|
||
# that is itself being stopped, so it must not be the only thing that can get
|
||
# us out of this loop: notice the switch ourselves and re-exec onto FerretDB.
|
||
# #6471: the marker can appear WHILE we wait here - migration-control writes it
|
||
# the moment it finds nothing can read the data - and no database is coming
|
||
# after that, so waiting is waiting for nothing. Re-exec to serve the page.
|
||
if [ -f "$SNAP_COMMON/.mongodb-data-too-old" ]; then
|
||
echo "the database files are from a MongoDB this snap cannot read; serving the explanatory page."
|
||
# The page below holds the web port, and exec keeps this process's
|
||
# children - so it has to go before the next start binds that port.
|
||
stop_db_wait_page
|
||
exec "$0" "$@"
|
||
fi
|
||
if [ "ferretdb" = "$(bash "$SNAP/bin/database-role" 2>/dev/null)" ]; then
|
||
echo "the migration finished while WeKan was waiting for MongoDB; restarting onto FerretDB."
|
||
stop_db_wait_page
|
||
exec "$0" "$@"
|
||
fi
|
||
if [ "$db_waited" -ge "$WEKAN_DB_WAIT_PAGE_SECONDS" ]; then
|
||
start_db_wait_page mongodb
|
||
fi
|
||
echo "MongoDB not ready yet, retrying in 5 seconds..."
|
||
sleep 5
|
||
db_waited=$((db_waited + 5))
|
||
done
|
||
stop_db_wait_page
|
||
echo "MongoDB replica set primary is ready."
|
||
fi
|
||
|
||
# Cache the Admin Panel product name to $SNAP_COMMON/.productname.txt while the database is up,
|
||
# so the maintenance page can show a custom product name later when BOTH databases are stopped.
|
||
# Best-effort; only overwrite when a non-empty name is returned (otherwise keep the last known).
|
||
_pn="$("$DB_EVAL" product-name "$DB_URL" 2>/dev/null | head -n1)"
|
||
if [ -n "$_pn" ]; then
|
||
printf '%s\n' "$_pn" > "$SNAP_COMMON/.productname.txt" 2>/dev/null || true
|
||
fi
|
||
|
||
# #6492: if a data recovery is in progress (ferretdb-control wrote this marker during a
|
||
# restore/re-migrate of the text-data database), serve the standalone recovery page on
|
||
# the web port for a SHORT grace window while the just-restored FerretDB comes back up,
|
||
# then start WeKan. WeKan itself then shows the in-app recovery spinner (driven by the
|
||
# same marker) until it has health-probed the database and cleared the marker — so the
|
||
# user sees a continuous maintenance UI and never a bare connection error. The static
|
||
# page here is only a brief bridge; it is time-bounded so it can never block WeKan (the
|
||
# server, not this script, clears the marker). Reuses wekan-maintenance-page.mjs with a
|
||
# "recovering data" wording.
|
||
RECOVERY_MARKER="$SNAP_COMMON/files/db/RECOVERY_IN_PROGRESS"
|
||
if [ -f "$RECOVERY_MARKER" ]; then
|
||
_rgrace="${WEKAN_RECOVERY_BRIDGE_SECONDS:-20}"
|
||
echo "RECOVERY IN PROGRESS ($RECOVERY_MARKER present): bridging with the recovery page for up to ${_rgrace}s before starting WeKan."
|
||
WEKAN_MAINTENANCE_REASON=recovery ${CPU_EXEC:+bash "$CPU_EXEC"} "$SNAP/bin/node" "$SNAP/bin/wekan-maintenance-page.mjs" &
|
||
_mp_pid=$!
|
||
_rwait=0
|
||
while [ -f "$RECOVERY_MARKER" ] && [ "$_rwait" -lt "$_rgrace" ]; do
|
||
sleep 2
|
||
_rwait=$((_rwait + 2))
|
||
done
|
||
kill "$_mp_pid" 2>/dev/null || true
|
||
wait "$_mp_pid" 2>/dev/null || true
|
||
echo "Starting WeKan; its in-app recovery spinner takes over until the database is verified healthy."
|
||
fi
|
||
|
||
# Bound Node and the FerretDB service relative to available RAM; explicit overrides win.
|
||
_memory_mb=$(awk '/^MemTotal:/{print int($2/1024)}' /proc/meminfo 2>/dev/null || echo 2048)
|
||
for _f in /sys/fs/cgroup/memory.max /sys/fs/cgroup/memory/memory.limit_in_bytes; do [ -r "$_f" ] || continue; _b=$(cat "$_f" 2>/dev/null || true); case "$_b" in ''|max|*[!0-9]*) continue;; esac; _m=$((_b/1048576)); [ "$_m" -gt 0 ] && [ "$_m" -lt "$_memory_mb" ] && _memory_mb=$_m; break; done
|
||
_heap_mb=$((_memory_mb*3/5)); [ "$_heap_mb" -gt 4096 ] && _heap_mb=4096
|
||
_go_mb=$((_memory_mb/5)); [ "$_go_mb" -gt 1024 ] && _go_mb=1024
|
||
export NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=$_heap_mb}"
|
||
export GOMEMLIMIT="${GOMEMLIMIT:-${_go_mb}MiB}"
|
||
echo "WeKan memory budget: Node ${_heap_mb} MiB; FerretDB Go ${_go_mb} MiB; available ${_memory_mb} MiB."
|
||
APPLICATION_DIRECTORY=$SNAP
|
||
APPLICATION_START=main.js
|
||
cd $APPLICATION_DIRECTORY
|
||
#$NODE_PATH/node $APPLICATION_START
|
||
#---------------------------------------------------------------------
|
||
# https://github.com/wekan/wekan/issues/3585#issuecomment-1021522132
|
||
# Add more Node heap:
|
||
# export NODE_OPTIONS="--max_old_space_size=4096"
|
||
# Add more stack:
|
||
# bash -c "ulimit -s 65500; exec node --stack-size=65500 main.js"
|
||
#---------------------------------------------------------------------
|
||
#bash -c "ulimit -s 65500; exec $NODE_PATH/node --stack-size=65500 $APPLICATION_START"
|
||
#bash -c "ulimit -s 65500; exec $NODE_PATH/node --stack-size=65500 --max-old-space-size=8192 $APPLICATION_START"
|
||
bash -c "ulimit -s 65500; exec ${CPU_EXEC:+bash \"$CPU_EXEC\"} $NODE_PATH/node $APPLICATION_START"
|