How do I change the restart policy for Machines?

I think this is similar to this Machine did not have a restart policy, defaulting to restart, which did not offer a solution that worked for me.

If one of my machines fail I don’t want it to restart. I’m not seeing where I can set the restart policy. Attempting to add it to the configuration leads to a server time out.

1 Like

When you spawn your machine you can add this to your config:

{
  ...other configs for your machine...
  "restart": {
    "policy": "no",
  },
}

Available options to policy are “no”, “always” and “on-failure”

Don’t forget that if you’re updating a machine you need to provide the full config too :slight_smile:

3 Likes

Ah okay, I tried setting it to false. Thanks!

1 Like

Neat! Is this possible with machines deployed using the flyctl deploy -c fly.toml cmd? If so, which section of the toml should this policy be defined?

1 Like

Regular fly deploy doesn’t use machine yet. Right now regular apps restart when your checks don’t pass: App Configuration (fly.toml) · Fly Docs

flyctl deploy -c ... does support Machines (but with caveats): Preview: Deploying applications on Machines with flyctl I’ve used it, and it works (for the most part).

I’d expect Machines to also restart on health-check failures (or restart on any non-zero exit code)?

Yea, it’s currently on preview right now :slight_smile:

Health checks for machines is something we are still working on :slight_smile:

1 Like

Most our services rely on health checks. Any timeline for these for Machines?

  1. If there aren’t health-checks for Machines, what does the restart-policy act on? Does it monitor the exit code to apply restart-policies?
    a. Does no mean, a new vm is started on next request?
    b. Does always mean, even if it exits with 0, the main process is restarted inside the existing vm?
    c. Does on-failure mean, only on non-zero exit codes, the main process is restarted in the existing vm?
  2. What does the default policy do? In all our Machine configs, the restart-policy is empty.
      "restart": {                                                              
        "policy": ""                                                            
      }

Thanks.

1 Like

@lubien Can you share what the fly command would be to update the restart policy on an existing machine? I can’t figure out how to do that. If I pass it an updated config.toml file, it detects no changes.

Coincidentally @catflydotio just wrote a great explanation about that:

I’m curious how you approached that from your .toml file, can you share it?

1 Like

Hey @nick-brevity , the specific command you’ll need is fly machine update <machine_id> --restart <policy>. The three policy options are:

no - always let a Machine stop when its main process exits, whether that’s on purpose or on a crash

always - never let a Machine enter the stopped state, even if your main process exits cleanly

on-fail - try up to 10 times to restart the Machine if it exits with a non-zero exit code, before letting it stop. This is the default behaviour if the policy is not specified.

The thread Lubien linked above goes into more detail on the three policies and when you’d want to apply them to your app

3 Likes

Hello, I noted this policy is gone after I run fly deploy. Even fly deploy --update-only is removing my restart policy previously set. is it possible to sets policy during deploy command to avoid to run machine update after it?

Hi… It looks like a way to do so was added very recently:

Hope this helps!

1 Like

oh, nice!