From b7629e4c2b80f8507f7e3a2950de238cd6ae233e Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Fri, 17 Nov 2023 10:44:46 -0500 Subject: [PATCH] fix(tools): enable anvil-access-module to emit events --- tools/anvil-access-module | 43 +++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/tools/anvil-access-module b/tools/anvil-access-module index a2688cc8..6e8e49aa 100755 --- a/tools/anvil-access-module +++ b/tools/anvil-access-module @@ -474,19 +474,19 @@ sub pstderr $anvil->Get->switches; -$anvil->Database->connect; -$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132" }); -if (not $anvil->data->{sys}{database}{connections}) -{ - # No databases, exit. - $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003" }); - $anvil->nice_exit({ exit_code => 1 }); -} - my $data_hash = $anvil->data->{switches}{'data'}; my $switch_debug = $anvil->data->{switches}{'debug'} // 3; my $db_access_mode = $anvil->data->{switches}{'mode'} // ""; my $db_uuid = $anvil->data->{switches}{'uuid'}; +# +# Events in this context are simply printing the event name before +# and/or after operations. The output should enable other programs to +# parse and activate additional logic as necessary. +# +# Event names should be present-tense before an operation, and +# past-tense after. +# +my $emit_events = $anvil->data->{switches}{'emit-events'}; my $pre_data = $anvil->data->{switches}{'predata'}; my $script_file = $anvil->data->{switches}{'script'} // "-"; my $sql_query = $anvil->data->{switches}{'query'}; @@ -494,6 +494,19 @@ my $sub_module_name = $anvil->data->{switches}{'sub-module'} // "Database"; my $sub_name = $anvil->data->{switches}{'sub'} // ""; my $sub_params = $anvil->data->{switches}{'sub-params'} // "{}"; +emit("initialized"); + +$anvil->Database->connect; +$anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 2, secure => 0, key => "log_0132" }); +if (not $anvil->data->{sys}{database}{connections}) +{ + # No databases, exit. + $anvil->Log->entry({ source => $THIS_FILE, line => __LINE__, level => 0, 'print' => 1, priority => "err", key => "error_0003" }); + $anvil->nice_exit({ exit_code => 1 }); +} + +emit("connected"); + if ($sql_query) { my $results = db_access({ db_uuid => $db_uuid, sql_query => $sql_query, db_access_mode => $db_access_mode }); @@ -612,4 +625,16 @@ else }; } +emit("exit"); + $anvil->nice_exit({ exit_code => 0 }); + +################################################## +# Functions +################################################## +# TODO: need to move all subroutines down here. + +sub emit +{ + pstdout("event=".$_[0]) if ($emit_events); +}