Skip to content

Commit

Permalink
Adding the ability to cancel a running convert-library process
Browse files Browse the repository at this point in the history
  • Loading branch information
crocodilestick committed Dec 13, 2024
1 parent 580694d commit 254124e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
71 changes: 40 additions & 31 deletions root/app/calibre-web/cps/cwa_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

import subprocess
import sqlite3
import os.path
from pathlib import Path
from time import sleep

import json
from threading import Thread
import queue
import os
import tempfile

import sys
sys.path.insert(1, '/app/calibre-web-automated/scripts/')
Expand Down Expand Up @@ -236,40 +239,46 @@ def cwa_flash_status():
return redirect(url_for('admin.admin'))


def convert_library_start():
subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py'])
def convert_library_start(queue):
cl_process = subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py'])
queue.put(cl_process)

def kill_convert_library(queue):
trigger_file = Path("/config/.kill_convert_library_trigger")
while True:
sleep(0.1)
if trigger_file.exists():
cl_process = queue.get()
cl_process.terminate()
os.remove(tempfile.gettempdir() + '/convert_library.lock')
os.remove(trigger_file)
with open("/config/convert-library.log", 'a') as f:
f.write("\nCONVERT LIBRARY PROCESS TERMINATED BY USER")
break

@convert_library.route('/cwa-library-convert', methods=['GET'])
def start_conversion():
open('/config/convert-library.log', 'w').close() # Wipe conversion log from previous runs
t1 = Thread(target=convert_library_start)
t1.start()
# Queue to share the subprocess reference
process_queue = queue.Queue()
# Create and start the subprocess thread
cl_thread = Thread(target=convert_library_start, args=(process_queue,))
cl_thread.start()
# Create and start the kill thread
cl_kill_thread = Thread(target=kill_convert_library, args=(process_queue,))
cl_kill_thread.start()
return render_title_template('cwa_convert_library.html', title=_("Calibre-Web Automated - Convert Library"), page="cwa-library-convert",
target_format=CWA_DB().cwa_settings['auto_convert_target_format'].upper())

@convert_library.route('/convert-library-status', methods=['GET'])
def getStatus():
with open("/config/convert-library.log", 'r') as f:
status = f.read()
statusList = {'status':status}
return json.dumps(statusList)


# def flask_logger():
# subprocess.Popen(['python3', '/app/calibre-web-automated/scripts/convert_library.py'])
# if os.path.isfile("/config/convert-library.log") == False:
# with open('/config/convert-library.log', 'w') as create_new_log:
# pass
# with open("/config/convert-library.log", 'r') as log_info:
# while True:
# data = log_info.read()
# yield data.encode()
# sleep(1)
# if "FIN" in data:
# break

# @convert_library.route("/cwa-library-convert", methods=["GET", "POST"])
# @login_required_if_no_ano
# @admin_required
# def cwa_library_convert():
# return Response(flask_logger(), mimetype="text/plain", content_type="text/event-stream")
@convert_library.route('/convert-library-status', methods=['GET', 'POST'])
def get_status():
if request.method == "POST" and request.form['cancel_button'] == "Cancel":
open("/config/.kill_convert_library_trigger", 'w').close()
return render_title_template('cwa_convert_library.html', title=_("Calibre-Web Automated - Convert Library"), page="cwa-library-convert",
target_format=CWA_DB().cwa_settings['auto_convert_target_format'].upper())

elif request.method == "GET":
with open("/config/convert-library.log", 'r') as f:
status = f.read()
statusList = {'status':status}
return json.dumps(statusList)
2 changes: 1 addition & 1 deletion root/app/calibre-web/cps/templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ <h2>{{_('CWA Admin Functions⚡')}}</h2>
<a class="btn btn-default" id="library_convert" href="{{url_for('convert_library.start_conversion')}}">{{_('Convert Library to Target Format')}}</a>
<a class="btn btn-default" id="cwa_history" href="{{url_for('cwa_history.cwa_history_show')}}">{{_('Show CWA History')}}</a>
</div>
<div class="row-form-group">
<div class="row form-group">
<a class="btn btn-default github" id="cwa_github_link" href="https://github.com/crocodilestick/Calibre-Web-Automated/">{{_('CWA GitHub')}}</a>
<a class="btn btn-default discord" id="cwa_discord_link" href="https://discord.gg/EjgSeek94R">{{_('CWA Discord Server')}}</a>
</div>
Expand Down
22 changes: 16 additions & 6 deletions root/app/calibre-web/cps/templates/cwa_convert_library.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
<h2>{{title}}</h2>
<div>
<h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {{target_format}}</h3><br>
<p style="font-size: xx-small;">
The Library Conversion Process automatically starts upon loading this page and therefore has already started,
you can follow it's progress below. Leaving this page will not interrupt the process but you will be unable to
return to check on it's process here in the Web UI after leaving, you would then need to check it's progress
in the container's logs.</p>
<div style="display: flex;
justify-content: space-between;
gap: 20px;">
<p style="font-size: xx-small;max-width: 80%;">
The Library Conversion Process automatically starts upon loading this page and therefore has already started,
you can follow it's progress below. Leaving this page will not interrupt the process but you will be unable to
return to check on it's process here in the Web UI after leaving, you would then need to check it's progress
in the container's logs.</p>
<form action="{{ url_for('convert_library.get_status')}}" method="post">
<input class="btn btn-default" type="submit" name="cancel_button" value="Cancel"
style="vertical-align: top;
float: right;
width: 100px;">Cancel</button>
</form>
</div>
<div class="row">
<div class="logging_window" style="padding-left: 15px;
padding-right: 15px;
Expand All @@ -32,7 +42,7 @@ <h3 style="color: whitesmoke;">CWA Library Convertor - Current Target Format - {
let get;

try {
const res = await fetch("/convert-library-status");
const res = await fetch("{{ url_for('convert_library.get_status')}}");
get = await res.json();
} catch (e) {
console.error("Error: ", e);
Expand Down
4 changes: 2 additions & 2 deletions scripts/convert_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def print_and_log(string) -> None:
lock.close()
except FileExistsError:
print_and_log("[convert-library]: CANCELLING... convert-library was initiated but is already running")
print_and_log("FIN")
print_and_log("\nDone!")
sys.exit(2)

# Defining function to delete the lock on script exit
Expand Down Expand Up @@ -256,7 +256,7 @@ def convert_to_kepub(self, filepath:str ,import_format:str) -> tuple[bool, str]:
) as process:
for line in process.stdout: # Read from the combined stdout (which includes stderr)
print_and_log(line)

if self.cwa_settings['auto_backup_conversions']:
shutil.copy2(filepath, f"/config/processed_books/converted")

Expand Down

0 comments on commit 254124e

Please sign in to comment.