mirror of
https://github.com/vale981/dirkules
synced 2025-03-04 17:01:40 -05:00
improved Telegram Logging, created task for cleaning
This commit is contained in:
parent
2ef8ba9fee
commit
ea75f03571
5 changed files with 37 additions and 15 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -52,7 +52,7 @@ coverage.xml
|
||||||
*.pot
|
*.pot
|
||||||
|
|
||||||
# Django stuff:
|
# Django stuff:
|
||||||
*.log
|
*.log*
|
||||||
local_settings.py
|
local_settings.py
|
||||||
db.sqlite3
|
db.sqlite3
|
||||||
|
|
||||||
|
|
|
@ -1,17 +1,15 @@
|
||||||
from logging import Handler
|
from logging import Handler
|
||||||
import urllib.request
|
|
||||||
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class TelegramHandler(Handler):
|
class TelegramHandler(Handler):
|
||||||
def __init__(self, token=None, chat_id=None):
|
def __init__(self, token=None, chat_id=None):
|
||||||
Handler.__init__(self)
|
Handler.__init__(self)
|
||||||
self.communicator = TelegramCom(token, chat_id)
|
self.communicator = TelegramCom(token, chat_id)
|
||||||
self.test_number = 1
|
|
||||||
|
|
||||||
def emit(self, record):
|
def emit(self, record):
|
||||||
if self.test_number != 0:
|
self.communicator.send_logging_emit(self.format(record).split("#"))
|
||||||
self.communicator.send_logging_emit(self.format(record).split("#"))
|
|
||||||
self.test_number = 0
|
|
||||||
|
|
||||||
|
|
||||||
class TelegramCom:
|
class TelegramCom:
|
||||||
|
@ -21,10 +19,11 @@ class TelegramCom:
|
||||||
self.api_url = "https://api.telegram.org/bot"
|
self.api_url = "https://api.telegram.org/bot"
|
||||||
|
|
||||||
def _send_message(self, message):
|
def _send_message(self, message):
|
||||||
my_url = self.api_url + self.token + "/sendMessage?chat_id=" + self.chat_id + "&text=" + message + "&parse_mode=Markdown"
|
my_url = self.api_url + self.token + "/sendMessage?chat_id=" + self.chat_id + "&parse_mode=Markdown" + \
|
||||||
response = urllib.request.urlopen(my_url).read()
|
"&text=" + message
|
||||||
|
response = requests.get(my_url)
|
||||||
|
# response.text contains ok with value true/false
|
||||||
|
|
||||||
def send_logging_emit(self, record):
|
def send_logging_emit(self, record):
|
||||||
print(record)
|
message = '*{}*%0AWhere: `{}`%0AWhat: `{}`'.format(record[0], record[1], record[2])
|
||||||
message = '*{}* %0A hi'.format(record[0])
|
|
||||||
self._send_message(message)
|
self._send_message(message)
|
|
@ -1,8 +1,9 @@
|
||||||
import os
|
import os
|
||||||
from dirkules.telegram_config import *
|
from dirkules.telegram_config import *
|
||||||
import datetime
|
import datetime
|
||||||
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
|
||||||
from logging.config import dictConfig
|
from logging.config import dictConfig
|
||||||
|
from apscheduler.jobstores.base import ConflictingIdError
|
||||||
|
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
|
||||||
|
|
||||||
# from apscheduler.jobstores.memory import MemoryJobStore
|
# 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_DATABASE_URI = 'sqlite:///' + os.path.join(baseDir, 'dirkules.db')
|
||||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
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_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.
|
# 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
|
# 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_JOBSTORES = {'default': MemoryJobStore()}
|
||||||
|
|
||||||
SCHEDULER_EXECUTORS = {'default': {'type': 'threadpool', 'max_workers': 3}}
|
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
|
SCHEDULER_API_ENABLED = True
|
||||||
|
|
||||||
|
@ -36,6 +46,13 @@ JOBS = [
|
||||||
'next_run_time': datetime.datetime.now(),
|
'next_run_time': datetime.datetime.now(),
|
||||||
'replace_existing': True,
|
'replace_existing': True,
|
||||||
'seconds': 3600
|
'seconds': 3600
|
||||||
|
},
|
||||||
|
{
|
||||||
|
'id': 'cleaning',
|
||||||
|
'func': 'dirkules.tasks:cleaning',
|
||||||
|
'trigger': 'cron',
|
||||||
|
'replace_existing': False,
|
||||||
|
'hour': 12
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -64,10 +81,11 @@ dictConfig({
|
||||||
'backupCount': 90,
|
'backupCount': 90,
|
||||||
},
|
},
|
||||||
'telegram': {
|
'telegram': {
|
||||||
'class': 'dirkules.Telegram_Log_New.TelegramHandler',
|
'class': 'dirkules.TelegramLogging.TelegramHandler',
|
||||||
'formatter': 'telegram',
|
'formatter': 'telegram',
|
||||||
'token': TOKEN,
|
'token': TOKEN,
|
||||||
'chat_id': CHAT_ID,
|
'chat_id': CHAT_ID,
|
||||||
|
'level': 'WARNING',
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'root': {
|
'root': {
|
||||||
|
|
|
@ -1,7 +1,12 @@
|
||||||
import dirkules.manager.driveManager as drive_man
|
import dirkules.manager.driveManager as drive_man
|
||||||
|
import dirkules.manager.cleaning as clean_man
|
||||||
|
|
||||||
|
|
||||||
def refresh_disks():
|
def refresh_disks():
|
||||||
drive_man.get_drives()
|
drive_man.get_drives()
|
||||||
drive_man.get_partitions()
|
drive_man.get_partitions()
|
||||||
drive_man.pool_gen()
|
drive_man.pool_gen()
|
||||||
|
|
||||||
|
|
||||||
|
def cleaning():
|
||||||
|
clean_man.clean_folders()
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -10,7 +10,7 @@ setup(
|
||||||
'Flask-SQLAlchemy',
|
'Flask-SQLAlchemy',
|
||||||
'Flask-APScheduler',
|
'Flask-APScheduler',
|
||||||
'APScheduler',
|
'APScheduler',
|
||||||
'Flask-WTF',
|
'Flask-WTF', 'requests'
|
||||||
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue