I would like to make a function check if my app is in production mode. What would be the difference between these 2 methods?

1- Set a config (:mix_env) and then get it in the function

if Application.get_env(:my_app, :mix_env) == :prod do
  Task.start(fn -> ... end)
end

Or
2- Use directly Mix.env in the function

if Mix.env == :prod do
  Task.start(fn -> ... end)
end

Is there any advantage setting a config value here ?

Curious @Kurisu, do you have a bit more context around this issue? Are you only trying to run a Task in production? I think having a bit more detail / context around your problem could even help people who are answering the question.

In general, in most Elixir codebases (at least the ones I’ve seen), people tend to set environment specific config values (e.g., for API keys - sandbox and production) when the environment makes a difference rather than calling Mix.env directly.

1 Like

Dont do this. This will breake in releases, as mix doesn’t get bundled into them.

Make yourself a config that has a proper meaning, eg run_the_task rather then mix_env.

6 Likes

@treble37
Here the context… In dev environment I configured Arc uploader to store pictures on local disk, but in production on a storage like Amazon S3. So to delete a file in dev mode, I’m using « File.rm_rf!/1 » but this won’t work wiith S3 (I will use Arc deleting funtion for this case).

@NobbZ
Thank you. I did well by asking the question then. ^^

Shouldn’t arc be able to handle this?

Maybe… I just found it easier some times ago to delete the files from the local disk with my own function when in dev environment…
I will try again the Arc delete function in dev with local storage and if it works as expected, then good!

If arc does not handle it correctly, it’s worth a bug report in my opinion, as in a good API those functions should be totally agnostic to the actual storing backend.

1 Like

This would work anywhere (in a function invocation):

if unquote(Mix.env == :prod) do
  Task.start(fn -> ... end)
end
1 Like

It is still not the way to do. I’d be happy if you weren’t proposing this everywhere.

Even though it works, it is rarely the thing you want to do to branch on the actual environment.

In this thread the actual thing we want to branch on (arc doesn’t handle it corrently) is the backend configured for arc.

Using Mix anywhere but in a mix-task or the mix.exs is rarely correct.

2 Likes

I feel like I’m proposing an immediate solution to other people’s immediate problems. They can use it until, as you suggested above, arc fixes this “bug”. And, as always, other users are free to suggest more idiomatic approaches if they don’t agree with me. There is zero reason to not be happy about it.

It’s good to know I can even do something like that! ^^

I understand too, the way I should use configs…

Finally I have to say that Arc function for files deletion works. I just have not to store files in subfolders because Arc would let plenty empty folders after deletion. I was using subfolders so I can delete all versions of a picture by deleting a subfolder, but Arc delete them one by one and let an empty subfolder.

Anyway thank you guys!

I almost never see this sentiment actually working in practice. People code something, ugly hack or not, check that it works, and they never touch it again.

That’s how tech debt creeps in.

2 Likes