简体   繁体   中英

keeping an activity just for production Elixir Phoenix

I am doing as written below in my project. But I only want to run it in Production mode, not in development mode, for this, I tried with Mix.env and it worked locally by giving me :dev or :prod but It didn't work on online API. It got an error as there is nothing like env.

I want to make it just for production with some kind of pattern matching function

  Task.start(fn ->
    if user |> Intercom.get_user |> intercom_user? do
      Logger.info "User '#{user.username}' already present at Intercom."
    else
      Intercom.create_user(user, user_agent, requester_ip)
    end
  end)

any help will be appreciated!

You'll have to set a config for this in the config/*.exs files as Mix is usually not included in production releases. You could just store the env value or a more meaningful name like :create_intercom_user .

In config/dev.exs and config/test.exs :

config :my_app, :create_intercom_user, false

In config/prod.exs :

config :my_app, :create_intercom_user, true

and then in your code:

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

or:

In config/config.exs :

config :my_app, :mix_env, Mix.env

and in your code:

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

(Replace :my_app with your app's name.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM