beto

beto

Hello, I am working on an task, that requires to parse a csv sent from a react frontend. When the backend receives it, it should make an API call to a webservice. But now the csv is large and it’s not functional. My idea is to get the csv, answer the frontend and process the csv and API calls in the background, but I have no idea where to start. Can someone point on a tutorial or something on how to start? If there a way to avoid rabbitMQ redid would be better, if possible
Thank you very much

Showing Posts 1 to 10

joaquinalcerro

joaquinalcerro

Depending in your specific use case you can start simple with tasks:

task = Task.async(fn -> do_some_work() end)
res = do_some_other_work() res + Task.await(task)

santif

santif

Looks like you need a background job processing library.

Take a look to rihanna which uses postgres as store for jobs.

Other solutions use redis: exq, verk, toniq.

que is backed by Mnesia.

More libs:

rjk

rjk

If you do it like this (without a job processing library, which is fine) keep the things in mind that are described in this article from Chris McCord Phoenix Tips and Tricks - DockYard under the title “Avoid Task.async if you don’t plan to task.await”. The summary is that if you want to use Task.async to process your CSV file, hook it into it’s own Task.Supervisor so that it is isolated from the controller.

dokuzbir

dokuzbir

As @santif mentioned using a library better but if you wanna start quickly. You can start like that

Task.start(fn -> 
process_something() 
|> notify_phoenix_channel()
end)
kokolegorille

kokolegorille

There are many ways to do this and You might find a different approach for each answer.

to start with communication.

  • Initiate the command/request client side using channels
  • The channel answer with request received
  • The channel trigger a long pipeline of transformation
  • When the pipeline is done, the channel get the async result (for example with handle_info message) and notify the frontend via websocket with the result.

Now You have a large choice with the pipeline, GenStage, Queue… or just a process.

The only thing async is the API call, all the rest could be transformed by simple functions.

For this call, I would use a simple Task, either supervised, or not.

Anyway You can leverage with any libraries mentionned, depending on the situation :slight_smile:

joaquinalcerro

joaquinalcerro

Here is another thread that might help @beto:

Best regards,

cnck1387

cnck1387

When I first came to Elixir, this was one of the biggest questions I had.

I think it would be really beneficial if there were a few blog posts and open source projects that went over a few production-ready solutions to handle cases like this. I think it would really help people using Elixir who are coming over from Rails or other stacks with the “web app + worker + redis” mentality.

Some of the use cases could be:

  • You don’t care about the response
  • You do care about the response
  • You need it to persist across beam reloads
  • Handle rate limiting (X per minute style)
  • Handle retries (constant or exponential back-offs) and hooks for custom things to happen on error
beto

beto OP

Hello ! I appreciate your comments a lot, and I come to this solution, still not working, but I would love to see if I am on the right track:

def call_external_api(csv_file) do
  csv_file
  |> parse_csv()
  |> filter_list()
  |> map_list()
  |> prepare_tasks()
end

def prepare_tasks(list) do
  Enum.each(list, fn(element) -> create_task(element))
end

def create_task(record) do
  Task.start_link(ExternalAPI.notify(record))
end
dimitarvp

dimitarvp

For starters, you have to pass a pointer to a function, not to invoke the function immediately and pass its result:

def create_task(record) do
  Task.start_link(&(ExternalAPI.notify(record)))
end

EDIT: As pointed out by @axelson, this is what would compile (also see the comment below mine):

def create_task(record) do
  Task.start_link(fn -> ExternalAPI.notify(record) end)
end
axelson

axelson

Scenic Core Team

Actually that won’t compile because you can’t use the shorthand anonymous function syntax (using &) to define an anonymous function with 0 arguments. So you need to define the anonymous function with the explicit anonymous function syntax:

def create_task(record) do
  Task.start_link(fn -> ExternalAPI.notify(record) end)
end

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews