Enqueue asciicast processing job from Elixir

ex-upload
Marcin Kulik 7 years ago
parent b94af03d58
commit 033744cf1f

@ -49,6 +49,10 @@ config :asciinema, Asciinema.PngGenerator.A2png,
bin_path: System.get_env("A2PNG_BIN_PATH") || "./a2png/a2png.sh",
pool_size: String.to_integer(System.get_env("A2PNG_POOL_SIZE") || "2")
config :asciinema, :redis_url, System.get_env("REDIS_URL") || "redis://redis:6379"
config :asciinema, :poster_generator, Asciinema.Asciicasts.PosterGenerator.Sidekiq
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env}.exs"

@ -21,3 +21,5 @@ config :asciinema, Asciinema.Repo,
config :asciinema, :file_store, Asciinema.FileStore.Local
config :asciinema, Asciinema.FileStore.Local, path: "uploads/test/"
config :asciinema, :poster_generator, Asciinema.Asciicasts.PosterGenerator.Noop

@ -6,6 +6,8 @@ defmodule Asciinema do
def start(_type, _args) do
import Supervisor.Spec
redis_url = Application.get_env(:asciinema, :redis_url)
# Define workers and child supervisors to be supervised
children = [
# Start the Ecto repository
@ -15,6 +17,7 @@ defmodule Asciinema do
# Start your own worker by calling: Asciinema.Worker.start_link(arg1, arg2, arg3)
# worker(Asciinema.Worker, [arg1, arg2, arg3]),
:poolboy.child_spec(:worker, Asciinema.PngGenerator.A2png.poolboy_config(), []),
worker(Redix, [redis_url, [name: :redix]])
]
# See http://elixir-lang.org/docs/stable/elixir/Supervisor.html

@ -1,6 +1,7 @@
defmodule Asciinema.Asciicasts do
import Ecto.Query, warn: false
alias Asciinema.{Repo, Asciicast, FileStore}
alias Asciinema.Asciicasts.PosterGenerator
def get_asciicast!(id) when is_integer(id) do
Repo.get!(Asciicast, id)
@ -93,7 +94,7 @@ defmodule Asciinema.Asciicasts do
:ok = FileStore.put_file(file_store_path, tmp_file_path, content_type)
end
defp generate_poster(_asciicast) do
# TODO
defp generate_poster(asciicast) do
PosterGenerator.generate(asciicast)
end
end

@ -0,0 +1,10 @@
defmodule Asciinema.Asciicasts.PosterGenerator do
alias Asciinema.Asciicast
@doc "Generates poster for given asciicast"
@callback generate(asciicast :: %Asciicast{}) :: :ok | {:error, term}
def generate(asciicast) do
Application.get_env(:asciinema, :poster_generator).generate(asciicast)
end
end

@ -0,0 +1,5 @@
defmodule Asciinema.Asciicasts.PosterGenerator.Noop do
def generate(_asciicast) do
:ok
end
end

@ -0,0 +1,8 @@
defmodule Asciinema.Asciicasts.PosterGenerator.Sidekiq do
alias Asciinema.Asciicast
alias Asciinema.SidekiqClient
def generate(%Asciicast{id: id}) do
SidekiqClient.enqueue("AsciicastWorker", [id])
end
end

@ -0,0 +1,14 @@
defmodule Asciinema.SidekiqClient do
def enqueue(class, args, queue \\ "default") do
job = %{queue: queue,
class: class,
args: args,
enqueued_at: Timex.now |> Timex.to_unix,
jid: Crypto.random_token(24),
retry: true}
payload = Poison.encode!(job)
{:ok, _} = Redix.command(:redix, ["LPUSH", "queue:#{queue}", payload])
:ok
end
end

@ -33,6 +33,7 @@ defmodule Asciinema.Mixfile do
:poolboy,
:porcelain,
:postgrex,
:redix,
:timex,
:timex_ecto,
]]
@ -63,6 +64,7 @@ defmodule Asciinema.Mixfile do
{:poolboy, "~> 1.5"},
{:porcelain, "~> 2.0"},
{:postgrex, ">= 0.0.0"},
{:redix, ">= 0.6.1"},
{:timex, "~> 3.0"},
{:timex_ecto, "~> 3.0"},
]

@ -32,6 +32,7 @@
"porcelain": {:hex, :porcelain, "2.0.3", "2d77b17d1f21fed875b8c5ecba72a01533db2013bd2e5e62c6d286c029150fdc", [:mix], [], "hexpm"},
"postgrex": {:hex, :postgrex, "0.11.2", "139755c1359d3c5c6d6e8b1ea72556d39e2746f61c6ddfb442813c91f53487e8", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 1.0-rc", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: false]}], "hexpm"},
"ranch": {:hex, :ranch, "1.3.2", "e4965a144dc9fbe70e5c077c65e73c57165416a901bd02ea899cfd95aa890986", [:rebar3], [], "hexpm"},
"redix": {:hex, :redix, "0.6.1", "20986b0e02f02b13e6f53c79a1ae70aa83147488c408f40275ec261f5bb0a6d0", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.1", "28a4d65b7f59893bc2c7de786dec1e1555bd742d336043fe644ae956c3497fbe", [:make, :rebar], [], "hexpm"},
"timex": {:hex, :timex, "3.1.15", "94abaec8fef2436ced4d0e1b4ed50c8eaa5fb9138fc0699946ddee7abf5aaff2", [:mix], [{:combine, "~> 0.7", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.10", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 0.1.8 or ~> 0.5", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm"},
"timex_ecto": {:hex, :timex_ecto, "3.0.5", "3ec6c25e10d2c0020958e5df64d2b5e690e441faa2c2259da8bc6bd3d7f39256", [:mix], [{:ecto, "~> 2.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:timex, "~> 3.0", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm"},

Loading…
Cancel
Save