Skip to content

Commit

Permalink
Version 4.9.2 (#335)
Browse files Browse the repository at this point in the history
* Fixing #334 machine now sleeps during encoding (thanks to Don Gafford)
* Fixing After Conversion command running after every encoding (thanks to Don Gafford)
  • Loading branch information
cdgriffith authored Apr 27, 2022
1 parent 1071726 commit d8cb9d8
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 64 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Version 4.9.2

* Fixing #334 machine now sleeps during encoding (thanks to Don Gafford)
* Fixing After Conversion command running after every encoding (thanks to Don Gafford)

## Version 4.9.1

* Fixing QSV AVC command builder not working (thanks to Marco Ravich)
Expand Down
53 changes: 0 additions & 53 deletions fastflix/conversion_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import logging
from pathlib import Path
from queue import Empty
from typing import Optional
from multiprocessing import Lock

import reusables
from appdirs import user_data_dir
Expand All @@ -12,48 +10,16 @@
from fastflix.command_runner import BackgroundRunner
from fastflix.language import t
from fastflix.shared import file_date
from fastflix.models.video import Video
from fastflix.ff_queue import save_queue


logger = logging.getLogger("fastflix-core")

log_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "logs"
after_done_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "after_done_logs"

CONTINUOUS = 0x80000000
SYSTEM_REQUIRED = 0x00000001


def prevent_sleep_mode():
"""https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx"""
if reusables.win_based:
import ctypes

try:
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS | SYSTEM_REQUIRED)
except Exception:
logger.exception("Could not prevent system from possibly going to sleep during conversion")
else:
logger.debug("System has been asked to not sleep")


def allow_sleep_mode():
if reusables.win_based:
import ctypes

try:
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS)
except Exception:
logger.exception("Could not allow system to resume sleep mode")
else:
logger.debug("System has been allowed to enter sleep mode again")


@reusables.log_exception(log="fastflix-core")
def queue_worker(gui_proc, worker_queue, status_queue, log_queue):
runner = BackgroundRunner(log_queue=log_queue)
after_done_command = ""
gui_died = False
currently_encoding = False
video_uuid = None
Expand All @@ -73,7 +39,6 @@ def start_command():
encoding="utf-8",
)
logger.addHandler(new_file_handler)
prevent_sleep_mode()
currently_encoding = True
runner.start_exec(
command,
Expand All @@ -84,7 +49,6 @@ def start_command():
if currently_encoding and not runner.is_alive():
reusables.remove_file_handlers(logger)
log_queue.put("STOP_TIMER")
allow_sleep_mode()
currently_encoding = False

if runner.error_detected:
Expand All @@ -96,13 +60,6 @@ def start_command():
continue

status_queue.put(("complete", video_uuid, command_uuid))
if after_done_command:
logger.info(f"{t('Running after done command:')} {after_done_command}")
try:
runner.start_exec(after_done_command, str(after_done_path))
except Exception:
logger.exception("Error occurred while running after done command")
continue
if gui_died:
return

Expand All @@ -120,7 +77,6 @@ def start_command():
continue
except KeyboardInterrupt:
status_queue.put(("exit",))
allow_sleep_mode()
return
else:
if request[0] == "execute":
Expand All @@ -131,17 +87,8 @@ def start_command():
logger.debug(t("Cancel has been requested, killing encoding"))
runner.kill()
currently_encoding = False
allow_sleep_mode()
status_queue.put(("cancelled", video_uuid, command_uuid))
log_queue.put("STOP_TIMER")
video = None

if request[0] == "set after done":
after_done_command = request[1]
if after_done_command:
logger.debug(f'{t("Setting after done command to:")} {after_done_command}')
else:
logger.debug(t("Removing after done command"))

if request[0] == "pause encode":
logger.debug(t("Command worker received request to pause current encode"))
Expand Down
2 changes: 1 addition & 1 deletion fastflix/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__version__ = "4.9.1"
__version__ = "4.9.2"
__author__ = "Chris Griffith"
28 changes: 20 additions & 8 deletions fastflix/widgets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
get_text_color,
)
from fastflix.shared import error_message, message, time_to_number, yes_no_message, clean_file_string
from fastflix.windows_tools import show_windows_notification
from fastflix.windows_tools import show_windows_notification, prevent_sleep_mode, allow_sleep_mode
from fastflix.widgets.background_tasks import ThumbnailCreator
from fastflix.widgets.progress_bar import ProgressBar, Task
from fastflix.widgets.video_options import VideoOptions
Expand Down Expand Up @@ -1825,6 +1825,7 @@ def encode_video(self):
logger.debug(t("Starting conversion process"))

self.app.fastflix.currently_encoding = True
prevent_sleep_mode()
self.set_convert_button(False)
self.send_video_request_to_worker_queue(video_to_send)
self.disable_all()
Expand Down Expand Up @@ -1870,6 +1871,7 @@ def add_to_queue(self):
# @reusables.log_exception("fastflix", show_traceback=False)
def conversion_complete(self, success: bool):
self.paused = False
allow_sleep_mode()
self.set_convert_button()

if not success:
Expand All @@ -1890,6 +1892,7 @@ def conversion_complete(self, success: bool):
def conversion_cancelled(self, video: Video):
self.app.fastflix.worker_queue.put(Request("cancel"))
self.app.fastflix.currently_encoding = False
allow_sleep_mode()
self.set_convert_button()

exists = video.video_settings.output_path.exists()
Expand Down Expand Up @@ -1952,14 +1955,18 @@ def status_update(self, status_response):
video_to_send: Optional[Video] = None
errored = False
same_video = False

for video in self.app.fastflix.conversion_list:
if response.video_uuid == video.uuid:
video.status.running = False

if response.status == "cancelled":
video.status.cancelled = True
self.app.fastflix.currently_encoding = False
allow_sleep_mode()
self.video_options.update_queue()
return

if response.status == "complete":
video.status.current_command += 1
if len(video.video_settings.conversion_commands) > video.status.current_command:
Expand All @@ -1968,15 +1975,15 @@ def status_update(self, status_response):
break
else:
video.status.complete = True

if response.status == "error":
video.status.error = True
errored = True
break

if errored and not self.video_options.queue.ignore_errors.isChecked():
self.app.fastflix.currently_encoding = False
self.conversion_complete(success=False)
self.video_options.update_queue()
self.end_encoding()
return

if not video_to_send:
Expand All @@ -1987,32 +1994,37 @@ def status_update(self, status_response):
break

if not video_to_send:
self.app.fastflix.currently_encoding = False
self.conversion_complete(success=True)
self.video_options.update_queue()
self.end_encoding()
return

self.app.fastflix.currently_encoding = True
if not same_video and self.app.fastflix.conversion_paused:
self.app.fastflix.currently_encoding = False
self.video_options.update_queue()
return
return self.end_encoding()

self.send_video_request_to_worker_queue(video_to_send)

def end_encoding(self):
self.app.fastflix.currently_encoding = False
allow_sleep_mode()
self.video_options.queue.run_after_done()
self.video_options.update_queue()

def send_next_video(self) -> bool:
if not self.app.fastflix.currently_encoding:
for video in self.app.fastflix.conversion_list:
if video.status.ready:
video.status.running = True
self.send_video_request_to_worker_queue(video)
self.app.fastflix.currently_encoding = True
prevent_sleep_mode()
return True
return False

def send_video_request_to_worker_queue(self, video: Video):
command = video.video_settings.conversion_commands[video.status.current_command]
self.app.fastflix.currently_encoding = True
prevent_sleep_mode()

# logger.info(f"Sending video {video.uuid} command {command.uuid} called from {inspect.stack()}")

Expand Down
20 changes: 18 additions & 2 deletions fastflix/widgets/panels/queue_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
from pathlib import Path

from appdirs import user_data_dir
import reusables
from box import Box
from PySide2 import QtCore, QtGui, QtWidgets
Expand All @@ -19,6 +20,8 @@
from fastflix.shared import no_border, open_folder, yes_no_message, message, error_message
from fastflix.widgets.panels.abstract_list import FlixList
from fastflix.exceptions import FastFlixInternalException
from fastflix.windows_tools import allow_sleep_mode, prevent_sleep_mode
from fastflix.command_runner import BackgroundRunner

logger = logging.getLogger("fastflix")

Expand All @@ -38,6 +41,8 @@
},
}

after_done_path = Path(user_data_dir("FastFlix", appauthor=False, roaming=True)) / "after_done_logs"


class EncodeItem(QtWidgets.QTabWidget):
def __init__(self, parent, video: Video, index, first=False):
Expand All @@ -49,6 +54,7 @@ def __init__(self, parent, video: Video, index, first=False):
self.last = False
self.video = video.copy()
self.setFixedHeight(60)
self.after_done_action = None

self.widgets = Box(
up_button=QtWidgets.QPushButton(
Expand Down Expand Up @@ -412,6 +418,7 @@ def pause_resume_queue(self):

def pause_resume_encode(self):
if self.encode_paused:
allow_sleep_mode()
self.pause_encode.setText(t("Pause Encode"))
self.pause_encode.setIcon(self.app.style().standardIcon(QtWidgets.QStyle.SP_MediaPause))
self.app.fastflix.worker_queue.put(["resume encode"])
Expand All @@ -425,6 +432,7 @@ def pause_resume_encode(self):
"Pause Warning",
):
return
prevent_sleep_mode()
self.pause_encode.setText(t("Resume Encode"))
self.pause_encode.setIcon(self.app.style().standardIcon(QtWidgets.QStyle.SP_MediaPlay))
self.app.fastflix.worker_queue.put(["pause encode"])
Expand All @@ -441,15 +449,15 @@ def set_after_done(self):
option = self.after_done_combo.currentText()

if option == "None":
command = ""
command = None
elif option in self.app.fastflix.config.custom_after_run_scripts:
command = self.app.fastflix.config.custom_after_run_scripts[option]
elif reusables.win_based:
command = done_actions["windows"][option]
else:
command = done_actions["linux"][option]

self.app.fastflix.worker_queue.put(["set after done", command])
self.after_done_action = command

def retry_video(self, current_video):

Expand Down Expand Up @@ -494,3 +502,11 @@ def add_to_queue(self):
self.app.fastflix.conversion_list.append(copy.deepcopy(self.app.fastflix.current_video))
self.new_source()
save_queue(self.app.fastflix.conversion_list, self.app.fastflix.queue_path, self.app.fastflix.config)

def run_after_done(self):
if not self.after_done_action:
return
logger.info(f"Running after done action: {self.after_done_action}")
BackgroundRunner(self.app.fastflix.log_queue).start_exec(
self.after_done_action, str(after_done_path), shell=True
)
31 changes: 31 additions & 0 deletions fastflix/windows_tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# -*- coding: utf-8 -*-
import logging

import reusables

logger = logging.getLogger("fastflix")

tool_window = None
tool_icon = None
CONTINUOUS = 0x80000000
SYSTEM_REQUIRED = 0x00000001


def show_windows_notification(title, msg, icon_path):
Expand Down Expand Up @@ -72,3 +78,28 @@ def cleanup_windows_notification():
if tool_window:
DestroyWindow(tool_window)
UnregisterClass("FastFlix", None)


def prevent_sleep_mode():
"""https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx"""
if reusables.win_based:
import ctypes

try:
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS | SYSTEM_REQUIRED)
except Exception:
logger.exception("Could not prevent system from possibly going to sleep during conversion")
else:
logger.debug("System has been asked to not sleep")


def allow_sleep_mode():
if reusables.win_based:
import ctypes

try:
ctypes.windll.kernel32.SetThreadExecutionState(CONTINUOUS)
except Exception:
logger.exception("Could not allow system to resume sleep mode")
else:
logger.debug("System has been allowed to enter sleep mode again")

0 comments on commit d8cb9d8

Please sign in to comment.