Refactored test setup into real-world fixes

This commit is contained in:
Kieran Eglin 2024-04-11 14:55:46 -07:00
parent d9d6066626
commit 5d12e53242
No known key found for this signature in database
GPG key ID: 193984967FCF432D
8 changed files with 29 additions and 71 deletions

View file

@ -22,7 +22,4 @@ alias Pinchflat.Metadata.MetadataFileHelpers
alias Pinchflat.SlowIndexing.FileFollowerServer alias Pinchflat.SlowIndexing.FileFollowerServer
# TODO: remove
alias Pinchflat.KilledWorker
Pinchflat.Release.check_file_permissions() Pinchflat.Release.check_file_permissions()

View file

@ -1,58 +0,0 @@
defmodule Pinchflat.KilledWorker do
@moduledoc false
use Oban.Worker,
queue: :default
import Ecto.Query, warn: false
require Logger
alias __MODULE__
alias Pinchflat.Repo
def kickoff(job_args \\ %{}, opts \\ []) do
job_args
|> KilledWorker.new(opts)
|> Repo.insert_unique_job()
:ok
end
def cancel do
Oban.Job
|> where(worker: "Pinchflat.KilledWorker")
|> Oban.cancel_all_jobs()
end
def start_stop do
kickoff()
Process.sleep(2000)
cancel()
end
@impl Oban.Worker
def perform(%Oban.Job{}) do
# case System.cmd("/app/wrapper.sh", ["/app/slow.sh"]) do
args = [
"/usr/local/bin/yt-dlp",
"https://www.youtube.com/@OverSimplified",
"--simulate",
"--print",
"%(title)s"
]
case System.cmd("/app/wrapper.sh", args) do
{output, 0} ->
Logger.warning("KilledWorker: #{output}")
{:ok, output}
{output, _} ->
Logger.error("KilledWorker: #{output}")
{:error, output}
end
Logger.warning("KilledWorker: done")
:ok
end
end

View file

@ -45,7 +45,7 @@ defmodule Pinchflat.Notifications.CommandRunner do
""" """
@impl AppriseCommandRunner @impl AppriseCommandRunner
def version do def version do
case System.cmd(backend_executable(), ["--version"]) do case CliUtils.wrap_cmd(backend_executable(), ["--version"]) do
{output, 0} -> {output, 0} ->
output output
|> String.split(~r{\r?\n}) |> String.split(~r{\r?\n})

View file

@ -5,6 +5,23 @@ defmodule Pinchflat.Utils.CliUtils do
alias Pinchflat.Utils.StringUtils alias Pinchflat.Utils.StringUtils
@doc """
Wraps a command in a shell script that will terminate
the command if stdin is closed. Useful for stopping
commands if the job runner is cancelled.
Delegates to `System.cmd/3` and any options/output
are passed through.
Returns {binary(), integer()}
"""
def wrap_cmd(command, args, opts \\ []) do
wrapper_command = Path.join(:code.priv_dir(:pinchflat), "cmd_wrapper.sh")
actual_command = [command] ++ args
System.cmd(wrapper_command, actual_command, opts)
end
@doc """ @doc """
Parses a list of command options into a list of strings suitable for passing to Parses a list of command options into a list of strings suitable for passing to
`System.cmd/3`. `System.cmd/3`.

View file

@ -37,7 +37,7 @@ defmodule Pinchflat.YtDlp.CommandRunner do
formatted_command_opts = [url] ++ CliUtils.parse_options(all_opts) formatted_command_opts = [url] ++ CliUtils.parse_options(all_opts)
Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}") Logger.info("[yt-dlp] called with: #{Enum.join(formatted_command_opts, " ")}")
case System.cmd(command, formatted_command_opts, stderr_to_stdout: true) do case CliUtils.wrap_cmd(command, formatted_command_opts, stderr_to_stdout: true) do
{_, 0} -> {_, 0} ->
# IDEA: consider deleting the file after reading it. It's in the tmp dir, so it's not # IDEA: consider deleting the file after reading it. It's in the tmp dir, so it's not
# a huge deal, but it's still a good idea to clean up after ourselves. # a huge deal, but it's still a good idea to clean up after ourselves.

View file

@ -1,5 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# This script is a wrapper for other programs
# that ensures they are killed when stdin closes
# (eg: a job terminates)
# Start the program in the background # Start the program in the background
exec "$@" & exec "$@" &
pid1=$! pid1=$!

View file

@ -1,8 +0,0 @@
#!/bin/bash
# Runs for N seconds
for i in {1..15}; do
echo "Slow script running for $i seconds"
sleep 1
done
echo "Slow script done"

View file

@ -3,6 +3,12 @@ defmodule Pinchflat.Utils.CliUtilsTest do
alias Pinchflat.Utils.CliUtils alias Pinchflat.Utils.CliUtils
describe "wrap_cmd/3" do
test "delegates to System.cmd/3" do
assert {"output\n", 0} = CliUtils.wrap_cmd("echo", ["output"])
end
end
describe "parse_options/1" do describe "parse_options/1" do
test "it converts symbol k-v arg keys to kebab case" do test "it converts symbol k-v arg keys to kebab case" do
assert ["--buffer-size", "1024"] = CliUtils.parse_options(buffer_size: 1024) assert ["--buffer-size", "1024"] = CliUtils.parse_options(buffer_size: 1024)