Skip to content

Commit 3fca66e

Browse files
authored
Merge pull request #6728 from grondo/issue#6703
export `FLUX_JOB_RANKS` to housekeeping scripts
2 parents 0365918 + 2545553 commit 3fca66e

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

doc/man7/flux-environment.rst

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,13 @@ contains the following:
323323
:term:`instance owner`
324324
- :envvar:`FLUX_JOB_ID` - the job id that triggered the script
325325
- :envvar:`FLUX_JOB_USERID` - the uid of the of the job owner
326+
- :envvar:`FLUX_JOB_RANKS` - the broker ranks which were assigned to the
327+
job for which the current script is running. This can be used in conjunction
328+
with the ``hostlist`` attribute to construct the job hostlist. For example:
329+
330+
.. code-block:: shell
331+
332+
FLUX_JOB_HOSTLIST=$(flux hostlist --nth=${FLUX_JOB_RANKS} instance)
326333
327334
If the IMP is configured to allow other ``FLUX_`` prefixed environment
328335
variables to be set as described in :man5:`flux-config-security-imp`,
@@ -333,18 +340,6 @@ then the following are set to allow Flux commands to work from the script:
333340
- :envvar:`FLUX_EXEC_PATH`
334341
- :envvar:`FLUX_CONNECTOR_PATH`
335342

336-
In addition, the following extra environment variables are set in the
337-
prolog and epilog for convenience:
338-
339-
- :envvar:`FLUX_JOB_RANKS` - the broker ranks which were assigned to the
340-
job for which the current prolog or epilog is running. This can be
341-
used in conjunction with the ``hostlist`` attribute to construct the
342-
job hostlist. For example:
343-
344-
.. code-block:: shell
345-
346-
FLUX_JOB_HOSTLIST=$(flux hostlist --nth=${FLUX_JOB_RANKS} instance)
347-
348343
TESTING
349344
=======
350345

src/modules/job-manager/housekeeping.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
* Script environment:
4242
* FLUX_JOB_ID - the job whose resources are running housekeeping
4343
* FLUX_JOB_USERID - the UID of the job's owner
44+
* FLUX_JOB_RANKS - idset of broker ranks which were assigned to FLUX_JOB_ID
4445
* FLUX_URI - the URI of the local flux broker
4546
* The IMP must be configured to explicitly allow FLUX_* to pass through.
4647
*
@@ -161,11 +162,28 @@ static void allocation_destructor (void **item)
161162

162163
}
163164

164-
static int update_cmd_env (flux_cmd_t *cmd, flux_jobid_t id, uint32_t userid)
165+
static char *get_rlist_ranks (struct rlist *rl)
165166
{
166-
if (flux_cmd_setenvf (cmd, 1, "FLUX_JOB_ID", "%ju", (uintmax_t)id) < 0
167-
|| flux_cmd_setenvf (cmd, 1, "FLUX_JOB_USERID", "%u", userid) < 0)
167+
struct idset *ids;
168+
if (!(ids = rlist_ranks (rl)))
169+
return NULL;
170+
return idset_encode (ids, IDSET_FLAG_RANGE);
171+
}
172+
173+
static int update_cmd_env (flux_cmd_t *cmd,
174+
flux_jobid_t id,
175+
uint32_t userid,
176+
struct rlist *rl)
177+
{
178+
char *ranks;
179+
180+
if (!(ranks = get_rlist_ranks (rl))
181+
|| flux_cmd_setenvf (cmd, 1, "FLUX_JOB_ID", "%ju", (uintmax_t)id) < 0
182+
|| flux_cmd_setenvf (cmd, 1, "FLUX_JOB_USERID", "%u", userid) < 0
183+
|| flux_cmd_setenvf (cmd, 1, "FLUX_JOB_RANKS", "%s", ranks) < 0)
168184
return -1;
185+
186+
free (ranks);
169187
return 0;
170188
}
171189

@@ -195,7 +213,7 @@ static struct allocation *allocation_create (struct housekeeping *hk,
195213
id,
196214
"housekeeping",
197215
a))
198-
|| update_cmd_env (hk->cmd, id, userid) < 0
216+
|| update_cmd_env (hk->cmd, id, userid, a->rl) < 0
199217
|| bulk_exec_push_cmd (a->bulk_exec, a->pending, hk->cmd, 0) < 0) {
200218
allocation_destroy (a);
201219
return NULL;

t/t2226-housekeeping.t

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ test_expect_success 'run a job on rank 3, wait for hk, and check environment' '
277277
wait_for_running 0 &&
278278
grep "^FLUX_JOB_ID=$(flux job last | flux job id --to=dec)$" env.out &&
279279
grep "^FLUX_JOB_USERID=$(id -u)$" env.out &&
280-
grep "^FLUX_URI=$(flux exec -r 3 flux getattr local-uri)$" env.out
280+
grep "^FLUX_URI=$(flux exec -r 3 flux getattr local-uri)$" env.out &&
281+
grep "^FLUX_JOB_RANKS=$(flux jobs -no {ranks} $(flux job last))" env.out
281282
'
282283
test_expect_success 'configure housekeeping to sleep forever' '
283284
flux config load <<-EOT

0 commit comments

Comments
 (0)