+sub handle_action{
+ my ($state,$database,$queue) = @_;
+ my $keyword;
+ my $actioned_keywords = [];
+ my $failed_keywords = [];
+ DEBUG("Beginning to handle actions for state '$state' database '$database'");
+ while ($keyword = $queue->dequeue) {
+ DEBUG("Handling state '$state' database '$database' keyword '$keyword'");
+ # handle the action, baybee
+ if ($state eq 'get') {
+ my $command_fh;
+ eval {
+ open($command_fh,'|-',
+ "$base_dir/get_${database}_results",
+ ) or die "unable to execute '$base_dir/get_${database}_results'";
+ print {$command_fh} "$keyword\n" or die "unable to print $keyword to 'get_${database}_results'";
+ close($command_fh) or die "Unable to close filehandle";
+ if ($? != 0) {
+ die "get_${database}_results with keyword $keyword failed with error code ".($?>>8);
+ }
+ };
+ if ($@) {
+ WARN($@);
+ push @{$failed_keywords}, $keyword;
+ next;
+ }
+ }
+ elsif ($state eq 'parse') {
+ eval {
+ write_command_to_file("parsed_results_${database}_${keyword}.txt",
+ "$base_dir/parse_${database}_results",
+ '--keywords',
+ $keyword,
+ );
+ };
+ if ($@) {
+ WARN("parse_${database}_results failed with $@");
+ push @{$failed_keywords}, $keyword;
+ next;
+ }
+ }
+ else {
+ die "I don't know how to handle state $state";
+ }
+ ADVISE("$state results from '$database' for '$keyword'");
+ push @{$actioned_keywords},$keyword;
+ }
+ return [$actioned_keywords,$failed_keywords];
+}