May 10, 2020
3 min read
Development tips

Any service can work in two modes

  1. Constantly, performing some actions/handling some events.
  2. Periodically start up, do something and then turn off, freeing up RAM.

Is worth determining which category the service will belong.

All python services should be placed in separate miniconda environments to avoid dependency conflicts. For example, the ta-lib library requires Python version = 3.5 to work, and aiogram >= 3.6 to work. Different projects require different versions of python.

Services running all the time

Typical project structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#!/bin/bash

ARGS=("$@")
SESSION=${PWD##*/} # tmux session name = directory name

if ! [[ $(tmux ls | grep $SESSION) ]]; then
	tmux new-session -d -s $SESSION

	tmux send-keys 'conda activate ' $SESSION C-m # virtual environment name = tmux session name
	tmux send-keys 'python main.py' C-m
fi

# if executed with the -d argument, runs in daemon mode
if ! [[ $ARGS = "-d" || $ARGS = "--daemon" ]]; then
	tmux attach-session -t $SESSION
fi

Each process should be placed in a separate tmux shell. This will allow you to detach from the process and return to the terminal. Tmux cheat sheet:

  1. https://www.ocf.berkeley.edu/~ckuehl/tmux/
  2. https://tmuxcheatsheet.com/

Scheduled services

Linux has a built-in scheduler cron. Read about cron in my post.

Typical project structure:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#!/bin/bash

# otherwise you won't be able to log into the conda environment
CONDA_BASE=$(conda info --base)

source $CONDA_BASE/etc/profile.d/conda.sh

SESSION=${PWD##*/} # direcroty name
conda activate $SESSION

python main.py