Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import asyncio
- from datetime import datetime, timedelta
- import os
- from ncogs.Utils import Utils as utils
- import yaml
- import threading
- import sqlite3
- from ncogs.Utils import Utils
- from discord import app_commands
- from discord.ext import commands
- from typing import NoReturn
- class Scheduler:
- def __init__(self, guild_id: int) -> NoReturn:
- self.tasks = {}
- self.running = False
- self.server_name = Utils.get_server_name(guild_id)
- self.conn = sqlite3.connect(f'{self.server_name}/{Utils.shorten_server_name(guild_id)}.db')
- self.cursor = self.conn.cursor()
- self.cursor.execute("""CREATE TABLE IF NOT EXISTS tasks (name TEXT PRIMARY KEY, func_name TEXT, interval INTEGER, last_run TEXT, next_run TEXT)""")
- self.guild_id = guild_id
- self.schedulers = {}
- def get_scheduler(self, guild_id):
- return self.schedulers.get(guild_id)
- def set_scheduler(self, guild_id, scheduler):
- self.schedulers[guild_id] = scheduler
- async def load_tasks(self):
- self.cursor.execute("SELECT * FROM tasks")
- rows = self.cursor.fetchall()
- for row in rows:
- name, func_name, interval, last_run, next_run = row
- self.tasks[name] = {
- 'func': getattr(self.bot, func_name),
- 'interval': interval,
- 'last_run': datetime.fromisoformat(last_run),
- 'next_run': datetime.fromisoformat(next_run)
- }
- async def save_task(self, name, func_name, interval, last_run, next_run):
- self.cursor.execute("INSERT OR REPLACE INTO tasks VALUES (?, ?, ?, ?, ?)",
- (name, func_name, interval, last_run.isoformat(), next_run.isoformat()))
- self.conn.commit()
- async def start(self):
- self.running = True
- await self.check_tasks()
- async def add_task(self, name, func, interval):
- self.tasks[name] = {
- 'func': func,
- 'interval': interval,
- 'last_run': datetime.now(),
- 'next_run': datetime.now() + timedelta(seconds=interval)
- }
- await self.save_task(name, func.__name__, interval, datetime.now(), self.tasks[name]['next_run'])
- if not self.running:
- await self.start()
- async def backup_database(self, backup_type):
- ServerName = await asyncio.to_thread(Utils.get_server_name, self.guild_id)
- BasePath = await asyncio.to_thread(Utils.server_folder, ServerName)
- SourceDatabasePath = await asyncio.to_thread(Utils.primary_database_folder, BasePath, ServerName)
- BackupsRoute = await asyncio.to_thread(Utils.backup_folder, BasePath, ServerName)
- if backup_type == "full":
- await Utils.full_backup(SourceDatabasePath, BackupsRoute)
- elif backup_type == 'differential':
- await Utils.differential_backup(SourceDatabasePath, BackupsRoute)
- async def schedule_full_backup(self, db_name):
- current_time = datetime.now()
- midnight = current_time.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
- self.tasks[f"full_backup_{db_name}"] = {
- 'func': lambda: self.backup_database(db_name, 'full'),
- 'interval': 86400,
- 'last_run': current_time,
- 'next_run': midnight
- }
- await self.save_task(f"full_backup_{db_name}", 'backup_database', 86400, current_time, midnight)
- async def schedule_differential_backup(self, db_name):
- current_time = datetime.now()
- midnight = current_time.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
- self.tasks[f"differential_backup_{db_name}"] = {
- 'func': lambda: self.backup_database(db_name, 'differential'),
- 'interval': 86400,
- 'last_run': current_time,
- 'next_run': midnight
- }
- await self.save_task(f"differential_backup_{db_name}", 'backup_database', 86400, current_time, midnight)
- async def check_tasks(self):
- while self.running:
- current_time = datetime.now()
- for name, task in self.tasks.items():
- if current_time >= task['next_run']:
- if name.startswith('full_backup_'):
- await task['func']()
- task['last_run'] = current_time
- task['next_run'] = current_time + timedelta(seconds=task['interval'])
- await self.save_task(name, 'backup_database', task['interval'], task['last_run'], task['next_run'])
- elif name.startswith('differential_backup_'):
- db_name = name.split('_')[2]
- full_backup_task = self.tasks.get(f"full_backup_{db_name}")
- if full_backup_task and full_backup_task['next_run'] - current_time < timedelta(hours=1):
- task['next_run'] = current_time + timedelta(hours=1)
- await self.save_task(name, 'backup_database', task['interval'], task['last_run'], task['next_run'])
- else:
- await task['func']()
- task['last_run'] = current_time
- task['next_run'] = current_time + timedelta(seconds=task['interval'])
- await self.save_task(name, 'backup_database', task['interval'], task['last_run'], task['next_run'])
- await asyncio.sleep(3600)
- def stop(self):
- self.running = False
- self.conn.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement