git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v6 2/3] maintenance: `git maintenance run` learned `--scheduler=<scheduler>`

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Jun 17, 2021, 14:26 UTC
Message-ID
<80d6050a-71d9-1278-e68f-91c3a1ca52e4@gmail.com>
In-Reply-To
<CAPig+cSLi7aN=6ahrHwy4fO-7JMBN3pmzfpWe5ZXOcC9j4+e+g@mail.gmail.com>
On 14/06/2021 05:36, Eric Sunshine wrote:
Show 43 quoted lines
> On Sat, Jun 12, 2021 at 12:51 PM Lénaïc Huard <lenaic@lhuard.fr> wrote:
>> Depending on the system, different schedulers can be used to schedule
>> the hourly, daily and weekly executions of `git maintenance run`:
>> * `launchctl` for MacOS,
>> * `schtasks` for Windows and
>> * `crontab` for everything else.
>>
>> `git maintenance run` now has an option to let the end-user explicitly
>> choose which scheduler he wants to use:
>> `--scheduler=auto|crontab|launchctl|schtasks`.
>>
>> When `git maintenance start --scheduler=XXX` is run, it not only
>> registers `git maintenance run` tasks in the scheduler XXX, it also
>> removes the `git maintenance run` tasks from all the other schedulers to
>> ensure we cannot have two schedulers launching concurrent identical
>> tasks.
>>
>> The default value is `auto` which chooses a suitable scheduler for the
>> system.
>>
>> `git maintenance stop` doesn't have any `--scheduler` parameter because
>> this command will try to remove the `git maintenance run` tasks from all
>> the available schedulers.
>>
>> Signed-off-by: Lénaïc Huard <lenaic@lhuard.fr>
> 
> Thanks. Unfortunately, I haven't been following this series too
> closely since I reviewed v1, so I set aside time to review v6, which I
> have now done. The material in the cover letter and individual commit
> messages was helpful in understanding the nuances of the changes, and
> the series seems pretty well complete at this point. (If, however, you
> do happen to re-roll for some reason, please consider using the
> --range-diff option of git-format-patch as an aid to reviewers.)
> 
> I did leave a number of comments below regarding possible improvements
> to the code and documentation, however, they're probably mostly
> subjective and don't necessarily warrant a re-roll; I'd have no
> problem seeing this accepted as-is without the suggestions applied.
> (They can always be applied later on if someone considers them
> important enough.)
>
> I do, though, have one question (below) about is_crontab_available()
> for which I could not figure out the answer.
I think that is a bug
Show 52 quoted lines
>> [...]
>> diff --git a/builtin/gc.c b/builtin/gc.c
>> @@ -1529,6 +1529,59 @@ static const char *get_frequency(enum schedule_priority schedule)
>> +static int get_schedule_cmd(const char **cmd, int *is_available)
>> +{
>> +       char *item;
>> +       char *testing = xstrdup_or_null(getenv("GIT_TEST_MAINT_SCHEDULER"));
>> +
>> +       if (!testing)
>> +               return 0;
>> +
>> +       if (is_available)
>> +               *is_available = 0;
>> +
>> +       for (item = testing;;) {
>> +               char *sep;
>> +               char *end_item = strchr(item, ',');
>> +               if (end_item)
>> +                       *end_item = '\0';
>> +
>> +               sep = strchr(item, ':');
>> +               if (!sep)
>> +                       die("GIT_TEST_MAINT_SCHEDULER unparseable: %s", testing);
>> +               *sep = '\0';
>> +
>> +               if (!strcmp(*cmd, item)) {
>> +                       *cmd = sep + 1;
>> +                       if (is_available)
>> +                               *is_available = 1;
>> +                       UNLEAK(testing);
>> +                       return 1;
>> +               }
>> +
>> +               if (!end_item)
>> +                       break;
>> +               item = end_item + 1;
>> +       }
>> +
>> +       free(testing);
>> +       return 1;
>> +}
> 
> I ended up studying this implementation several times since I had to
> come back to it repeatedly after reading calling code in order to (I
> hope) fully understand all the different conditions represented by its
> three distinct return values (the function return value, and the
> values returned in **cmd and **is_available). That it required several
> readings might warrant a comment block explaining what the function
> does and what the various return conditions mean. As a bonus, an
> explanation of the value of GIT_TEST_MAINT_SCHEDULER -- a
> comma-separated list of colon-delimited tuples, and what those tuples
> represent -- could be helpful.
I agree documenting GIT_TEST_MAINT_SCHEDULER would be useful
Show 85 quoted lines
>> +static int is_launchctl_available(void)
>> +{
>> +       const char *cmd = "launchctl";
>> +       int is_available;
>> +       if (get_schedule_cmd(&cmd, &is_available))
>> +               return is_available;
>> +
>> +#ifdef __APPLE__
>> +       return 1;
>> +#else
>> +       return 0;
>> +#endif
>> +}
> 
> On this project, we usually frown upon #if conditionals within
> functions since the code often can become unreadable. The usage in
> this function doesn't suffer from that problem, however,
> resolve_auto_scheduler() is somewhat ugly. An alternative would be to
> set up these values outside of all functions, perhaps like this:
> 
>      #ifdef __APPLE__
>      #define MAINT_SCHEDULER SCHEDULER_LAUNCHCTL
>      #elif GIT_WINDOWS_NATIVE
>      #define MAINT_SCHEDULER SCHEDULER_SCHTASKS
>      #else
>      #define MAINT_SCHEDULER SCHEDULER_CRON
>      #endif
> 
> and then:
> 
>      static int is_launchctl_available(void)
>      {
>          if (get_schedule_cmd(...))
>              return is_available;
>          return MAINT_SCHEDULER == SCHEDULER_LAUNCHCTL;
>      }
> 
>      static void resolve_auto_scheduler(enum scheduler *scheduler)
>      {
>          if (*scheduler == SCHEDULER_AUTO)
>              *scheduler = MAINT_SCHEDULER;
>      }
> 
>> +static int is_crontab_available(void)
>> +{
>> +       const char *cmd = "crontab";
>> +       int is_available;
>> +       struct child_process child = CHILD_PROCESS_INIT;
>> +
>> +       if (get_schedule_cmd(&cmd, &is_available) && !is_available)
>> +               return 0;
>> +
>> +       strvec_split(&child.args, cmd);
>> +       strvec_push(&child.args, "-l");
>> +       child.no_stdin = 1;
>> +       child.no_stdout = 1;
>> +       child.no_stderr = 1;
>> +       child.silent_exec_failure = 1;
>> +
>> +       if (start_command(&child))
>> +               return 0;
>> +       /* Ignore exit code, as an empty crontab will return error. */
>> +       finish_command(&child);
>> +       return 1;
>>   }
> 
> If I understand get_schedule_cmd() correctly, it will always return
> true if GIT_TEST_MAINT_SCHEDULER is present in the environment,
> however, it will only set `is_available` to true if
> GIT_TEST_MAINT_SCHEDULER contains a matching entry for `cmd` (which in
> this case is "crontab"). Assuming this understanding is correct, then
> I'm having trouble understanding why this:
> 
>      if (get_schedule_cmd(&cmd, &is_available) && !is_available)
>          return 0;
> 
> isn't instead written like this:
> 
>      if (get_schedule_cmd(&cmd, &is_available))
>          return is_available;
> 
> That is, why doesn't is_crontab_available() trust the result of
> get_schedule_cmd(), instead going ahead and trying to invoke `crontab`
> itself? Am I missing something which makes the `!is_available` case
> special?

I agree, I think we should be returning is_available irrespective of its value if get_schedule_cmd() returns true. This is what is_systemd_timer_available() does in the next patch. Well spotted.

Best Wishes
Phillip
Show 70 quoted lines
>> +static void resolve_auto_scheduler(enum scheduler *scheduler)
>> +{
>> +       if (*scheduler != SCHEDULER_AUTO)
>> +               return;
>> +
>>   #if defined(__APPLE__)
>> +       *scheduler = SCHEDULER_LAUNCHCTL;
>> +       return;
>> +
>>   #elif defined(GIT_WINDOWS_NATIVE)
>> +       *scheduler = SCHEDULER_SCHTASKS;
>> +       return;
>> +
>>   #else
>> +       *scheduler = SCHEDULER_CRON;
>> +       return;
>>   #endif
>> +}
> 
> (See above for a way to simplify this implementation.)
> 
> Is there a strong reason which I'm missing that this function alters
> its argument rather than simply returning the resolved scheduler?
> 
>      static enum scheduler resolve_scheduler(enum scheduler x) {...}
> 
> Or is it just personal preference?
>
> (Minor: I took the liberty of shortening the function name since it
> doesn't feel like the longer name adds much value.)
> 
>> +static int maintenance_start(int argc, const char **argv, const char *prefix)
>>   {
>> +       struct maintenance_start_opts opts = { 0 };
>> +       struct option options[] = {
>> +               OPT_CALLBACK_F(
>> +                       0, "scheduler", &opts.scheduler, N_("scheduler"),
>> +                       N_("scheduler to use to trigger git maintenance run"),
> 
> Dropping "to use" would make this more concise without losing clarity:
> 
>      "scheduler to trigger git maintenance run"
> 
>> +                       PARSE_OPT_NONEG, maintenance_opt_scheduler),
>> +               OPT_END()
>> +       };
>> diff --git a/t/t7900-maintenance.sh b/t/t7900-maintenance.sh
>> @@ -494,8 +494,21 @@ test_expect_success !MINGW 'register and unregister with regex metacharacters' '
>> +test_expect_success 'start --scheduler=<scheduler>' '
>> +       test_expect_code 129 git maintenance start --scheduler=foo 2>err &&
>> +       test_i18ngrep "unrecognized --scheduler argument" err &&
>> +
>> +       test_expect_code 129 git maintenance start --no-scheduler 2>err &&
>> +       test_i18ngrep "unknown option" err &&
>> +
>> +       test_expect_code 128 \
>> +               env GIT_TEST_MAINT_SCHEDULER="launchctl:true,schtasks:true" \
>> +               git maintenance start --scheduler=crontab 2>err &&
>> +       test_i18ngrep "fatal: crontab scheduler is not available" err
>> +'
> 
> Why does this test care about the exact exit codes rather than simply
> using test_must_fail() as is typically done elsewhere in the test
> suite, especially since we're also checking the error message itself?
> Am I missing some non-obvious property of the error codes?
> 
> I don't see `auto` being tested anywhere. Do we want such a test? (It
> seems like it should be doable, though perhaps the complexity is too
> high -- I haven't thought it through fully.)
> 
Previous: Eric SunshineNext: Lénaïc Huard
Message 92 of 138 in “maintenance: use systemd timers on Linux”
  1. maintenance: use systemd timers on LinuxLénaïc Huard, May 1, 2021
  2. brian m. carlsonMay 1, 2021
  3. Bagas SanjayaMay 2, 2021
  4. Eric SunshineMay 2, 2021
  5. Eric SunshineMay 2, 2021
  6. Phillip WoodMay 2, 2021
  7. Đoàn Trần Công DanhMay 5, 2021
  8. Phillip WoodMay 5, 2021
  9. Ævar Arnfjörð BjarmasonMay 5, 2021
  10. Lénaïc HuardMay 9, 2021
  11. Ævar Arnfjörð BjarmasonMay 10, 2021
  12. Bagas SanjayaMay 2, 2021
  13. Derrick StoleeMay 3, 2021
  14. 0/1 maintenance: use systemd timers on LinuxLénaïc Huard, May 9, 2021
  15. 1/1 maintenance: use systemd timers on LinuxLénaïc Huard, May 9, 2021
  16. Đoàn Trần Công DanhMay 10, 2021
  17. Eric SunshineMay 10, 2021
  18. Junio C HamanoMay 10, 2021
  19. Đoàn Trần Công DanhMay 12, 2021
  20. Felipe ContrerasMay 12, 2021
  21. Phillip WoodMay 12, 2021
  22. Phillip WoodMay 12, 2021
  23. Đoàn Trần Công DanhMay 12, 2021
  24. Phillip WoodMay 10, 2021
  25. Eric SunshineMay 10, 2021
  26. Phillip WoodMay 10, 2021
  27. Eric SunshineMay 10, 2021
  28. Lénaïc HuardJun 8, 2021
  29. Martin ÅgrenMay 10, 2021
  30. Phillip WoodMay 11, 2021
  31. Derrick StoleeMay 11, 2021
  32. 0/4 maintenance: use systemd timers on LinuxLénaïc Huard, May 20, 2021
  33. 2/4 maintenance: introduce ENABLE/DISABLE for code clarityLénaïc Huard, May 20, 2021
  34. 4/4 maintenance: optionally use systemd timers on LinuxLénaïc Huard, May 20, 2021
  35. Bagas SanjayaMay 21, 2021
  36. Derrick StoleeMay 21, 2021
  37. Johannes SchindelinMay 22, 2021
  38. Felipe ContrerasMay 23, 2021
  39. brian m. carlsonMay 23, 2021
  40. Felipe ContrerasMay 24, 2021
  41. Ævar Arnfjörð BjarmasonMay 24, 2021
  42. Junio C HamanoMay 24, 2021
  43. Johannes SchindelinMay 25, 2021
  44. Felipe ContrerasMay 25, 2021
  45. CoC, inclusivity etc. (was "Re: [...] systemd timers on Linux")Ævar Arnfjörð Bjarmason, May 26, 2021
  46. Felipe ContrerasMay 26, 2021
  47. Jeff KingMay 27, 2021
  48. Felipe ContrerasMay 27, 2021
  49. Junio C HamanoMay 27, 2021
  50. Phillip SusiMay 28, 2021
  51. Jeff KingMay 30, 2021
  52. Felipe ContrerasMay 24, 2021
  53. 1/4 cache.h: rename "xdg_config_home" to "xdg_config_home_git"Lénaïc Huard, May 20, 2021
  54. Đoàn Trần Công DanhMay 20, 2021
  55. 3/4 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, May 20, 2021
  56. Bagas SanjayaMay 21, 2021
  57. 0/4 add support for systemd timers on LinuxLénaïc Huard, May 24, 2021
  58. 4/4 maintenance: add support for systemd timers on LinuxLénaïc Huard, May 24, 2021
  59. Ævar Arnfjörð BjarmasonMay 24, 2021
  60. Eric SunshineMay 24, 2021
  61. Felipe ContrerasMay 24, 2021
  62. Phillip WoodMay 26, 2021
  63. 3/4 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, May 24, 2021
  64. Phillip WoodMay 24, 2021
  65. Lénaïc HuardMay 30, 2021
  66. Phillip WoodMay 30, 2021
  67. 2/4 maintenance: introduce ENABLE/DISABLE for code clarityLénaïc Huard, May 24, 2021
  68. Phillip WoodMay 24, 2021
  69. Đoàn Trần Công DanhMay 24, 2021
  70. Lénaïc HuardMay 25, 2021
  71. Junio C HamanoMay 25, 2021
  72. Ævar Arnfjörð BjarmasonMay 24, 2021
  73. 1/4 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, May 24, 2021
  74. Phillip WoodMay 24, 2021
  75. Đoàn Trần Công DanhMay 24, 2021
  76. Junio C HamanoMay 24, 2021
  77. 0/3 add support for systemd timers on LinuxLénaïc Huard, Jun 8, 2021
  78. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Jun 8, 2021
  79. Jeff KingJun 9, 2021
  80. Phillip WoodJun 9, 2021
  81. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Jun 8, 2021
  82. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Jun 8, 2021
  83. Junio C HamanoJun 9, 2021
  84. Phillip WoodJun 9, 2021
  85. 0/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Jun 12, 2021
  86. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Jun 12, 2021
  87. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Jun 12, 2021
  88. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Jun 12, 2021
  89. Eric SunshineJun 14, 2021
  90. Derrick StoleeJun 16, 2021
  91. Eric SunshineJun 17, 2021
  92. Phillip WoodJun 17, 2021
  93. Lénaïc HuardJul 2, 2021
  94. 0/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Jul 2, 2021
  95. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Jul 2, 2021
  96. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Jul 2, 2021
  97. Ævar Arnfjörð BjarmasonJul 6, 2021
  98. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Jul 2, 2021
  99. Ævar Arnfjörð BjarmasonJul 6, 2021
  100. Junio C HamanoJul 6, 2021
  101. Jeff KingJul 13, 2021
  102. Eric SunshineJul 13, 2021
  103. Jeff KingJul 13, 2021
  104. Eric SunshineJul 13, 2021
  105. Bagas SanjayaJul 13, 2021
  106. Felipe ContrerasJul 6, 2021
  107. Lénaïc HuardAug 23, 2021
  108. Junio C HamanoAug 23, 2021
  109. Junio C HamanoJul 2, 2021
  110. Phillip WoodJul 6, 2021
  111. 0/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Aug 23, 2021
  112. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Aug 23, 2021
  113. Derrick StoleeAug 24, 2021
  114. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Aug 23, 2021
  115. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Aug 23, 2021
  116. Derrick StoleeAug 24, 2021
  117. Derrick StoleeAug 24, 2021
  118. 0/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Aug 27, 2021
  119. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Aug 27, 2021
  120. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Aug 27, 2021
  121. Ramsay JonesAug 27, 2021
  122. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Aug 27, 2021
  123. 0/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Sep 4, 2021
  124. 1/3 cache.h: Introduce a generic "xdg_config_home_for(…)" functionLénaïc Huard, Sep 4, 2021
  125. 3/3 maintenance: add support for systemd timers on LinuxLénaïc Huard, Sep 4, 2021
  126. 2/3 maintenance: `git maintenance run` learned `--scheduler=<scheduler>`Lénaïc Huard, Sep 4, 2021
  127. Derrick StoleeSep 7, 2021
  128. Derrick StoleeSep 8, 2021
  129. Lénaïc HuardSep 9, 2021
  130. Derrick StoleeSep 9, 2021
  131. Ævar Arnfjörð BjarmasonSep 27, 2021
  132. Lénaïc HuardSep 27, 2021
  133. Derrick StoleeAug 17, 2021
  134. Phillip WoodAug 17, 2021
  135. Derrick StoleeAug 17, 2021
  136. Lénaïc HuardAug 18, 2021
  137. Derrick StoleeAug 18, 2021
  138. Junio C HamanoAug 18, 2021

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.