[asterisk-commits] mjordan: branch group/performance r399793 - /team/group/performance/main/cdr.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 25 14:24:04 CDT 2013


Author: mjordan
Date: Wed Sep 25 14:24:02 2013
New Revision: 399793

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=399793
Log:
Update CDRs to only subscribe if enabled

Modified:
    team/group/performance/main/cdr.c

Modified: team/group/performance/main/cdr.c
URL: http://svnview.digium.com/svn/asterisk/team/group/performance/main/cdr.c?view=diff&rev=399793&r1=399792&r2=399793
==============================================================================
--- team/group/performance/main/cdr.c (original)
+++ team/group/performance/main/cdr.c Wed Sep 25 14:24:02 2013
@@ -3823,6 +3823,67 @@
 	ast_cdr_engine_term();
 }
 
+/*!
+ * \brief Destroy the active Stasis subscriptions/router/topics
+ */
+static void destroy_subscriptions(void)
+{
+	if (channel_subscription) {
+		channel_subscription = stasis_forward_cancel(channel_subscription);
+		channel_subscription = NULL;
+	}
+	if (bridge_subscription) {
+		bridge_subscription = stasis_forward_cancel(bridge_subscription);
+		bridge_subscription = NULL;
+	}
+	if (parking_subscription) {
+		parking_subscription = stasis_forward_cancel(parking_subscription);
+		parking_subscription = NULL;
+	}
+	if (stasis_router) {
+		stasis_message_router_unsubscribe_and_join(stasis_router);
+		stasis_router = NULL;
+	}
+	ao2_cleanup(cdr_topic);
+	cdr_topic = NULL;
+}
+
+/*!
+ * \brief Create the Stasis subcriptions for CDRs
+ */
+static int create_subscriptions(void)
+{
+	cdr_topic = stasis_topic_create("cdr_engine");
+	if (!cdr_topic) {
+		return -1;
+	}
+
+	channel_subscription = stasis_forward_all(ast_channel_topic_all_cached(), cdr_topic);
+	if (!channel_subscription) {
+		return -1;
+	}
+	bridge_subscription = stasis_forward_all(ast_bridge_topic_all_cached(), cdr_topic);
+	if (!bridge_subscription) {
+		return -1;
+	}
+	parking_subscription = stasis_forward_all(ast_parking_topic(), cdr_topic);
+	if (!parking_subscription) {
+		return -1;
+	}
+
+	stasis_router = stasis_message_router_create(cdr_topic);
+	if (!stasis_router) {
+		return -1;
+	}
+	stasis_message_router_add_cache_update(stasis_router, ast_channel_snapshot_type(), handle_channel_cache_message, NULL);
+	stasis_message_router_add(stasis_router, ast_channel_dial_type(), handle_dial_message, NULL);
+	stasis_message_router_add(stasis_router, ast_channel_entered_bridge_type(), handle_bridge_enter_message, NULL);
+	stasis_message_router_add(stasis_router, ast_channel_left_bridge_type(), handle_bridge_leave_message, NULL);
+	stasis_message_router_add(stasis_router, ast_parked_call_type(), handle_parked_call_message, NULL);
+
+	return 0;
+}
+
 static int process_config(int reload)
 {
 	RAII_VAR(struct module_config *, mod_cfg, module_config_alloc(), ao2_cleanup);
@@ -3868,12 +3929,7 @@
 
 static void cdr_engine_cleanup(void)
 {
-	channel_subscription = stasis_forward_cancel(channel_subscription);
-	bridge_subscription = stasis_forward_cancel(bridge_subscription);
-	parking_subscription = stasis_forward_cancel(parking_subscription);
-	stasis_message_router_unsubscribe_and_join(stasis_router);
-	ao2_cleanup(cdr_topic);
-	cdr_topic = NULL;
+	destroy_subscriptions();
 }
 
 static void cdr_engine_shutdown(void)
@@ -3939,10 +3995,34 @@
 	}
 }
 
+/*!
+ * \brief Checks if CDRs are enabled and enables/disables the necessary options
+ */
+static int cdr_toggle_runtime_options(void)
+{
+	RAII_VAR(struct module_config *, mod_cfg,
+		ao2_global_obj_ref(module_configs), ao2_cleanup);
+
+	if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
+		if (create_subscriptions()) {
+			ast_log(AST_LOG_ERROR, "Failed to create Stasis subscriptions\n");
+			return 1;
+		}
+		if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
+			cdr_enable_batch_mode(mod_cfg->general);
+		} else {
+			ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
+		}
+	} else {
+		destroy_subscriptions();
+		ast_log(LOG_NOTICE, "CDR logging disabled.\n");
+	}
+
+	return 0;
+}
+
 int ast_cdr_engine_init(void)
 {
-	RAII_VAR(struct module_config *, mod_cfg, NULL, ao2_cleanup);
-
 	if (process_config(0)) {
 		return -1;
 	}
@@ -3954,34 +4034,6 @@
 	}
 	ao2_container_register("cdrs_by_channel", active_cdrs_by_channel, cdr_container_print_fn);
 
-	cdr_topic = stasis_topic_create("cdr_engine");
-	if (!cdr_topic) {
-		return -1;
-	}
-
-	channel_subscription = stasis_forward_all(ast_channel_topic_all_cached(), cdr_topic);
-	if (!channel_subscription) {
-		return -1;
-	}
-	bridge_subscription = stasis_forward_all(ast_bridge_topic_all_cached(), cdr_topic);
-	if (!bridge_subscription) {
-		return -1;
-	}
-	parking_subscription = stasis_forward_all(ast_parking_topic(), cdr_topic);
-	if (!parking_subscription) {
-		return -1;
-	}
-
-	stasis_router = stasis_message_router_create(cdr_topic);
-	if (!stasis_router) {
-		return -1;
-	}
-	stasis_message_router_add_cache_update(stasis_router, ast_channel_snapshot_type(), handle_channel_cache_message, NULL);
-	stasis_message_router_add(stasis_router, ast_channel_dial_type(), handle_dial_message, NULL);
-	stasis_message_router_add(stasis_router, ast_channel_entered_bridge_type(), handle_bridge_enter_message, NULL);
-	stasis_message_router_add(stasis_router, ast_channel_left_bridge_type(), handle_bridge_leave_message, NULL);
-	stasis_message_router_add(stasis_router, ast_parked_call_type(), handle_parked_call_message, NULL);
-
 	sched = ast_sched_context_create();
 	if (!sched) {
 		ast_log(LOG_ERROR, "Unable to create schedule context.\n");
@@ -3992,16 +4044,8 @@
 	ast_register_cleanup(cdr_engine_cleanup);
 	ast_register_atexit(cdr_engine_shutdown);
 
-	mod_cfg = ao2_global_obj_ref(module_configs);
-
-	if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
-		if (ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
-			cdr_enable_batch_mode(mod_cfg->general);
-		} else {
-			ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
-		}
-	} else {
-		ast_log(LOG_NOTICE, "CDR logging disabled.\n");
+	if (cdr_toggle_runtime_options()) {
+		return -1;
 	}
 
 	return 0;
@@ -4042,14 +4086,8 @@
 		}
 	}
 
-	if (ast_test_flag(&mod_cfg->general->settings, CDR_ENABLED)) {
-		if (!ast_test_flag(&mod_cfg->general->settings, CDR_BATCHMODE)) {
-			ast_log(LOG_NOTICE, "CDR simple logging enabled.\n");
-		} else {
-			cdr_enable_batch_mode(mod_cfg->general);
-		}
-	} else {
-		ast_log(LOG_NOTICE, "CDR logging disabled, data will be lost.\n");
+	if (cdr_toggle_runtime_options()) {
+		return -1;
 	}
 
 	return 0;




More information about the asterisk-commits mailing list