This commit is contained in:
Benedetta 2024-03-25 15:09:32 +01:00
parent 89cd4a3b20
commit ee8391fbc7

View file

@ -10,20 +10,26 @@ def read_conf():
conf['configuration']['mq_user'], conf['configuration']['mq_user'],
conf['configuration']['mq_password']) conf['configuration']['mq_password'])
def remaining_space_gb(): def remaining_space_gb():
from shutil import disk_usage from shutil import disk_usage
total, used, free = disk_usage("/") total, used, free = disk_usage("/")
return free / 1024**3 return free / 1024**3
def pop(): def all_transcodes():
folder_path = '/var/lib/jellyfin/transcodes' folder_path = '/var/lib/jellyfin/transcodes'
files = os.listdir(folder_path) files = os.listdir(folder_path)
a = [] a = []
for file in files: for file in files:
file_path = os.path.join(folder_path, file) file_path = os.path.join(folder_path, file)
creation_time = os.path.getctime(file_path) creation_time = os.path.getctime(file_path)
a.append((file, creation_time)) space = os.path.getsize(file_path)
a.append((file, creation_time, space))
return a
def pop(files):
if a: if a:
a.sort(key=lambda t: t[1]) a.sort(key=lambda t: t[1])
return a[0] return a[0]
@ -31,7 +37,7 @@ def pop():
return None return None
def send_to_mq(mq_host, mq_user, mq_pass, space_rem): def send_to_mq(mq_host, mq_user, mq_pass, space_rem, transcode_space):
import pika import pika
import json import json
creds = pika.PlainCredentials(mq_user, mq_pass) creds = pika.PlainCredentials(mq_user, mq_pass)
@ -40,7 +46,7 @@ def send_to_mq(mq_host, mq_user, mq_pass, space_rem):
channel = conn.channel() channel = conn.channel()
space_rem = round(space_rem, 2) space_rem = round(space_rem, 2)
msg = f'Paperino, manca spazio su disco: {space_rem}' msg = f'Paperino, manca spazio su disco: {space_rem}|{transcode_space}'
ROOM_ID = '!KABwGlTSmXAbzCOhCX:goulash.lezzo.org' ROOM_ID = '!KABwGlTSmXAbzCOhCX:goulash.lezzo.org'
response = { response = {
@ -60,14 +66,21 @@ def send_to_mq(mq_host, mq_user, mq_pass, space_rem):
def main(mq_host, mq_user, mq_pass): def main(mq_host, mq_user, mq_pass):
while True: while True:
space_rem = remaining_space_gb() space_rem = remaining_space_gb()
send_to_mq(mq_host, mq_user, mq_pass, space_rem)
if space_rem < 1.0: if space_rem >= 1.0:
send_to_mq(mq_host, mq_user, mq_pass, space_rem) time.sleep(60)
else:
files = all_transcodes()
total_space = sum(map(lambda t: t[2], files))
send_to_mq(mq_host, mq_user, mq_pass, space_rem, total_space)
while remaining_space_gb() < 2.0: while remaining_space_gb() < 2.0:
file = pop() file = pop(files)
if not file:
break
else:
os.remove(file) os.remove(file)
time.sleep(60) files = all_transcodes()
if __name__ == '__main__': if __name__ == '__main__':