Girocco::Notify: Introduce, add ref-change support to taskd
[girocco.git] / taskd / taskd.pl
blob35dafd63aaca970cf97ecc53e18e9be8d0215358
1 #!/usr/bin/perl
3 # taskd - Clone repositories on request
5 # taskd is Girocco mirroring servant; it processes requests for clones
6 # of given URLs received over its socket.
8 # When a request is received, new process is spawned that sets up
9 # the repository and reports further progress
10 # to .clonelog within the repository. In case the clone fails,
11 # .clone_failed is touched and .clone_in_progress is removed.
13 # Clone protocol:
14 # Alice sets up repository and touches .cloning
15 # Alice opens connection to Bob
16 # Alice sends project name through the connection
17 # Bob opens the repository and sends error code if there is a problem
18 # Bob closes connection
19 # Alice polls .clonelog in case of success.
20 # If Alice reads "@OVER@" from .clonelog, it stops polling.
22 # Ref-change protocol:
23 # Alice opens connection to Bob
24 # Alice sends ref-change command for each changed ref
25 # Alice closes connection
26 # Bob sends out notifications
28 # Based on perlipc example.
30 use strict;
31 use warnings;
33 use Girocco::Config;
34 use Girocco::Notify;
35 use Girocco::Project;
36 use Socket;
38 $| = 1;
40 sub logmsg { print '['.(scalar localtime)."] $0 $$: @_\n" }
42 my $NAME = $Girocco::Config::chroot.'/etc/taskd.socket';
43 my $uaddr = sockaddr_un($NAME);
45 socket(Server, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
46 unlink($NAME);
47 bind(Server, $uaddr) or die "bind: $!";
48 listen(Server, SOMAXCONN) or die "listen: $!";
49 chmod 0666, $NAME or die "chmod: $!";
52 use POSIX ":sys_wait_h";
53 sub REAPER {
54 my $child;
55 my $waitedpid;
56 while (($waitedpid = waitpid(-1, WNOHANG)) > 0) {
57 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
59 $SIG{CHLD} = \&REAPER; # loathe sysV
62 $SIG{CHLD} = \&REAPER; # Apollo 440
64 sub spawn {
65 my $coderef = shift;
67 my $pid = fork;
68 if (not defined $pid) {
69 logmsg "cannot fork: $!";
70 return;
71 } elsif ($pid) {
72 logmsg "begat $pid";
73 return; # I'm the parent
76 open STDIN, "<&Client" or die "can't dup client to stdin";
77 open STDOUT, ">&Client" or die "can't dup client to stdout";
78 exit &$coderef();
81 sub clone {
82 my ($name) = @_;
83 my $proj = Girocco::Project->load($name);
84 $proj or die "failed to load project $name";
85 $proj->{clone_in_progress} or die "project $name is not marked for cloning";
86 $proj->{clone_logged} and die "project $name is already being cloned";
87 print STDERR "cloning $name\n";
88 open STDOUT, ">".$Girocco::Config::reporoot."/".$name.".git/.clonelog" or die "cannot open clonelog: $!";
89 open STDERR, ">&STDOUT";
90 open STDIN, "</dev/null";
91 exec $Girocco::Config::basedir.'/taskd/clone.sh', "$name.git" or die "exec failed: $!";
94 sub ref_change {
95 my ($arg) = @_;
96 my ($name, $oldrev, $newrev, $ref) = split(/\s+/, $arg);
97 my $proj = Girocco::Project->load($name);
98 $proj or die "failed to load project $name";
99 print STDERR "ref-change $name ($ref: $oldrev -> $newrev)\n";
100 open STDIN, "</dev/null";
101 Girocco::Notify::ref_change($proj, $ref, $oldrev, $newrev);
104 while (1) {
105 unless (accept(Client, Server)) {
106 logmsg "accept failed: $!";
107 next;
109 logmsg "connection on $NAME";
110 spawn sub {
111 my $inp = <>;
112 chomp $inp;
113 my ($cmd, $arg) = $inp =~ /^([a-zA-Z0-9-]+)\s+(.*)$/;
114 if ($cmd eq 'clone') {
115 clone($arg);
116 } elsif ($cmd eq 'ref-change') {
117 ref_change($arg);
118 } else {
119 die "unknown command: $cmd";
122 close Client;
123 sleep 1;