Utils.pm 1.2 KB

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