Skip to content

Commit

Permalink
chore(*): re-enable WAL for SQLite
Browse files Browse the repository at this point in the history
Signed-off-by: Rongrong <i@rong.moe>
  • Loading branch information
Rongronggg9 committed May 27, 2022
1 parent 3c39136 commit 71a8461
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TELEGRAPH_TOKEN="
# ↓------ Advanced settings ------↓ #
#MULTIUSER=0 # default: 1
#CRON_SECOND=30 # 0-59, default: 0
#DATABASE_URL=postgres://username:password@host:port/db_name # default: sqlite://config/db.sqlite3?journal_mode=OFF
#DATABASE_URL=postgres://username:password@host:port/db_name # default: sqlite://path/to/config/db.sqlite3
#API_ID=1025907 # get it from https://core.telegram.org/api/obtaining_api_id
#API_HASH=452b0359b988148995f22ff0f4229750 # get it from https://core.telegram.org/api/obtaining_api_id
#IMG_RELAY_SERVER=https://images.weserv.nl/?url= # default: https://rsstt-img-relay.rongrong.workers.dev/
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml.sample
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ services:
# ↓------ Advanced settings ------↓ #
#- MULTIUSER=0 # default: 1
#- CRON_SECOND=30 # 0-59, default: 0
#- DATABASE_URL=postgres://username:password@host:port/db_name # default: sqlite://config/db.sqlite3?journal_mode=OFF
#- DATABASE_URL=postgres://username:password@host:port/db_name # default: sqlite://path/to/config/db.sqlite3
#- API_ID=1025907 # get it from https://core.telegram.org/api/obtaining_api_id
#- API_HASH=452b0359b988148995f22ff0f4229750 # get it from https://core.telegram.org/api/obtaining_api_id
#- IMG_RELAY_SERVER=https://images.weserv.nl/?url= # default: https://rsstt-img-relay.rongrong.workers.dev/
Expand Down
24 changes: 12 additions & 12 deletions docs/advanced-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,18 @@

### Misc settings

| Key | Description | Example | Default |
|----------------------|---------------------------------------------------------------------------|-----------------------------------------------|--------------------------------------------------------|
| `MULTIUSER` | Enable multi-user feature or not? | `0` | `1` |
| `CRON_SECOND` | Run the feed monitoring task at the n-th second of each minute? (0-59) | `30` | `0` |
| `IMG_RELAY_SERVER` | Media relay server URL | `https://images.weserv.nl/?url=` | `https://rsstt-img-relay.rongrong.workers.dev/` |
| `IMAGES_WESERV_NL` | images.weserv.nl URL | `https://t0.nl/` | `https://images.weserv.nl/` |
| `DATABASE_URL` | Database URL [^7] | `postgres://user:pass@example.com:5432/table` | `sqlite://$PATH_TO_CONFIG/db.sqlite3?journal_mode=OFF` |
| `TABLE_TO_IMAGE` | Convert tables to image (causing higher CPU load) or just drop them? | `1` | `0` |
| `MANAGER_PRIVILEGED` | Allow the bot manager to manipulate any users' subscriptions or not? [^8] | `1` | `0` |
| `NO_UVLOOP` | Never enable `uvloop` (even if it is found) or not? | `1` | `0` |
| `NO_MULTIPROCESSING` | Limit the process count to `1` or not (up to `min(3, CPU_COUNT)`)? [^9] | `1` | `0` |
| `DEBUG` | Enable debug logging or not? | `1` | `0` |
| Key | Description | Example | Default |
|----------------------|---------------------------------------------------------------------------|-----------------------------------------------|-------------------------------------------------|
| `MULTIUSER` | Enable multi-user feature or not? | `0` | `1` |
| `CRON_SECOND` | Run the feed monitoring task at the n-th second of each minute? (0-59) | `30` | `0` |
| `IMG_RELAY_SERVER` | Media relay server URL | `https://images.weserv.nl/?url=` | `https://rsstt-img-relay.rongrong.workers.dev/` |
| `IMAGES_WESERV_NL` | images.weserv.nl URL | `https://t0.nl/` | `https://images.weserv.nl/` |
| `DATABASE_URL` | Database URL [^7] | `postgres://user:pass@example.com:5432/table` | `sqlite://path/to/config/db.sqlite3` |
| `TABLE_TO_IMAGE` | Convert tables to image (causing higher CPU load) or just drop them? | `1` | `0` |
| `MANAGER_PRIVILEGED` | Allow the bot manager to manipulate any users' subscriptions or not? [^8] | `1` | `0` |
| `NO_UVLOOP` | Never enable `uvloop` (even if it is found) or not? | `1` | `0` |
| `NO_MULTIPROCESSING` | Limit the process count to `1` or not (up to `min(3, CPU_COUNT)`)? [^9] | `1` | `0` |
| `DEBUG` | Enable debug logging or not? | `1` | `0` |

## Manager options

Expand Down
57 changes: 30 additions & 27 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from telethon.tl import types
from random import sample
from os import path
from signal import signal, SIGTERM

from . import log, db, command
from .i18n import i18n, ALL_LANGUAGES, get_commands_list
Expand Down Expand Up @@ -239,35 +240,37 @@ async def post():


def main():
init()

loop.run_until_complete(pre())

logger.info(f"RSS-to-Telegram-Bot ({', '.join(env.VERSION.split())}) started!\n"
f"MANAGER: {env.MANAGER}\n"
f"T_PROXY (for Telegram): {env.TELEGRAM_PROXY or 'not set'}\n"
f"R_PROXY (for RSS): {env.REQUESTS_PROXIES['all'] if env.REQUESTS_PROXIES else 'not set'}\n"
f"DATABASE: {env.DATABASE_URL.split('://', 1)[0]}\n"
f"TELEGRAPH: {f'Enable ({tgraph.apis.count} accounts)' if tgraph.apis else 'Disable'}\n"
f"UVLOOP: {'Enable' if env.uvloop_enabled else 'Disable'}\n"
f"MULTIUSER: {'Enable' if env.MULTIUSER else 'Disable'}\n"
f"CPU: {pool.PROCESS_COUNT} (usable) / {pool.AVAIL_CPU_COUNT} (available) / {pool.CPU_COUNT} (total)")
if env.MANAGER_PRIVILEGED:
logger.warning('Bot manager privileged mode is enabled! '
'Use with caution and should be disabled in production!')

loop.create_task(lazy())

scheduler = AsyncIOScheduler(event_loop=loop)
scheduler.add_job(func=command.monitor.run_monitor_task,
trigger=CronTrigger(minute='*', second=env.CRON_SECOND, timezone='UTC'),
max_instances=10,
misfire_grace_time=10)
scheduler.start()

try:
signal(SIGTERM, lambda *_, **__: exit(1)) # graceful exit handler

init()

loop.run_until_complete(pre())

logger.info(f"RSS-to-Telegram-Bot ({', '.join(env.VERSION.split())}) started!\n"
f"MANAGER: {env.MANAGER}\n"
f"T_PROXY (for Telegram): {env.TELEGRAM_PROXY or 'not set'}\n"
f"R_PROXY (for RSS): {env.REQUESTS_PROXIES['all'] if env.REQUESTS_PROXIES else 'not set'}\n"
f"DATABASE: {env.DATABASE_URL.split('://', 1)[0]}\n"
f"TELEGRAPH: {f'Enable ({tgraph.apis.count} accounts)' if tgraph.apis else 'Disable'}\n"
f"UVLOOP: {'Enable' if env.uvloop_enabled else 'Disable'}\n"
f"MULTIUSER: {'Enable' if env.MULTIUSER else 'Disable'}\n"
f"CPU: {pool.PROCESS_COUNT} (usable) / {pool.AVAIL_CPU_COUNT} (available) / {pool.CPU_COUNT} (total)")
if env.MANAGER_PRIVILEGED:
logger.warning('Bot manager privileged mode is enabled! '
'Use with caution and should be disabled in production!')

loop.create_task(lazy())

scheduler = AsyncIOScheduler(event_loop=loop)
scheduler.add_job(func=command.monitor.run_monitor_task,
trigger=CronTrigger(minute='*', second=env.CRON_SECOND, timezone='UTC'),
max_instances=10,
misfire_grace_time=10)
scheduler.start()

bot.run_until_disconnected()
except KeyboardInterrupt:
except (KeyboardInterrupt, SystemExit):
pass
finally:
loop.run_until_complete(post())
Expand Down
2 changes: 1 addition & 1 deletion src/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def __list_parser(var: Optional[str]) -> list[str]:
del _images_weserv_nl

# ----- db config -----
_database_url = os.environ.get('DATABASE_URL') or f'sqlite://{config_folder_path}/db.sqlite3?journal_mode=OFF'
_database_url = os.environ.get('DATABASE_URL') or f'sqlite://{config_folder_path}/db.sqlite3'
DATABASE_URL: Final = (_database_url.replace('postgresql', 'postgres', 1) if _database_url.startswith('postgresql')
else _database_url)
del _database_url
Expand Down
8 changes: 6 additions & 2 deletions src/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
from time import sleep
from signal import signal, SIGINT
from signal import signal, SIGINT, SIGTERM

from . import env

Expand All @@ -19,7 +19,11 @@
ThreadPoolExecutor(max_workers=1)
if PROCESS_COUNT == 1
else ProcessPoolExecutor(max_workers=PROCESS_COUNT - 1,
initializer=lambda: signal(SIGINT, lambda *_, **__: exit(1)))
initializer=lambda: (
signal(SIGINT, lambda *_, **__: exit(1))
and
signal(SIGTERM, lambda *_, **__: exit(1))
))
)


Expand Down

0 comments on commit 71a8461

Please sign in to comment.