Skip to content

Commit 4cd265a

Browse files
committed
job-info: support JSON_DECODE flag
Problem: It can be inconvenient to lookup a key and always have to convert it to a json object / python dictionary (such as a jobspec or R). Solution: Support a new FLUX_JOB_LOOKUP_JSON_DECODE flag that will treat some special keys (currently jobspec and R) as special lookups and will returned the values as decoded JSON objects instead of strings.
1 parent b31d706 commit 4cd265a

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/common/libjob/job.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ enum job_event_watch_flags {
3030
FLUX_JOB_EVENT_WATCH_WAITCREATE = 1, // wait for path to exist
3131
};
3232

33+
enum job_lookup_flags {
34+
/* return special fields as decoded JSON objects instead of strings
35+
* - currently works for jobspec and R
36+
*/
37+
FLUX_JOB_LOOKUP_JSON_DECODE = 1,
38+
};
39+
3340
enum job_urgency {
3441
FLUX_JOB_URGENCY_MIN = 0,
3542
FLUX_JOB_URGENCY_HOLD = FLUX_JOB_URGENCY_MIN,

src/modules/job-info/lookup.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ static void info_lookup_continuation (flux_future_t *fall, void *arg)
193193
json_array_foreach (l->keys, index, key) {
194194
flux_future_t *f;
195195
const char *keystr = json_string_value (key); /* validated earlier */
196-
json_t *str = NULL;
196+
json_t *val = NULL;
197197

198198
if (!(f = flux_future_get_child (fall, keystr))) {
199199
flux_log_error (ctx->h, "%s: flux_future_get_child", __FUNCTION__);
@@ -212,11 +212,21 @@ static void info_lookup_continuation (flux_future_t *fall, void *arg)
212212
goto error;
213213
}
214214

215-
if (!(str = json_string (s)))
216-
goto enomem;
215+
if ((l->flags & FLUX_JOB_LOOKUP_JSON_DECODE)
216+
&& (streq (keystr, "jobspec")
217+
|| streq (keystr, "R"))) {
218+
/* We assume if it was stored in the KVS it's valid JSON,
219+
* so failure is ENOMEM */
220+
if (!(val = json_loads (s, 0, NULL)))
221+
goto enomem;
222+
}
223+
else {
224+
if (!(val = json_string (s)))
225+
goto enomem;
226+
}
217227

218-
if (json_object_set_new (o, keystr, str) < 0) {
219-
json_decref (str);
228+
if (json_object_set_new (o, keystr, val) < 0) {
229+
json_decref (val);
220230
goto enomem;
221231
}
222232
}
@@ -288,7 +298,7 @@ void lookup_cb (flux_t *h, flux_msg_handler_t *mh,
288298
flux_jobid_t id;
289299
uint32_t rolemask;
290300
int flags;
291-
int valid_flags = 0;
301+
int valid_flags = FLUX_JOB_LOOKUP_JSON_DECODE;
292302
const char *errmsg = NULL;
293303

294304
if (flux_request_unpack (msg, NULL, "{s:I s:o s:i}",

0 commit comments

Comments
 (0)