2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-05-21 09:32:39 +02:00
|
|
|
#include <ccan/array_size/array_size.h>
|
2021-09-16 07:00:42 +02:00
|
|
|
#include <common/json_tok.h>
|
2019-05-21 09:32:39 +02:00
|
|
|
#include <plugins/libplugin.h>
|
|
|
|
|
|
|
|
static u64 cycle_seconds = 0, expired_by = 86400;
|
|
|
|
static struct plugin_timer *cleantimer;
|
|
|
|
|
2020-02-03 22:27:46 +01:00
|
|
|
static void do_clean(void *cb_arg);
|
2019-05-21 09:32:39 +02:00
|
|
|
|
|
|
|
static struct command_result *ignore(struct command *timer,
|
|
|
|
const char *buf,
|
|
|
|
const jsmntok_t *result,
|
|
|
|
void *arg)
|
|
|
|
{
|
2020-01-24 14:57:12 +01:00
|
|
|
struct plugin *p = arg;
|
2020-02-03 22:27:46 +01:00
|
|
|
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds), do_clean, p);
|
2020-01-24 14:57:12 +01:00
|
|
|
return timer_complete(p);
|
2019-05-21 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 22:27:46 +01:00
|
|
|
static void do_clean(void *cb_arg)
|
2019-05-21 09:32:39 +02:00
|
|
|
{
|
2020-02-03 22:27:46 +01:00
|
|
|
struct plugin *p = cb_arg;
|
2020-01-30 00:55:55 +01:00
|
|
|
/* FIXME: delexpiredinvoice should be in our plugin too! */
|
|
|
|
struct out_req *req = jsonrpc_request_start(p, NULL, "delexpiredinvoice",
|
|
|
|
ignore, ignore, p);
|
|
|
|
json_add_u64(req->js, "maxexpirytime",
|
2019-06-12 02:38:54 +02:00
|
|
|
time_now().ts.tv_sec - expired_by);
|
|
|
|
|
2020-02-03 22:27:46 +01:00
|
|
|
send_outreq(p, req);
|
2019-05-21 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static struct command_result *json_autocleaninvoice(struct command *cmd,
|
|
|
|
const char *buffer,
|
|
|
|
const jsmntok_t *params)
|
|
|
|
{
|
|
|
|
u64 *cycle;
|
|
|
|
u64 *exby;
|
2021-05-26 07:43:01 +02:00
|
|
|
struct json_stream *response;
|
2019-05-21 09:32:39 +02:00
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
p_opt_def("cycle_seconds", param_u64, &cycle, 3600),
|
|
|
|
p_opt_def("expired_by", param_u64, &exby, 86400),
|
|
|
|
NULL))
|
2019-09-18 12:14:44 +02:00
|
|
|
return command_param_failed();
|
2019-05-21 09:32:39 +02:00
|
|
|
|
|
|
|
cycle_seconds = *cycle;
|
|
|
|
expired_by = *exby;
|
|
|
|
|
|
|
|
if (cycle_seconds == 0) {
|
2021-05-26 07:43:01 +02:00
|
|
|
response = jsonrpc_stream_success(cmd);
|
|
|
|
json_add_bool(response, "enabled", false);
|
|
|
|
return command_finished(cmd, response);
|
2019-05-21 09:32:39 +02:00
|
|
|
}
|
|
|
|
tal_free(cleantimer);
|
2020-02-03 22:27:46 +01:00
|
|
|
cleantimer = plugin_timer(cmd->plugin, time_from_sec(cycle_seconds),
|
|
|
|
do_clean, cmd->plugin);
|
2019-05-21 09:32:39 +02:00
|
|
|
|
2021-05-26 07:43:01 +02:00
|
|
|
response = jsonrpc_stream_success(cmd);
|
|
|
|
json_add_bool(response, "enabled", true);
|
|
|
|
json_add_u64(response, "cycle_seconds", cycle_seconds);
|
|
|
|
json_add_u64(response, "expired_by", expired_by);
|
|
|
|
return command_finished(cmd, response);
|
2019-05-21 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
2021-01-13 04:00:24 +01:00
|
|
|
static const char *init(struct plugin *p,
|
|
|
|
const char *buf UNUSED, const jsmntok_t *config UNUSED)
|
2019-05-21 09:32:39 +02:00
|
|
|
{
|
|
|
|
if (cycle_seconds) {
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_log(p, LOG_INFORM, "autocleaning every %"PRIu64" seconds", cycle_seconds);
|
2020-01-24 14:57:12 +01:00
|
|
|
cleantimer = plugin_timer(p, time_from_sec(cycle_seconds),
|
2020-02-03 22:27:46 +01:00
|
|
|
do_clean, p);
|
2019-05-21 09:32:39 +02:00
|
|
|
} else
|
2020-01-26 15:44:16 +01:00
|
|
|
plugin_log(p, LOG_DBG, "autocleaning not active");
|
2021-01-13 04:00:24 +01:00
|
|
|
|
|
|
|
return NULL;
|
2019-05-21 09:32:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct plugin_command commands[] = { {
|
|
|
|
"autocleaninvoice",
|
2019-05-22 16:27:34 +02:00
|
|
|
"payment",
|
2019-05-21 09:32:39 +02:00
|
|
|
"Set up autoclean of expired invoices. ",
|
|
|
|
"Perform cleanup every {cycle_seconds} (default 3600), or disable autoclean if 0. "
|
|
|
|
"Clean up expired invoices that have expired for {expired_by} seconds (default 86400). ",
|
|
|
|
json_autocleaninvoice
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
setup_locale();
|
2020-07-20 14:39:32 +02:00
|
|
|
plugin_main(argv, init, PLUGIN_STATIC, true, NULL, commands, ARRAY_SIZE(commands),
|
2021-04-28 17:28:27 +02:00
|
|
|
NULL, 0, NULL, 0, NULL, 0,
|
2019-05-21 09:32:39 +02:00
|
|
|
plugin_option("autocleaninvoice-cycle",
|
2019-05-26 23:19:00 +02:00
|
|
|
"string",
|
2019-05-21 09:32:39 +02:00
|
|
|
"Perform cleanup of expired invoices every"
|
|
|
|
" given seconds, or do not autoclean if 0",
|
|
|
|
u64_option, &cycle_seconds),
|
|
|
|
plugin_option("autocleaninvoice-expired-by",
|
2019-05-26 23:19:00 +02:00
|
|
|
"string",
|
2019-05-21 09:32:39 +02:00
|
|
|
"If expired invoice autoclean enabled,"
|
|
|
|
" invoices that have expired for at least"
|
|
|
|
" this given seconds are cleaned",
|
|
|
|
u64_option, &expired_by),
|
|
|
|
NULL);
|
|
|
|
}
|