From 070ef5b84678a7e76b2dc44c32e0bd512efcf1a2 Mon Sep 17 00:00:00 2001 From: tianyi Date: Mon, 29 Apr 2024 15:05:51 +0000 Subject: [PATCH] added cronjobs --- .gitignore | 4 +++- backend/randpic/update_pics.py | 20 ++++++++++++++++++++ backend/tianyi/settings.py | 6 ++++++ backend/update_pics.py | 19 ------------------- 4 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 backend/randpic/update_pics.py delete mode 100644 backend/update_pics.py diff --git a/.gitignore b/.gitignore index a635b4c..879a4f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ +backend/__pycache__ backend/randpic/__pycache__ backend/tianyi/__pycache__ backend/db.sqlite3 backend/secret.txt -backend/jsons/pics.json \ No newline at end of file +backend/jsons +backend/logs \ No newline at end of file diff --git a/backend/randpic/update_pics.py b/backend/randpic/update_pics.py new file mode 100644 index 0000000..d786dbc --- /dev/null +++ b/backend/randpic/update_pics.py @@ -0,0 +1,20 @@ +# Description: This script is used to update the list of pictures in the Azure Blob Storage container. +from azure.storage.blob import BlobServiceClient +import os +import json +def update_pictures(container='tianyi-random', remotedir='', secretdir='/home/tianyi/repos/TianyiMoe/backend/secret.txt', jsondir='/home/tianyi/repos/TianyiMoe/backend/jsons/pics.json'): + # print(os.getcwd()) + try: + with open(secretdir, 'r') as f: + connect_str = f.read() + blob_service_client = BlobServiceClient.from_connection_string(connect_str) + container_client = blob_service_client.get_container_client(container) + existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=remotedir)] + print("File number: " + str(len(existing_files))) + with open(jsondir, 'w') as f: + json.dump(existing_files, f, indent=4, sort_keys=True) + except Exception as e: + print(e) + +if __name__ == '__main__': + update_pictures() \ No newline at end of file diff --git a/backend/tianyi/settings.py b/backend/tianyi/settings.py index dad57ff..4eee04b 100644 --- a/backend/tianyi/settings.py +++ b/backend/tianyi/settings.py @@ -37,6 +37,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django_crontab', ] MIDDLEWARE = [ @@ -121,3 +122,8 @@ STATIC_URL = 'static/' # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' + +# Crontab settings +CRONJOBS = [ + ('* * * * *', 'randpic.update_pics.update_pictures', '>> /home/tianyi/repos/TianyiMoe/backend/logs/crontab.log') +] \ No newline at end of file diff --git a/backend/update_pics.py b/backend/update_pics.py deleted file mode 100644 index 78b2268..0000000 --- a/backend/update_pics.py +++ /dev/null @@ -1,19 +0,0 @@ -# Description: This script is used to update the list of pictures in the Azure Blob Storage container. -from azure.storage.blob import BlobServiceClient -import argparse -import json -parser = argparse.ArgumentParser() -parser.add_argument('--container', type=str, default="tianyi-random") -parser.add_argument('--remotedir', type=str, default="") -args=parser.parse_args() -try: - with open('secret.txt', 'r') as f: - connect_str = f.read() - blob_service_client = BlobServiceClient.from_connection_string(connect_str) - container_client = blob_service_client.get_container_client(args.container) - existing_files=[blob.name.split("/")[-1] for blob in container_client.list_blobs(name_starts_with=args.remotedir)] - with open('jsons/pics.json', 'w') as f: - json.dump(existing_files, f, indent=4, sort_keys=True) -except Exception as e: - print(e) - exit(1) \ No newline at end of file