parse_restart_flags Subroutine

private subroutine parse_restart_flags(auto_restart, restart_delay, restart_max, is_child, self_exe)

Parse restart-related flags and apply manifest defaults.

This routine also applies --watch-low-cpu to the sleep implementation so supervisor delays do not busy-wait.

Arguments

Type IntentOptional Attributes Name
logical, intent(out) :: auto_restart
real, intent(out) :: restart_delay
integer, intent(out) :: restart_max
logical, intent(out) :: is_child
character(len=:), intent(out), allocatable :: self_exe

Calls

proc~~parse_restart_flags~~CallsGraph proc~parse_restart_flags parse_restart_flags proc~get_arg get_arg proc~parse_restart_flags->proc~get_arg proc~get_restart_defaults get_restart_defaults proc~parse_restart_flags->proc~get_restart_defaults proc~parse_int parse_int proc~parse_restart_flags->proc~parse_int proc~parse_real parse_real proc~parse_restart_flags->proc~parse_real proc~set_low_cpu set_low_cpu proc~parse_restart_flags->proc~set_low_cpu proc~starts_with~2 starts_with proc~parse_restart_flags->proc~starts_with~2 proc~load_watch_table load_watch_table proc~get_restart_defaults->proc~load_watch_table proc~toml_get_int toml_get_int proc~get_restart_defaults->proc~toml_get_int proc~toml_get_logical toml_get_logical proc~get_restart_defaults->proc~toml_get_logical proc~toml_get_real toml_get_real proc~get_restart_defaults->proc~toml_get_real proc~toml_get_string toml_get_string proc~get_restart_defaults->proc~toml_get_string exists exists proc~load_watch_table->exists get_value get_value proc~load_watch_table->get_value read_package_file read_package_file proc~load_watch_table->read_package_file proc~toml_get_int->get_value proc~toml_get_logical->get_value proc~toml_get_real->get_value proc~toml_get_string->get_value

Called by

proc~~parse_restart_flags~~CalledByGraph proc~parse_restart_flags parse_restart_flags proc~maybe_supervise maybe_supervise proc~maybe_supervise->proc~parse_restart_flags

Source Code

   subroutine parse_restart_flags(auto_restart, restart_delay, restart_max, is_child, self_exe)
      logical, intent(out) :: auto_restart, is_child
      real,    intent(out) :: restart_delay
      integer, intent(out) :: restart_max
      character(len=:), allocatable, intent(out) :: self_exe

      integer :: narg, i
      character(len=:), allocatable :: a, v

      call get_restart_defaults(auto_restart, restart_delay, restart_max, self_exe)
      is_child = .false.

      narg = command_argument_count()
      i = 1
      do while (i <= narg)
         a = get_arg(i)

         if (a == "--watch-low-cpu") then
            call set_low_cpu(.true.)
            i = i + 1
            cycle
         end if

         if (a == "--watch-no-low-cpu") then
            call set_low_cpu(.false.)
            i = i + 1
            cycle
         end if

         if (starts_with(a, "--watch-low-cpu=")) then
            v = a(len("--watch-low-cpu=")+1:)
            call set_low_cpu(parse_bool(v, .true.))
            i = i + 1
            cycle
         end if

         if (a == "--watch-auto-restart") then
            auto_restart = .true.
            i = i + 1
            cycle
         end if

         if (a == "--watch-child") then
            is_child = .true.
            i = i + 1
            cycle
         end if

         if (starts_with(a, "--watch-restart-delay=")) then
            v = a(len("--watch-restart-delay=")+1:)
            restart_delay = parse_real(v, restart_delay)
            i = i + 1
            cycle
         end if

         if (starts_with(a, "--watch-restart-max=")) then
            v = a(len("--watch-restart-max=")+1:)
            restart_max = parse_int(v, restart_max)
            i = i + 1
            cycle
         end if

         if (starts_with(a, "--watch-self=")) then
            self_exe = trim(a(len("--watch-self=")+1:))
            i = i + 1
            cycle
         end if

         if (a == "--watch-restart-delay") then
            if (i+1 <= narg) then
               v = get_arg(i+1)
               restart_delay = parse_real(v, restart_delay)
               i = i + 2
            else
               i = i + 1
            end if
            cycle
         end if

         if (a == "--watch-restart-max") then
            if (i+1 <= narg) then
               v = get_arg(i+1)
               restart_max = parse_int(v, restart_max)
               i = i + 2
            else
               i = i + 1
            end if
            cycle
         end if

         if (a == "--watch-self") then
            if (i+1 <= narg) then
               self_exe = trim(get_arg(i+1))
               i = i + 2
            else
               i = i + 1
            end if
            cycle
         end if

         i = i + 1
      end do

   contains

      pure logical function parse_bool(s, default) result(vb)
         character(len=*), intent(in) :: s
         logical, intent(in) :: default
         character(len=:), allocatable :: t
         t = trim(adjustl(lower_ascii(s)))
         select case (t)
          case ("1","true","on","yes","y","t")
            vb = .true.
          case ("0","false","off","no","n","f")
            vb = .false.
          case default
            vb = default
         end select
      end function parse_bool

      pure function lower_ascii(s) result(r)
         character(len=*), intent(in) :: s
         character(len=len(s)) :: r
         integer :: k, c, da
         da = iachar('a') - iachar('A')
         do k = 1, len(s)
            c = iachar(s(k:k))
            if (c >= iachar('A') .and. c <= iachar('Z')) then
               r(k:k) = achar(c + da)
            else
               r(k:k) = s(k:k)
            end if
         end do
      end function lower_ascii

   end subroutine parse_restart_flags