mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-25 22:45:05 +00:00
Unify enqueueing code.
This commit is contained in:
parent
e493ff962f
commit
e8fbc94089
1 changed files with 38 additions and 38 deletions
76
data.c
76
data.c
|
@ -892,34 +892,45 @@ d_char *history_list(snac *snac)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int _enqueue_put(char *fn, char *msg)
|
||||||
|
/* writes safely to the queue */
|
||||||
|
{
|
||||||
|
int ret = 1;
|
||||||
|
xs *tfn = xs_fmt("%s.tmp", fn);
|
||||||
|
FILE *f;
|
||||||
|
|
||||||
|
if ((f = fopen(tfn, "w")) != NULL) {
|
||||||
|
xs *j = xs_json_dumps_pp(msg, 4);
|
||||||
|
|
||||||
|
fwrite(j, strlen(j), 1, f);
|
||||||
|
fclose(f);
|
||||||
|
|
||||||
|
rename(tfn, fn);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ret = 0;
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void enqueue_input(snac *snac, char *msg, char *req, int retries)
|
void enqueue_input(snac *snac, char *msg, char *req, int retries)
|
||||||
/* enqueues an input message */
|
/* enqueues an input message */
|
||||||
{
|
{
|
||||||
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
||||||
xs *ntid = tid(retries * 60 * qrt);
|
xs *ntid = tid(retries * 60 * qrt);
|
||||||
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
||||||
xs *tfn = xs_fmt("%s.tmp", fn);
|
xs *qmsg = xs_dict_new();
|
||||||
FILE *f;
|
xs *rn = xs_number_new(retries);
|
||||||
|
|
||||||
if ((f = fopen(tfn, "w")) != NULL) {
|
qmsg = xs_dict_append(qmsg, "type", "input");
|
||||||
xs *qmsg = xs_dict_new();
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
||||||
xs *rn = xs_number_new(retries);
|
qmsg = xs_dict_append(qmsg, "req", req);
|
||||||
xs *j;
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
||||||
|
|
||||||
qmsg = xs_dict_append(qmsg, "type", "input");
|
_enqueue_put(fn, qmsg);
|
||||||
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
||||||
qmsg = xs_dict_append(qmsg, "req", req);
|
|
||||||
qmsg = xs_dict_append(qmsg, "retries", rn);
|
|
||||||
|
|
||||||
j = xs_json_dumps_pp(qmsg, 4);
|
snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
|
||||||
|
|
||||||
fwrite(j, strlen(j), 1, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
rename(tfn, fn);
|
|
||||||
|
|
||||||
snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -934,28 +945,17 @@ void enqueue_output(snac *snac, char *msg, char *actor, int retries)
|
||||||
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
|
||||||
xs *ntid = tid(retries * 60 * qrt);
|
xs *ntid = tid(retries * 60 * qrt);
|
||||||
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
|
||||||
xs *tfn = xs_fmt("%s.tmp", fn);
|
xs *qmsg = xs_dict_new();
|
||||||
FILE *f;
|
xs *rn = xs_number_new(retries);
|
||||||
|
|
||||||
if ((f = fopen(tfn, "w")) != NULL) {
|
qmsg = xs_dict_append(qmsg, "type", "output");
|
||||||
xs *qmsg = xs_dict_new();
|
qmsg = xs_dict_append(qmsg, "actor", actor);
|
||||||
xs *rn = xs_number_new(retries);
|
qmsg = xs_dict_append(qmsg, "object", msg);
|
||||||
xs *j;
|
qmsg = xs_dict_append(qmsg, "retries", rn);
|
||||||
|
|
||||||
qmsg = xs_dict_append(qmsg, "type", "output");
|
_enqueue_put(fn, qmsg);
|
||||||
qmsg = xs_dict_append(qmsg, "actor", actor);
|
|
||||||
qmsg = xs_dict_append(qmsg, "object", msg);
|
|
||||||
qmsg = xs_dict_append(qmsg, "retries", rn);
|
|
||||||
|
|
||||||
j = xs_json_dumps_pp(qmsg, 4);
|
snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
|
||||||
|
|
||||||
fwrite(j, strlen(j), 1, f);
|
|
||||||
fclose(f);
|
|
||||||
|
|
||||||
rename(tfn, fn);
|
|
||||||
|
|
||||||
snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", actor, fn, retries));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue