Troubleshooting local development
*********************************


Docker no space left on device
==============================

Suppose you see an error message like this when trying to run an
experiment using Docker:

   ERROR: failed to solve: failed to copy: write /var/lib/docker/buildkit/content/ingest/ae8153b11f4d4f00d8b937b5de83ad657bae8a815251f89f9476de4147382577/data: no space left on device

This means too many old Docker images have accumulated on your system.
This can be fixed by running the following command:

   docker system prune

If the error is on a remote SSH server rather than your laptop, also
prune volumes and see Troubleshooting deployments.


Docker connection errors
========================

   docker.errors.DockerException: Error while fetching server API version:
   ('Connection aborted.', ConnectionRefusedError(61, 'Connection refused'))

The Docker daemon on your computer is not running; start Docker
Desktop.

   docker.errors.DockerException: Error while fetching server API
   version: ('Connection aborted.', PermissionError(13, 'Permission denied'))

Your user cannot access the Docker socket. On Linux, add your user to
the "docker" group and log in again; otherwise, change the socket's
permissions.


Port 5000 is already in use
===========================

On macOS, turn off AirPlay Receiver (**System Settings > General >
AirDrop & Handoff**), which uses port 5000. Otherwise, stop any other
experiment running in another terminal or IDE window.

By default, "psynet debug local" sessions share port 5000, the
PostgreSQL database, Redis and Dallinger's development folder. A
second "psynet debug local" or "psynet deploy local" therefore refuses
to start, without touching the running one, while another uses the
same port, or the same database on another port, even when another
user started it. It also refuses while a PsyNet test session ("psynet
test local" or pytest) has claimed the port, or while tests or another
"psynet debug" (for example an "--isolated" one) use the same
directory. (Leftover worker processes from a server that was killed
are stopped automatically.) Stop the other experiment (Ctrl+C in its
terminal) first; to see what is using the port, run "lsof -nP
-iTCP:5000 -sTCP:LISTEN". The check reads "base_port" from the
environment, "config.txt" and "~/.dallingerconfig", not from the
experiment class's "config" dictionary, and "--docker" always uses
port 5000. To keep both running, start the second with "psynet debug
local --isolated", or give one of them its own database, Redis server
and port as described in Run several experiments at once. "psynet test
local" already uses its own, so it can run alongside.

Remote launches ("psynet deploy ssh", "psynet debug ssh" and "psynet
performance-test ssh") prepare the experiment in a scratch database
and Redis of their own, so they leave a running local experiment
alone. Because they replace the experiment directory's generated
files, they don't run at the same time as a local server or test
session in the same directory: whichever starts second refuses to.


Database connection refused
===========================

Suppose you see an error message like this:

   connection to server at "localhost" (::1), port 5432 failed: Connection refused
       Is the server running on that host and accepting TCP/IP connections?

PostgreSQL isn't running. If you use the Docker services that Install
sets up, check and start them from the experiment directory:

   psynet services check
   psynet services ensure

The rest of this section applies only to a PostgreSQL installed with
Homebrew on a Mac. Check the status of your database by running this
command:

   brew services

If you don't see a line with "postgresql", PostgreSQL isn't installed
with Homebrew; use the Docker services above instead.

If you do see a line with "postgresql", it probably has "error"
written next to it. You need to get access to the logs to debug this
error. To do so, look at the "File" column of the "brew services"
output, find the value corresponding to "postgresql". Print that file
in your terminal using "cat", for example:

   cat ~/Library/LaunchAgents/homebrew.mxcl.postgresql@14.plist

Look for a line like this:

   <key>StandardErrorPath</key>

The error log path is contained underneath it, between the "<string>"
identifiers. View the last few lines of that file in your terminal
using "tail", for example:

   tail /usr/local/var/log/postgresql@14.log

Have a look at the error message. One possible message is something
like this:

   2023-04-25 16:53:51.224 BST [28527] FATAL:  lock file "postmaster.pid" already exists
   2023-04-25 16:53:51.224 BST [28527] HINT:  Is another postmaster (PID 716) running in data directory "/usr/local/var/postgresql@14"?

If you see this error message, try restarting your computer and trying
again.

Another possible error message is this:

   Reason: tried: '/usr/local/opt/icu4c/lib/libicui18n.72.dylib' (no such file)

It has proved possible in the past to fix this problem by running the
following:

   brew reinstall postgresql@14
   brew services restart postgresql@14

where "postgresql@14" should be replaced with the exact name for the
Postgres service that you saw in "brew services".

If that doesn't work, try searching Google for help. If you find
another solution, please share your experience here.


MISCONF Redis is configured to save RDB snapshots
=================================================

If you you see an error beginning 'MISCONF Redis is configured to save
RDB snapshots', and you are using MacOS, then you may be able to fix
your problem by running the following command:

   brew services restart redis


Postgres stops working after a Homebrew upgrade
===============================================

If you find that Postgres stops working after upgrading via Homebrew,
you might need to delete your local Postgres files and try again. This
can be done as follows (these instructions are from Moncef Belyamani's
tutorial):

   brew remove --force postgresql

Or if you had previously a versioned form of Postgres, for example
Postgres 14:

   brew remove --force postgresql@14

Delete the Postgres folders:

   rm -rf /usr/local/var/postgres/
   rm -rf /usr/local/var/postgresql@14/

Or if you're on an Apple Silicon Mac:

   rm -rf /opt/homebrew/var/postgres
   rm -rf /opt/homebrew/var/postgresql@14

Finally you can reinstall Postgres:

   brew install postgresql@14
   brew services start postgresql@14


Local server processes (local tests)
====================================

"psynet test local" and "psynet performance-test local" start the
experiment's Procfile processes (web server, worker and clock) with
honcho, which is installed with Dallinger. They don't use the Heroku
CLI.
