| 346 | } |
| 347 | |
| 348 | void plugin_hook_db_sync(struct db *db) |
| 349 | { |
| 350 | const struct plugin_hook *hook = &db_write_hook; |
| 351 | struct jsonrpc_request *req; |
| 352 | struct plugin_hook_request *ph_req; |
| 353 | void *ret; |
| 354 | struct plugin **plugins; |
| 355 | size_t i; |
| 356 | size_t num_hooks; |
| 357 | |
| 358 | const char **changes = db_changes(db); |
| 359 | num_hooks = tal_count(hook->hooks); |
| 360 | if (num_hooks == 0) |
| 361 | return; |
| 362 | |
| 363 | plugins = notleak(tal_arr(NULL, struct plugin *, |
| 364 | num_hooks)); |
| 365 | for (i = 0; i < num_hooks; ++i) |
| 366 | plugins[i] = hook->hooks[i]->plugin; |
| 367 | |
| 368 | ph_req = notleak(tal(hook->hooks, struct plugin_hook_request)); |
| 369 | ph_req->hook = hook; |
| 370 | ph_req->db = db; |
| 371 | ph_req->cb_arg = &num_hooks; |
| 372 | |
| 373 | for (i = 0; i < num_hooks; ++i) { |
| 374 | /* Create an object for this plugin. */ |
| 375 | struct db_write_hook_req *dwh_req; |
| 376 | dwh_req = tal(ph_req, struct db_write_hook_req); |
| 377 | dwh_req->plugin = plugins[i]; |
| 378 | dwh_req->ph_req = ph_req; |
| 379 | dwh_req->num_hooks = &num_hooks; |
| 380 | |
| 381 | /* FIXME: id_prefix from caller? */ |
| 382 | /* FIXME: do IO logging for this! */ |
| 383 | req = jsonrpc_request_start(NULL, hook->name, NULL, NULL, NULL, |
| 384 | db_hook_response, |
| 385 | dwh_req); |
| 386 | |
| 387 | json_add_num(req->stream, "data_version", |
| 388 | db_data_version_get(db)); |
| 389 | |
| 390 | json_array_start(req->stream, "writes"); |
| 391 | for (size_t j = 0; j < tal_count(changes); j++) |
| 392 | json_add_string(req->stream, NULL, changes[j]); |
| 393 | json_array_end(req->stream); |
| 394 | jsonrpc_request_end(req); |
| 395 | |
| 396 | plugin_request_send(plugins[i], req); |
| 397 | } |
| 398 | |
| 399 | /* We can be called on way out of an io_loop, which is already breaking. |
| 400 | * That will make this immediately return; save the break value and call |
| 401 | * again, then hand it onwards. */ |
| 402 | ret = plugins_exclusive_loop(plugins); |
| 403 | if (ret != ph_req) { |
| 404 | void *ret2 = plugins_exclusive_loop(plugins); |
| 405 | assert(ret2 == ph_req); |
nothing calls this directly
no test coverage detected