...

/

Long-running Processes and Starting GenServer

Long-running Processes and Starting GenServer

Learn the drawbacks of long-running processes and how GenServers can resolve them.

Introduction

The Task module helps run single async functions, but we’ll need a sharper tool as the logic becomes more complex. This chapter explains how to create long-lived processes that also run in the background but offer greater control and more flexibility.

Problems with long-running processes

Concurrent work often takes a long time to complete, such as when we import a large amount of user data from another service or rely on third-party APIs. This presents several challenges when using the Task module. What if an API service goes offline briefly or a network error causes the task to fail? Do we need to increase the :timeout setting because the network is slow? How do we show progress to the user and provide a better user experience?

There are also times when we want to continuously run some code in the background. We may need the latest foreign currency exchange rates in a banking ...