Utils.pm 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package Trog::Utils;
  2. use strict;
  3. use warnings;
  4. no warnings 'experimental';
  5. use feature qw{signatures};
  6. use UUID::Tiny();
  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. sub restart_parent ( $env ) {
  23. if ($env->{PSGI_ENGINE} && $env->{PSGI_ENGINE} eq 'nginx-unit') {
  24. my $conf = Trog::Config->get();
  25. my $nginx_socket = $conf->param('nginx-unit.socket');
  26. my $client = HTTP::Tiny::UNIX->new();
  27. my $res = $client->request('GET', "http:$nginx_socket//control/applications/tcms/restart" );
  28. WARN("could not reload application (got $res->{status} from nginx-unit)!") unless $res->{status} == 200;
  29. return 1;
  30. }
  31. my $parent = getppid;
  32. kill 'HUP', $parent;
  33. }
  34. sub uuid {
  35. return UUID::Tiny::create_uuid_as_string( UUID::Tiny::UUID_V1, UUID::Tiny::UUID_NS_DNS );
  36. }
  37. 1;