mirror of
https://github.com/asterisk/asterisk.git
synced 2025-10-16 09:46:22 +00:00
pjsip_scheduler.c: Sort "pjsip show scheduled_tasks" output.
* A side benefit is that the scheduled tasks are not completely blocked while the CLI command executes. * Adjusted the "Task Name" column width to have more room for longer names. Change-Id: Iec64aa463ee8b10eef90120e00c38b1fb444087e
This commit is contained in:
@@ -366,7 +366,8 @@ struct ast_sip_sched_task *ast_sip_schedule_task(struct ast_taskprocessor *seria
|
|||||||
|
|
||||||
static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
|
||||||
{
|
{
|
||||||
struct ao2_iterator i;
|
struct ao2_iterator iter;
|
||||||
|
struct ao2_container *sorted_tasks;
|
||||||
struct ast_sip_sched_task *schtd;
|
struct ast_sip_sched_task *schtd;
|
||||||
const char *log_format;
|
const char *log_format;
|
||||||
struct ast_tm tm;
|
struct ast_tm tm;
|
||||||
@@ -375,7 +376,7 @@ static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
|
|||||||
char next_start[32];
|
char next_start[32];
|
||||||
int datelen;
|
int datelen;
|
||||||
struct timeval now;
|
struct timeval now;
|
||||||
static const char separator[] = "======================================";
|
static const char separator[] = "=============================================";
|
||||||
|
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case CLI_INIT:
|
case CLI_INIT:
|
||||||
@@ -391,6 +392,17 @@ static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
|
|||||||
return CLI_SHOWUSAGE;
|
return CLI_SHOWUSAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Get a sorted snapshot of the scheduled tasks */
|
||||||
|
sorted_tasks = ao2_container_alloc_rbtree(AO2_ALLOC_OPT_LOCK_NOLOCK, 0,
|
||||||
|
ast_sip_sched_task_sort_fn, NULL);
|
||||||
|
if (!sorted_tasks) {
|
||||||
|
return CLI_SUCCESS;
|
||||||
|
}
|
||||||
|
if (ao2_container_dup(sorted_tasks, tasks, 0)) {
|
||||||
|
ao2_ref(sorted_tasks, -1);
|
||||||
|
return CLI_SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
now = ast_tvnow();
|
now = ast_tvnow();
|
||||||
log_format = ast_logger_get_dateformat();
|
log_format = ast_logger_get_dateformat();
|
||||||
|
|
||||||
@@ -399,25 +411,28 @@ static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
|
|||||||
|
|
||||||
ast_cli(a->fd, "PJSIP Scheduled Tasks:\n\n");
|
ast_cli(a->fd, "PJSIP Scheduled Tasks:\n\n");
|
||||||
|
|
||||||
ast_cli(a->fd, " %1$-24s %2$-9s %3$-9s %4$-5s %6$-*5$s %7$-*5$s %8$-*5$s %9$7s\n",
|
ast_cli(a->fd, "%1$-45s %2$-9s %3$-9s %4$-5s %6$-*5$s %7$-*5$s %8$-*5$s %9$7s\n",
|
||||||
"Task Name", "Interval", "Times Run", "State",
|
"Task Name", "Interval", "Times Run", "State",
|
||||||
datelen, "Queued", "Last Started", "Next Start", "( secs)");
|
datelen, "Queued", "Last Started", "Next Start", "( secs)");
|
||||||
|
|
||||||
ast_cli(a->fd, " %1$-24.24s %2$-9.9s %3$-9.9s %4$-5.5s %6$-*5$.*5$s %7$-*5$.*5$s %9$-*8$.*8$s\n",
|
ast_cli(a->fd, "%1$-45.45s %2$-9.9s %3$-9.9s %4$-5.5s %6$-*5$.*5$s %7$-*5$.*5$s %9$-*8$.*8$s\n",
|
||||||
separator, separator, separator, separator,
|
separator, separator, separator, separator,
|
||||||
datelen, separator, separator, datelen + 8, separator);
|
datelen, separator, separator, datelen + 8, separator);
|
||||||
|
|
||||||
|
iter = ao2_iterator_init(sorted_tasks, AO2_ITERATOR_UNLINK);
|
||||||
ao2_rdlock(tasks);
|
for (; (schtd = ao2_iterator_next(&iter)); ao2_ref(schtd, -1)) {
|
||||||
i = ao2_iterator_init(tasks, AO2_ITERATOR_DONTLOCK);
|
|
||||||
while ((schtd = ao2_iterator_next(&i))) {
|
|
||||||
int next_run_sec;
|
int next_run_sec;
|
||||||
struct timeval next;
|
struct timeval next;
|
||||||
|
|
||||||
ao2_lock(schtd);
|
ao2_lock(schtd);
|
||||||
|
|
||||||
next_run_sec = ast_sip_sched_task_get_next_run(schtd) / 1000;
|
next_run_sec = ast_sip_sched_task_get_next_run(schtd) / 1000;
|
||||||
next = ast_tvadd(now, (struct timeval) {next_run_sec, 0});
|
if (next_run_sec < 0) {
|
||||||
|
/* Scheduled task is now canceled */
|
||||||
|
ao2_unlock(schtd);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
next = ast_tvadd(now, ast_tv(next_run_sec, 0));
|
||||||
|
|
||||||
ast_localtime(&schtd->when_queued, &tm, NULL);
|
ast_localtime(&schtd->when_queued, &tm, NULL);
|
||||||
ast_strftime(queued, sizeof(queued), log_format, &tm);
|
ast_strftime(queued, sizeof(queued), log_format, &tm);
|
||||||
@@ -432,7 +447,7 @@ static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
|
|||||||
ast_localtime(&next, &tm, NULL);
|
ast_localtime(&next, &tm, NULL);
|
||||||
ast_strftime(next_start, sizeof(next_start), log_format, &tm);
|
ast_strftime(next_start, sizeof(next_start), log_format, &tm);
|
||||||
|
|
||||||
ast_cli(a->fd, " %1$-24.24s %2$9.3f %3$9d %4$-5s %6$-*5$s %7$-*5$s %8$-*5$s (%9$5d)\n",
|
ast_cli(a->fd, "%1$-46.46s%2$9.3f %3$9d %4$-5s %6$-*5$s %7$-*5$s %8$-*5$s (%9$5d)\n",
|
||||||
schtd->name,
|
schtd->name,
|
||||||
schtd->interval / 1000.0,
|
schtd->interval / 1000.0,
|
||||||
schtd->run_count,
|
schtd->run_count,
|
||||||
@@ -441,11 +456,9 @@ static char *cli_show_tasks(struct ast_cli_entry *e, int cmd, struct ast_cli_arg
|
|||||||
next_start,
|
next_start,
|
||||||
next_run_sec);
|
next_run_sec);
|
||||||
ao2_unlock(schtd);
|
ao2_unlock(schtd);
|
||||||
|
|
||||||
ao2_cleanup(schtd);
|
|
||||||
}
|
}
|
||||||
ao2_iterator_destroy(&i);
|
ao2_iterator_destroy(&iter);
|
||||||
ao2_unlock(tasks);
|
ao2_ref(sorted_tasks, -1);
|
||||||
ast_cli(a->fd, "\n");
|
ast_cli(a->fd, "\n");
|
||||||
|
|
||||||
return CLI_SUCCESS;
|
return CLI_SUCCESS;
|
||||||
|
Reference in New Issue
Block a user