Zsolt

Zsolt

I’m trying to get some code to run in Flyio Flame machines.

I have a phoenix app (although, I think the question applies more generally to Elixir and Flame).

There is a live file upload module that takes a few files, and using an image library resizes, converts and saves them to disk. The path to the images is saved in a Postgres db. Very simplistic, this is a learning tool, not a production app.

Resizing and saving the files is not CPU intensive, but it can use up a lot of memory, so it would help to do each operation on a new machine. (I’m aware other kinds optimizations are possible, but the purpose of this is to try Flame).

This blog post (Rethinking Serverless with FLAME · The Fly Blog) seems to state that it’s enough to wrap a chunk of code in a FLAME.call with a FLAME.Pool and those chunks should run on separate machines.

That’s it! FLAME.call accepts the name of a runner pool, and a function. It then finds or boots a new copy of our entire application and runs the function there.

I have this implementation and I’m not seeing and Flame machines start up. Am I doing something wrong? Did I misunderstand the post, and do I in fact need the entire GenServer and FLAME.place_child functionality?

The FLAME.Pool in /lib/phoenix_albums/application.ex

  @impl true
  def start(_type, _args) do
    flame_parent = FLAME.Parent.get()

    children =
      [
        PhoenixAlbumsWeb.Telemetry,
        PhoenixAlbums.Repo,
        {DNSCluster, query: Application.get_env(:phoenix_albums, :dns_cluster_query) || :ignore},
        {Phoenix.PubSub, name: PhoenixAlbums.PubSub},
        # Start the Finch HTTP client for sending emails
        {Finch, name: PhoenixAlbums.Finch},
        # Start a worker by calling: PhoenixAlbums.Worker.start_link(arg)
        # {PhoenixAlbums.Worker, arg},
        # Start to serve requests, typically the last entry
        {FLAME.Pool,
         name: PhoenixAlbums.ImageProcessor, min: 0, max: 10, max_concurrency: 1, log: :debug},
        !flame_parent && PhoenixAlbumsWeb.Endpoint
      ]
      |> Enum.filter(& &1)

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: PhoenixAlbums.Supervisor]
    Supervisor.start_link(children, opts)
  end

My upload handler in the live fodler:

 @impl Phoenix.LiveView
  def handle_event("save", params, socket) do
    unless File.exists?(@folder) do
      File.mkdir!(@folder)
    end

    uploaded_files =
      consume_uploaded_entries(socket, :avatar, fn %{path: path}, entry ->
   
        uuid = UUID.generate()
        image_folder = "#{@folder}/#{uuid}"

        unless File.exists?(image_folder) do
          File.mkdir!(image_folder)
        end

        FLAME.call(
          PhoenixAlbums.ImageProcessor,
          fn -> resize_image(:thumbnail, path, 150, image_folder) end
        )

        FLAME.call(PhoenixAlbums.ImageProcessor, fn ->
          resize_image(:medium, path, 800, image_folder)
        end)

        FLAME.call(PhoenixAlbums.ImageProcessor, fn ->
          save_original(path, image_folder)
        end)

        image = Map.merge(params, %{"user_id" => socket.assigns.user_id, "url" => image_folder})

        case Album.create_image(image) do
          {:ok, image} ->
            socket
            |> put_flash(:info, "Image created successfully.")

            {:ok, ~p"/images/#{image}"}
        end
      end)

    {:noreply, update(socket, :uploaded_files, &(&1 ++ uploaded_files))}
  end

Here I just wrapped resize_image and save_original functions, with the expectation, that these would be run on separate machines. The code runs on fly just the same as without the wrappers, and I’m not sure why.

Showing Posts 1 to 5

chrismccord

chrismccord

Creator of Phoenix

Have you configured the FlyBackend? You can either pass the backend to the pool options, or per the FlyBackend docs:

The only required configuration is telling FLAME to use the
FLAME.FlyBackend by default and the :token which is your Fly.io API
token. These can be set via application configuration in your config/runtime.exs
withing a :prod block:

  if config_env() == :prod do
    config :flame, :backend, FLAME.FlyBackend
    config :flame, FLAME.FlyBackend, token: System.fetch_env!("FLY_API_TOKEN")
    ...
  end
Zsolt

Zsolt OP

Thank you, I completely missed that. After setting the token deployment was a little bumpy, but this helped: Deployments not working: error connecting to docker - #2 by greg - Questions / Help - Fly.io and now I can see the machines starting up straight away.

Zsolt

Zsolt OP

What determines the number of flame runners being started?

I have the following config:

application.ex

  @impl true
  def start(_type, _args) do
    flame_parent = FLAME.Parent.get()

    children =
      [
        PhoenixAlbumsWeb.Telemetry,
        PhoenixAlbums.Repo,
        {DNSCluster, query: Application.get_env(:phoenix_albums, :dns_cluster_query) || :ignore},
        {Phoenix.PubSub, name: PhoenixAlbums.PubSub},
        # Start the Finch HTTP client for sending emails
        {Finch, name: PhoenixAlbums.Finch},
        # Start a worker by calling: PhoenixAlbums.Worker.start_link(arg)
        # {PhoenixAlbums.Worker, arg},
        # Start to serve requests, typically the last entry
        {FLAME.Pool,
         name: PhoenixAlbums.ImageProcessor,
         min: 0,
         max: 10,
         max_concurrency: 1,
         single_use: true,
         log: :debug,
        !flame_parent && PhoenixAlbumsWeb.Endpoint
      ]
      |> Enum.filter(& &1)

    # See https://hexdocs.pm/elixir/Supervisor.html
    # for other strategies and supported options
    opts = [strategy: :one_for_one, name: PhoenixAlbums.Supervisor]
    Supervisor.start_link(children, opts)
  end

config/runtime.exs

  config :flame, :backend, FLAME.FlyBackend
  config :flame, FLAME.FlyBackend, token: System.fetch_env!("FLY_API_TOKEN")

and three separate cast calls to some functions.

    FLAME.cast(
      PhoenixAlbums.ImageProcessor,
      fn -> resize_image(:medium, file, uuid) end
    )

    FLAME.cast(PhoenixAlbums.ImageProcessor, fn ->
      resize_image(:thumbnail, file, uuid)
    end)

    FLAME.cast(PhoenixAlbums.ImageProcessor, fn ->
      save_original(file, uuid)
    end)

I always get one runner where all three calls are executed, but I would expect to spawn three instances. How do I organize these calls to run in separate machines?

Zsolt

Zsolt OP

There is a } and the end of the pool config:

`  log: :debug },`
willfore

willfore

You ever figure this out? I seem to be having the same issue right now. Only I’m not using single_use: true

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

CodeSync
Just a reminder for anyone still on the fence: early bird pricing for Code BEAM Europe closes on 8 September at 23:59 CEST. This is the l...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews