itopiz

itopiz

Hi,

I have an Elixir 1.15.7 application. At first, it was intended to be installed only on Windows. Therefore I configured mix.iex with Windows-specific environment variable ALLUSERSPROFILE in my config provider:

defmodule MyApp.MixProject do
  use Mix.Project

  def project do
    [
      ...
      releases: releases(),
    ]
  end

  ...

  defp releases do
    [
      myapp: [
        include_executables_for: [:windows],
        config_providers: [
          {MyApp.Config.TOMLConfigProvider,
           "%ALLUSERSPROFILE%\\MyCompany\\MyApp\\config.toml"}
        ]
      ]
    ]
  end
end

Now, I need to provide releases for linux as well and I first would like to get rid of the Windows-specific environment variable in mix.iex.

I have adapted my config provider to be used like so:

defp releases do
  [
    myapp: [
      include_executables_for: [:unix, :windows],
      config_providers: [
        {MyApp.Config.TOMLConfigProvider,
         {:system, "MYAPP_CONFIG_DIR", "/configuration.toml"}}
      ]
    ]
  ]
end

and I have created a rel/env.bat.eex:

@echo off

set MYAPP_CONFIG_DIR=%ALLUSERSPROFILE%\MyCompany\MyApp

With that in place, I can successfully launch my release with:

C:\MyCompany\MyApp>dir
 Volume in drive C is OS
 Volume Serial Number is ************

 Directory of C:\MyCompany\MyApp

17.01.2024  15:34    <DIR>          .
17.01.2024  15:01    <DIR>          ..
17.01.2024  15:33    <DIR>          bin
17.01.2024  15:33    <DIR>          erts-13.2.2.5
17.01.2024  15:34    <DIR>          lib
17.01.2024  15:34    <DIR>          releases
17.01.2024  15:34           451 643 unins000.dat
17.01.2024  15:33         3 223 613 unins000.exe
               2 File(s)      3 675 256 bytes
               6 Dir(s)  1 530 448 859 136 bytes free

C:\MyCompany\MyApp>bin\myapp.bat start

But the goal is to install the Windows service and after doing so (i.e. bin\myapp.bat install), the problem is that the service stops immediately after it started:

C:\MyCompany\MyApp>erts-13.2.2.5\bin\erlsrv.exe start myapp_myapp
erts-13.2.2.5\bin\erlsrv.exe: Service myapp_myapp started.

I am pretty sure the env.bat is not executed when starting the Windows service. I have no log to confirm this but when I use an absolute path in my config provider instead of the {:system, "MYAPP_CONFIG_DIR", "/configuration.toml"} tuple, then the service starts and I can successfully access my web app.

Is mix release’s env.bat suppose to work with Windows service?

Showing Posts 1 to 7

itopiz

itopiz OP

Still the same problem when rel/env.bat.eex contains the final path instead of relying on ALLUSERSPROFILE:

@echo off

set MYAPP_CONFIG_DIR=C:\ProgramData\MyCompany\MyApp
itopiz

itopiz OP

After reviewing bin\myapp.bat and in particular the install part, I understand a bit better how things works.

With the help of erlsrv documentation, I removed completely my rel/env.bat.eex and instead I run the command below after running bin\myapp.bat install:

C:\MyCompany\MyApp>erts-13.2.2.5\bin\erlsrv.exe set myapp_myapp -env MYAPP_CONFIG_DIR=%ALLUSERSPROFILE%\MyCompany\MyApp
erts-13.2.2.5\bin\erlsrv.exe: Service myapp_myapp updated.

This way the Windows service starts successfully.

So the workaround works but that still does not explain why env.bat is not evaluated when starting a Windows service. Any idea?

cmo

cmo

Can you load the config in Application.start with something like vapor or does it need to be a config provider?

Do you need to set that env var or can you concat the rest of it to ALLUSERPROFILE instead of creating an env var and concatenating onto that?

If you’re open to moving it basedir has some functions to handle common paths.

itopiz

itopiz OP

All I need is to configure the app at runtime with a human-friendly config file (TOML).
I was not aware of vapor. It seems interesting and I will have a deeper look.

I am not sure I understand what you mean here.

Interesting but I did not see a function that could return the path for a typical config file for an application that runs as a Windows service, which should be (to the best of my knowledge, please correct me if I am wrong) %ALLUSERSPROFILE%\MyCompany\MyApp (C:\ProgramData\MyCompany\MyApp on Windows 10 and 11).

Also in the near future, the application will have to run on Linux (NixOS) with systemd, so such a function would have to work with these as well. My systemd unit will use ConfigurationDirectory=myapp which puts config in /etc/myapp.

cmo

cmo

You might be right. I’m just storing it in the app’s installation directory and using vapor.

Beware there is a bug with erlsrv and you might need to put the service in Delayed Start for it to start after a reboot.

itopiz

itopiz OP

I did notice this a long time ago but I did not have the time to look any further. Do you have a link to this bug?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews