diff --git a/.iex.exs b/.iex.exs index ebe1a24..da010ee 100644 --- a/.iex.exs +++ b/.iex.exs @@ -22,4 +22,7 @@ alias Pinchflat.Metadata.MetadataFileHelpers alias Pinchflat.SlowIndexing.FileFollowerServer +# TODO: remove +alias Pinchflat.KilledWorker + Pinchflat.Release.check_file_permissions() diff --git a/dev.Dockerfile b/dev.Dockerfile index bbf2683..9acbd3c 100644 --- a/dev.Dockerfile +++ b/dev.Dockerfile @@ -8,7 +8,7 @@ FROM ${DEV_IMAGE} # Install debian packages RUN apt-get update -qq RUN apt-get install -y inotify-tools ffmpeg curl git openssh-client \ - python3 python3-pip python3-setuptools python3-wheel python3-dev locales + python3 python3-pip python3-setuptools python3-wheel python3-dev locales procps # Install nodejs RUN curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh diff --git a/lib/pinchflat/killed_worker.ex b/lib/pinchflat/killed_worker.ex new file mode 100644 index 0000000..72fdadd --- /dev/null +++ b/lib/pinchflat/killed_worker.ex @@ -0,0 +1,58 @@ +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 diff --git a/slow.sh b/slow.sh new file mode 100755 index 0000000..dcbae27 --- /dev/null +++ b/slow.sh @@ -0,0 +1,8 @@ +#!/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" diff --git a/wrapper.sh b/wrapper.sh new file mode 100755 index 0000000..6121c23 --- /dev/null +++ b/wrapper.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +# Start the program in the background +exec "$@" & +pid1=$! + +# Silence warnings from here on +exec >/dev/null 2>&1 + +# Read from stdin in the background and +# kill running program when stdin closes +exec 0<&0 $( + while read; do :; done + kill -KILL $pid1 +) & +pid2=$! + +# Clean up +wait $pid1 +ret=$? +kill -KILL $pid2 +exit $ret