Utils.pm 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package Trog::Utils;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use HTTP::Tiny::UNIX();
  7. use Trog::Log qw{WARN};
  8. use Trog::Config();
  9. # Deal with Params which may or may not be arrays
  10. sub coerce_array ($param) {
  11. my $p = $param || [];
  12. $p = [$param] if $param && ( ref $param ne 'ARRAY' );
  13. return $p;
  14. }
  15. sub strip_and_trunc ($s) {
  16. return unless $s;
  17. $s =~ s/<[^>]*>//g;
  18. return substr $s, 0, 280;
  19. }
  20. # Instruct the parent to restart. Normally this is HUP, but nginx-unit decides to be special.
  21. sub restart_parent ( $env ) {
  22. if ($env->{PSGI_ENGINE} && $env->{PSGI_ENGINE} eq 'nginx-unit') {
  23. my $conf = Trog::Config->get();
  24. my $nginx_socket = $conf->param('nginx-unit.socket');
  25. my $client = HTTP::Tiny::UNIX->new();
  26. my $res = $client->request('GET', "http:$nginx_socket//control/applications/tcms/restart" );
  27. WARN("could not reload application (got $res->{status} from nginx-unit)!") unless $res->{status} == 200;
  28. return 1;
  29. }
  30. my $parent = getppid;
  31. kill 'HUP', $parent;
  32. }
  33. 1;