improved Telegram Logging, created task for cleaning

This commit is contained in:
Daniel 2019-09-11 17:19:04 +02:00
parent 2ef8ba9fee
commit ea75f03571
5 changed files with 37 additions and 15 deletions

2
.gitignore vendored
View file

@ -52,7 +52,7 @@ coverage.xml
*.pot
# Django stuff:
*.log
*.log*
local_settings.py
db.sqlite3

View file

@ -1,17 +1,15 @@
from logging import Handler
import urllib.request
import requests
class TelegramHandler(Handler):
def __init__(self, token=None, chat_id=None):
Handler.__init__(self)
self.communicator = TelegramCom(token, chat_id)
self.test_number = 1
def emit(self, record):
if self.test_number != 0:
self.communicator.send_logging_emit(self.format(record).split("#"))
self.test_number = 0
self.communicator.send_logging_emit(self.format(record).split("#"))
class TelegramCom:
@ -21,10 +19,11 @@ class TelegramCom:
self.api_url = "https://api.telegram.org/bot"
def _send_message(self, message):
my_url = self.api_url + self.token + "/sendMessage?chat_id=" + self.chat_id + "&text=" + message + "&parse_mode=Markdown"
response = urllib.request.urlopen(my_url).read()
my_url = self.api_url + self.token + "/sendMessage?chat_id=" + self.chat_id + "&parse_mode=Markdown" + \
"&text=" + message
response = requests.get(my_url)
# response.text contains ok with value true/false
def send_logging_emit(self, record):
print(record)
message = '*{}* %0A hi'.format(record[0])
message = '*{}*%0AWhere: `{}`%0AWhat: `{}`'.format(record[0], record[1], record[2])
self._send_message(message)

View file

@ -1,8 +1,9 @@
import os
from dirkules.telegram_config import *
import datetime
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from logging.config import dictConfig
from apscheduler.jobstores.base import ConflictingIdError
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
# from apscheduler.jobstores.memory import MemoryJobStore
@ -12,16 +13,25 @@ staticDir = os.path.join(baseDir, 'static')
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(baseDir, 'dirkules.db')
SQLALCHEMY_TRACK_MODIFICATIONS = False
# The SCHEDULER_JOB_DEFAULTS configuration is per job, that means each job can execute at most 3 threads at the same time.
# The SCHEDULER_EXECUTORS is a global configuration, in this case, only 1 thread will be used for all the jobs.
# I believe the best way for you is to use max_workers: 1 when running locally
SCHEDULER_JOBSTORES = {'default': SQLAlchemyJobStore(url='sqlite:///' + os.path.join(baseDir, 'dirkules_tasks.db'))}
class SQLJobStore(SQLAlchemyJobStore):
def add_job(self, job):
try:
super().add_job(job)
except ConflictingIdError:
pass
SCHEDULER_JOBSTORES = {'default': SQLJobStore(url='sqlite:///' + os.path.join(baseDir, 'dirkules_tasks.db'))}
# SCHEDULER_JOBSTORES = {'default': MemoryJobStore()}
SCHEDULER_EXECUTORS = {'default': {'type': 'threadpool', 'max_workers': 3}}
SCHEDULER_JOB_DEFAULTS = {'coalesce': False, 'max_instances': 1}
SCHEDULER_JOB_DEFAULTS = {'coalesce': True, 'max_instances': 1}
SCHEDULER_API_ENABLED = True
@ -36,6 +46,13 @@ JOBS = [
'next_run_time': datetime.datetime.now(),
'replace_existing': True,
'seconds': 3600
},
{
'id': 'cleaning',
'func': 'dirkules.tasks:cleaning',
'trigger': 'cron',
'replace_existing': False,
'hour': 12
}
]
@ -64,10 +81,11 @@ dictConfig({
'backupCount': 90,
},
'telegram': {
'class': 'dirkules.Telegram_Log_New.TelegramHandler',
'class': 'dirkules.TelegramLogging.TelegramHandler',
'formatter': 'telegram',
'token': TOKEN,
'chat_id': CHAT_ID,
'level': 'WARNING',
}
},
'root': {

View file

@ -1,7 +1,12 @@
import dirkules.manager.driveManager as drive_man
import dirkules.manager.cleaning as clean_man
def refresh_disks():
drive_man.get_drives()
drive_man.get_partitions()
drive_man.pool_gen()
def cleaning():
clean_man.clean_folders()

View file

@ -10,7 +10,7 @@ setup(
'Flask-SQLAlchemy',
'Flask-APScheduler',
'APScheduler',
'Flask-WTF',
'Flask-WTF', 'requests'
],
)