diff --git a/bin/ffx.py b/bin/ffx.py index 2a84b71..d6e4ba2 100755 --- a/bin/ffx.py +++ b/bin/ffx.py @@ -168,6 +168,10 @@ def unmux(ctx, existingSourcePaths = [p for p in paths if os.path.isfile(p)] ctx.obj['logger'].debug(f"\nUnmuxing {len(existingSourcePaths)} files") + ctx.obj['resource_limits'] = {} + ctx.obj['resource_limits']['niceness'] = nice + ctx.obj['resource_limits']['cpu_percent'] = cpu + for sourcePath in existingSourcePaths: fp = FileProperties(ctx.obj, sourcePath) @@ -200,8 +204,8 @@ def unmux(ctx, if unmuxSequence: if not ctx.obj['dry_run']: - ctx.obj['logger'].debug(f"Executing unmuxing sequence: {' '.join(unmuxSequence)}") - out, err, rc = executeProcess(unmuxSequence, niceness=nice, cpu_percent=cpu) + ctx.obj['logger'].debug(f"Executing unmuxing sequence") + out, err, rc = executeProcess(unmuxSequence, context = ctx.obj) if rc: ctx.obj['logger'].error(f"Unmuxing of stream {trackDescriptor.getIndex()} failed with error ({rc}) {err}") else: diff --git a/bin/ffx/ffx_controller.py b/bin/ffx/ffx_controller.py index f1e28ba..67951d0 100644 --- a/bin/ffx/ffx_controller.py +++ b/bin/ffx/ffx_controller.py @@ -269,10 +269,10 @@ class FfxController(): commandSequence += self.generateOutputTokens(targetPath, targetFormat) - self.__logger.debug(f"FfxController.runJob() commandSequence:{' '.join(commandSequence)}") + self.__logger.debug(f"FfxController.runJob(): Running command sequence") if not self.__context['dry_run']: - executeProcess(commandSequence, niceness=self.__niceness, cpu_percent=self.__cpuPercent) + executeProcess(commandSequence, context = self.__context) if videoEncoder == VideoEncoder.VP9: @@ -294,13 +294,13 @@ class FfxController(): commandSequence1 += FfxController.NULL_TOKENS - self.__logger.debug(f"FfxController.runJob() commandSequence1:{' '.join(commandSequence1)}") - if os.path.exists(FfxController.TEMP_FILE_NAME): os.remove(FfxController.TEMP_FILE_NAME) + self.__logger.debug(f"FfxController.runJob(): Running command sequence 1") + if not self.__context['dry_run']: - executeProcess(commandSequence1, niceness=self.__niceness, cpu_percent=self.__cpuPercent) + executeProcess(commandSequence1, context = self.__context) commandSequence2 = (commandTokens + self.__targetMediaDescriptor.getImportFileTokens() @@ -319,10 +319,10 @@ class FfxController(): commandSequence2 += self.generateOutputTokens(targetPath, targetFormat) - self.__logger.debug(f"FfxController.runJob() commandSequence2:{' '.join(commandSequence2)}") + self.__logger.debug(f"FfxController.runJob(): Running command sequence 2") if not self.__context['dry_run']: - out, err, rc = executeProcess(commandSequence2, niceness=self.__niceness, cpu_percent=self.__cpuPercent) + out, err, rc = executeProcess(commandSequence2, context = self.__context) if rc: raise click.ClickException(f"Command resulted in error: rc={rc} error={err}") @@ -349,4 +349,4 @@ class FfxController(): str(length), path] - out, err, rc = executeProcess(commandTokens, niceness=self.__niceness, cpu_percent=self.__cpuPercent) + out, err, rc = executeProcess(commandTokens, context = self.__context) diff --git a/bin/ffx/file_properties.py b/bin/ffx/file_properties.py index 8106d08..b78b6ef 100644 --- a/bin/ffx/file_properties.py +++ b/bin/ffx/file_properties.py @@ -101,7 +101,8 @@ class FileProperties(): "-hide_banner", "-show_format", "-of", "json", - self.__sourcePath]) + self.__sourcePath], + context = self.context) if 'Invalid data found when processing input' in ffprobeError: raise Exception(f"File {self.__sourcePath} does not contain valid stream data") @@ -160,7 +161,8 @@ class FileProperties(): "-hide_banner", "-show_streams", "-of", "json", - self.__sourcePath]) + self.__sourcePath], + context = self.context) if 'Invalid data found when processing input' in ffprobeError: raise Exception(f"File {self.__sourcePath} does not contain valid stream data") diff --git a/bin/ffx/process.py b/bin/ffx/process.py index 805c4b1..6c92af4 100644 --- a/bin/ffx/process.py +++ b/bin/ffx/process.py @@ -1,27 +1,30 @@ -import subprocess, click +import subprocess, logging from typing import List -def executeProcess(commandSequence: List[str], directory: str = None, niceness: int = 99, cpu_percent: int = 0): +def executeProcess(commandSequence: List[str], directory: str = None, context: dict = None): """ niceness -20 bis +19 cpu_percent: 1 bis 99 """ - nice = int(niceness) - cpu = int(cpu_percent) - - click.echo(f"nice {nice} cpu {cpu}") + logger = (context['logger'] if not context is None + else logging.getLogger('FFX').addHandler(logging.NullHandler())) niceSequence = [] - if nice >= -20 and nice <= 19: - niceSequence += ['nice', '-n', str(nice)] - if cpu >= 1 and cpu <= 99: - niceSequence += ['cpulimit', '-l', str(cpu), '--'] + niceness = (int(context['resource_limits']['niceness']) + if 'resource_limits' in context.keys() and 'niceness' in context['resource_limits'].keys() else 99) + cpu_percent = (int(context['resource_limits']['cpu_percent']) + if 'resource_limits' in context.keys() and 'cpu_percent' in context['resource_limits'].keys() else 0) + + if niceness >= -20 and niceness <= 19: + niceSequence += ['nice', '-n', str(niceness)] + if cpu_percent >= 1: + niceSequence += ['cpulimit', '-l', str(cpu_percent), '--'] niceCommand = niceSequence + commandSequence - click.echo(f"executeProcess(): {' '.join(niceCommand)}") + logger.debug(f"executeProcess() command sequence: {' '.join(niceCommand)}") process = subprocess.Popen(niceCommand, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', cwd = directory) output, error = process.communicate() diff --git a/bin/ffx/test/helper.py b/bin/ffx/test/helper.py index f9135c3..882c21e 100644 --- a/bin/ffx/test/helper.py +++ b/bin/ffx/test/helper.py @@ -255,10 +255,9 @@ def createMediaTestFile(mediaDescriptor: MediaDescriptor, commandTokens += [outputPath] - if not logger is None: - logger.debug(f"createMediaTestFile(): Command sequence: {commandTokens}") + ctx = {'logger': logger} - out, err, rc = executeProcess(commandTokens) + out, err, rc = executeProcess(commandTokens, context = ctx) if not logger is None: if out: diff --git a/bin/ffx/test/scenario_1.py b/bin/ffx/test/scenario_1.py index 9778715..fd2f7c5 100644 --- a/bin/ffx/test/scenario_1.py +++ b/bin/ffx/test/scenario_1.py @@ -109,16 +109,10 @@ class Scenario1(Scenario): commandSequence += ['--label', variantFilenameLabel] - # if not testContext['use_jellyfin']: - # commandSequence += ['--no-jellyfin'] - commandSequence += ['--no-pattern'] commandSequence += ['--no-tmdb'] - - self._logger.debug(f"{variantLabel}: Test sequence: {commandSequence}") - - out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, niceness=self._niceness, cpu_percent=self._cpuPercent) + out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, context = self._context) if out and self._context['verbosity'] >= 9: self._logger.debug(f"{variantLabel}: Process output: {out}") diff --git a/bin/ffx/test/scenario_2.py b/bin/ffx/test/scenario_2.py index 2045eb0..809a193 100644 --- a/bin/ffx/test/scenario_2.py +++ b/bin/ffx/test/scenario_2.py @@ -97,13 +97,7 @@ class Scenario2(Scenario): '--no-prompt', '--no-signature'] - # if not testContext['use_jellyfin']: - # commandSequence += ['--no-jellyfin'] - - - self._logger.debug(f"{variantLabel}: Test sequence: {commandSequence}") - - out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, niceness=self._niceness, cpu_percent=self._cpuPercent) + out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, context = self._context) if out and self._context['verbosity'] >= 9: self._logger.debug(f"{variantLabel}: Process output: {out}") diff --git a/bin/ffx/test/scenario_4.py b/bin/ffx/test/scenario_4.py index e21a620..3fbb234 100644 --- a/bin/ffx/test/scenario_4.py +++ b/bin/ffx/test/scenario_4.py @@ -182,12 +182,7 @@ class Scenario4(Scenario): commandSequence += ['--no-prompt', '--no-signature'] - # if not testContext['use_jellyfin']: - # commandSequence += ['--no-jellyfin'] - - self._logger.debug(f"{variantLabel}: Test sequence: {commandSequence}") - - out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, niceness=self._niceness, cpu_percent=self._cpuPercent) + out, err, rc = executeProcess(commandSequence, directory = self._testDirectory, context = self._context) if out and self._context['verbosity'] >= 9: self._logger.debug(f"{variantLabel}: Process output: {out}")