How to prevent genserver to start when running seeds.ex file

hi,

how can I prevent genserver to start when running the seeds.ex file?

I have this cronjob that is running when I start the server, but I noticed that it starts too when I run the seeds.ex file. It wouldn’t be so bad but when the script is finished then the cronjob stops and that is bad for my data.

I want somehow the genserver to start only when I start the server.

Thanks

:wave:

How is this genserver started normally? Does something make it start in seeds.exs?

seeds.exs does need your application to be started. So it will start of course everything in your application. The hole supervision tree.

But also the seeds.exs isn’t meant to be run periodically. Usually its meant to be run once at an initial deploy of the application. Thats why it is called seeds. One usually doesn’t throw more seeds on the acre when the wheat is already growing…

1 Like

I have digged a bit.

mix run --no-start seeds.exs might work, but then you have do a lot of bootstrapping on your own, like starting your repo, the connection pool, other needed stuff.

But I still think it is a bad idea.

1 Like

yes but the cronjob puts some data in the database and suddenly is closed, leaving things in air.

I can postpone the cronjob for a minute just to be sure, but is kind of smelly :slight_smile:

I do assume your app is running as a release? Have you considered a “custom command” as described in the running migrations section?

That will actually run in th context of your already started application and not spawn a new one in parallel.

If you are not running a release created by distillery you can use other means to connect to the already running node and make it run your database updates.

Or last but not least, you can of course create those in plain SQL and just run them through your database…

2 Likes

No, it’s in dev mode.

Even when run via mix only, you can still remote shell into your application and run arbitrary functions. This is even possible via Cron.

But if it’s in dev, why bother at all? Just stop your application, run maintenance task and start the application again.

Dev is for development, not for production use.

My app/server is always stopped when I run my seeds.ex file.
This is how I register my cronjob:

defmodule MyApp.Application do

def start(_type, _args) do

children = [

supervisor(MyApp.MyCronjob, [])
]

You might not see my situation.

I have the seeds file, the app, and some genserver.

When I do some task first thing I reset the app:
mix echo.reset
This will execute seeds and start genserver(for 1 sec or so)

Then I start the app:
mix pix.server
This will start the app and the genserver.

seeds puts some test data in the database.
Genserver will run once at 10 minutes so that it can detect some changes and copy data to my database.

What I want is:

When I do some task first thing I reset the app:
mix echo.reset
This will execute seeds

Then I start the app:
mix phx.server
This will start the app and the genserver.