File: jobrunner

package info (click to toggle)
libanyevent-websocket-client-perl 0.55-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 420 kB
  • sloc: perl: 1,022; sh: 5; makefile: 2
file content (57 lines) | stat: -rwxr-xr-x 1,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/perl

use strict;
use warnings;
use JSON qw( to_json );
use AnyEvent::Open3::Simple;
use AnyEvent::WebSocket::Client 0.12;

unless(@ARGV > 0)
{
  print "usage: $0 command [arg1 [ arg2 [ ... ] ]\n";
  exit 1;
}

my $client = AnyEvent::WebSocket::Client->new;
my $connection = $client->connect("ws://localhost:3000/run")->recv;

# error stdout stderr signal exit

my $done = AnyEvent->condvar;

my $ipc = AnyEvent::Open3::Simple->new(
  on_stdout => sub {
    my($proc, $line) = @_;
    print $line, "\n";
    $connection->send(to_json({ type => 'out', data => $line }));
  },
  on_stderr => sub {
    my($proc, $line) = @_;
    print STDERR $line, "\n";
    $connection->send(to_json({ type => 'err', data => $line }));
  },
  on_exit => sub {
    my($proc, $exit, $signal) = @_;
    $connection->send(to_json({ type => 'exit', exit => $exit, signal => $signal }));
    $done->send([$exit,$signal]);
  },
  on_error => sub {
    my($error) = @_;
    $connection->send(to_json({ type => 'error', data => $error }));
    $done->croak($error);
  },
);

$connection->send(to_json(\@ARGV));
$ipc->run(@ARGV);

my($exit,$signal) = @{ $done->recv };
if($signal)
{
  print STDERR "died with signal $signal\n";
  exit 1;
}
else
{
  exit $exit;
}