Add way to run tasks
This commit is contained in:
parent
3af9c15492
commit
f699c6c91a
4 changed files with 75 additions and 3 deletions
1
.flake8
1
.flake8
|
@ -1,6 +1,7 @@
|
||||||
[flake8]
|
[flake8]
|
||||||
exclude =
|
exclude =
|
||||||
tests/*
|
tests/*
|
||||||
|
run.py
|
||||||
|
|
||||||
extend-ignore =
|
extend-ignore =
|
||||||
Q0, E501, C812, E203, W503, # These default to arguing with Black. We might configure some of them eventually
|
Q0, E501, C812, E203, W503, # These default to arguing with Black. We might configure some of them eventually
|
||||||
|
|
|
@ -1,2 +1,69 @@
|
||||||
"""JARVIS background tasks."""
|
"""JARVIS background tasks."""
|
||||||
__version__ = "0.1.0"
|
import asyncio
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from dis_snek import Intents, Snake
|
||||||
|
from jarvis_core.db import connect
|
||||||
|
from jarvis_core.log import get_logger
|
||||||
|
|
||||||
|
from jarvis_tasks.config import TaskConfig
|
||||||
|
from jarvis_tasks.tasks import ban, reminder, twitter, warning
|
||||||
|
|
||||||
|
logger = None
|
||||||
|
debug = False
|
||||||
|
|
||||||
|
|
||||||
|
async def _start(config: Optional[str] = "config.yaml") -> None:
|
||||||
|
"""
|
||||||
|
Main start function.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config: Config path
|
||||||
|
"""
|
||||||
|
# Load config
|
||||||
|
config = TaskConfig.from_yaml(config)
|
||||||
|
|
||||||
|
# Connect to database
|
||||||
|
testing = config.mongo["database"] != "jarvis"
|
||||||
|
logger.debug(f"Connecting to database, testing={testing}")
|
||||||
|
connect(**config.mongo["connect"], testing=testing)
|
||||||
|
|
||||||
|
# Get event loop
|
||||||
|
loop = asyncio.get_event_loop()
|
||||||
|
|
||||||
|
# Login as bot
|
||||||
|
logger.debug("Logging in bot")
|
||||||
|
intents = Intents.DEFAULT | Intents.GUILD_MEMBERS
|
||||||
|
bot = Snake(intents=intents, loop=loop)
|
||||||
|
await bot.login(config.token)
|
||||||
|
logger.info(f"Logged in as {bot.user.username}#{bot.user.discriminator}")
|
||||||
|
|
||||||
|
# Start tasks
|
||||||
|
try:
|
||||||
|
logger.debug("Starting tasks")
|
||||||
|
functions = [ban.unban, reminder.remind, twitter.twitter, warning.unwarn]
|
||||||
|
tasks = [loop.create_task(f(bot, logger)) for f in functions]
|
||||||
|
for task in tasks:
|
||||||
|
await task
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
for task in tasks:
|
||||||
|
task.cancel()
|
||||||
|
|
||||||
|
|
||||||
|
def start(config: Optional[str] = "config.yaml") -> None:
|
||||||
|
"""
|
||||||
|
Start the background tasks.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
config: Config path
|
||||||
|
"""
|
||||||
|
global logger, debug
|
||||||
|
# Set log level
|
||||||
|
_config = TaskConfig.from_yaml(config)
|
||||||
|
debug = _config.log_level == "DEBUG"
|
||||||
|
logger = get_logger(__name__, debug=debug)
|
||||||
|
logger.setLevel(_config.log_level)
|
||||||
|
|
||||||
|
# Run the main tasks
|
||||||
|
logger.debug("Starting asyncio")
|
||||||
|
asyncio.run(_start(config))
|
||||||
|
|
4
poetry.lock
generated
4
poetry.lock
generated
|
@ -200,7 +200,7 @@ plugins = ["setuptools"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jarvis-core"
|
name = "jarvis-core"
|
||||||
version = "0.3.0"
|
version = "0.4.1"
|
||||||
description = ""
|
description = ""
|
||||||
category = "main"
|
category = "main"
|
||||||
optional = false
|
optional = false
|
||||||
|
@ -218,7 +218,7 @@ umongo = "^3.1.0"
|
||||||
type = "git"
|
type = "git"
|
||||||
url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git"
|
url = "https://git.zevaryx.com/stark-industries/jarvis/jarvis-core.git"
|
||||||
reference = "main"
|
reference = "main"
|
||||||
resolved_reference = "b8db789590bc2b5148e19f15e86167403b3e8cc3"
|
resolved_reference = "f57d79c01a1781bf299b7ea504db60767a808545"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "jedi"
|
name = "jedi"
|
||||||
|
|
4
run.py
Normal file
4
run.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
from jarvis_tasks import start
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
start()
|
Loading…
Add table
Reference in a new issue