← Esempi di programmazione · libnex
05_staccati.c
Il file da solo · nel pacchetto libnex-dev: /usr/share/doc/libnex-dev/examples/
/*
-------------------------------------------------------------------------------------------------
_ ____ _ _____ ___ ___ _ _ ____ _____ ____ _ _ _____ ____ ___ _
/ \ | _ \ / \|_ _/ _ \ |_ _| \ | |/ ___| ____/ ___| \ | | ____| _ \|_ _| / \
/ _ \ | |_) | / _ \ | || | | | | || \| | | _| _|| | _| \| | _| | |_) || | / _ \
/ ___ \| _ < / ___ \| || |_| | | || |\ | |_| | |__| |_| | |\ | |___| _ < | | / ___ \
/_/ \_\_| \_\/_/ \_\_| \___/ |___|_| \_|\____|_____\____|_| \_|_____|_| \_\___/_/ \_\
-------------------------------------------------------------------------------------------------
Copyright (c) 2026 Arato Ingegneria. Tutti i diritti riservati.
Name : 05_staccati.c
Author : Ing. Roberto arato
Version : 1.0.0
Copyright : Ing. Roberto arato (c) 2026
Description : Esempio 5 - lavori staccati: avvio, elenco, riattacco per l'uscita, arresto
-------------------------------------------------------------------------------------------------
*/
/*
Un lavoro staccato (come "nex run -d") vive per conto suo: chi lo lancia puo' uscire.
Qui: si avviano due lavori, si legge l'elenco (nex_jobs), ci si riattacca al primo
per raccoglierne l'uscita gia' prodotta e il codice (nex_attach_json + nex_run),
e si ferma il secondo (nex_job_stop).
Attenzione: riattaccarsi a un lavoro gia' finito ne consegna l'uscita e lo chiude; dopo
non compare piu' in nex jobs (chi aspetta con nex_wait riceve NEX_ERR_NOTFOUND).
Uso: cd /nex/...; ./05_staccati
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <nex.h>
// Valore della chiave key nel JSON (la prima che compare), testo o numero, senza
// virgolette: basta per l'esempio, in un programma vero si usa una libreria JSON.
// 0 se la chiave non c'e'.
static int valore(const char *json, const char *key, char *out, size_t len) {
char pat[64];
snprintf(pat, sizeof(pat), "\"%s\"", key);
const char *p = json ? strstr(json, pat) : NULL;
out[0] = 0;
if (p == NULL) return 0;
p += strlen(pat);
while (*p == ' ' || *p == ':') p++; // "key":"v" e "key": "v"
if (*p == '"') p++;
snprintf(out, len, "%.*s", (int)strcspn(p, "\",}] "), p);
return 1;
}
int main(void) {
char cwd[4096], node1[64], job1[16], node2[64], job2[16];
if (getcwd(cwd, sizeof(cwd)) == NULL) return 1;
nex_conn *nd = nex_open(NULL);
if (nd == NULL) { fprintf(stderr, "agent non raggiungibile\n"); return 1; }
const char *breve[] = { "sh", "-c", "for i in 1 2 3; do echo riga $i; done; exit 7", NULL };
const char *lungo[] = { "sleep", "600", NULL };
char *resp = NULL;
// tty 0, righe e colonne 0, quiet 1 (nessun avviso ai terminali alla fine)
if (nex_run_detached(nd, "any", breve, cwd, NULL, NULL, NULL, 0, 0, 0, 1, &resp) != NEX_OK) {
fprintf(stderr, "avvio: %s\n", resp ? resp : "?");
return 1;
}
valore(resp, "node", node1, sizeof(node1));
valore(resp, "job", job1, sizeof(job1));
nex_free(resp);
nex_run_detached(nd, "any", lungo, cwd, NULL, NULL, NULL, 0, 0, 0, 1, &resp);
valore(resp, "node", node2, sizeof(node2));
valore(resp, "job", job2, sizeof(job2));
nex_free(resp);
printf("avviati %s:%s e %s:%s\n", node1, job1, node2, job2);
sleep(2);
if (nex_jobs(nd, &resp) == NEX_OK) { // tutti i propri lavori, tutti i nodi
printf("nex jobs: %s\n", strstr(resp, job2) ? "il lavoro lungo e' nell'elenco" : "lavoro lungo assente!");
nex_free(resp);
}
// Riattacco al primo: arrivano STARTED (con "replay"), le uscite conservate e EXIT.
nex_conn *na = nex_open(NULL);
char *req = nex_attach_json(node1, job1);
int err, ev;
nex_job *job = nex_run(na, req, &err);
nex_free(req);
char *data;
size_t len;
while (job && nex_job_next(job, &ev, &data, &len) == NEX_OK) {
if (ev == NEX_EV_STDOUT) fwrite(data, 1, len, stdout);
if (ev == NEX_EV_EXIT) printf("fine del primo: %s\n", data);
nex_free(data);
}
nex_job_free(job);
nex_close(na);
if (nex_job_stop(nd, node2, job2, &resp) == NEX_OK) printf("arresto del secondo: %s\n", resp);
nex_free(resp);
nex_close(nd);
return 0;
}