Add some utility scripts to toolbox
[girocco.git] / taskd / taskd.pl
blobc595713b519259bdbd5eb5d24536f4776e52c3d8
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 Girocco::User;
37 use Socket;
39 $| = 1;
41 sub logmsg { print '['.(scalar localtime)."] $0 $$: @_\n" }
43 my $NAME = $Girocco::Config::chroot.'/etc/taskd.socket';
44 my $uaddr = sockaddr_un($NAME);
46 socket(Server, PF_UNIX, SOCK_STREAM, 0) or die "socket: $!";
47 unlink($NAME);
48 bind(Server, $uaddr) or die "bind: $!";
49 listen(Server, SOMAXCONN) or die "listen: $!";
50 chmod 0666, $NAME or die "chmod: $!";
53 use POSIX ":sys_wait_h";
54 sub REAPER {
55 my $child;
56 my $waitedpid;
57 while (($waitedpid = waitpid(-1, WNOHANG)) > 0) {
58 logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
60 $SIG{CHLD} = \&REAPER; # loathe sysV
63 $SIG{CHLD} = \&REAPER; # Apollo 440
65 sub spawn {
66 my $coderef = shift;
68 my $pid = fork;
69 if (not defined $pid) {
70 logmsg "cannot fork: $!";
71 return;
72 } elsif ($pid) {
73 logmsg "begat $pid";
74 return; # I'm the parent
77 $SIG{CHLD} = 'DEFAULT';
79 open STDIN, "<&Client" or die "can't dup client to stdin";
80 open STDOUT, ">&Client" or die "can't dup client to stdout";
81 exit &$coderef();
84 sub clone {
85 my ($name) = @_;
86 my $proj = Girocco::Project->load($name);
87 $proj or die "failed to load project $name";
88 $proj->{clone_in_progress} or die "project $name is not marked for cloning";
89 $proj->{clone_logged} and die "project $name is already being cloned";
90 print STDERR "cloning $name\n";
91 open STDOUT, ">".$Girocco::Config::reporoot."/".$name.".git/.clonelog" or die "cannot open clonelog: $!";
92 open STDERR, ">&STDOUT";
93 open STDIN, "</dev/null";
94 exec $Girocco::Config::basedir.'/taskd/clone.sh', "$name.git" or die "exec failed: $!";
97 sub ref_change {
98 my ($arg) = @_;
99 my ($uid, $name, $oldrev, $newrev, $ref) = split(/\s+/, $arg);
101 my $proj = Girocco::Project->load($name);
102 $proj or die "failed to load project $name";
104 my $user;
105 if ($uid >= 0) {
106 $user = Girocco::User->load_by_uid($uid);
107 $user or die "failed to load user $uid";
110 print STDERR "ref-change $uid $name ($ref: $oldrev -> $newrev)\n";
111 open STDIN, "</dev/null";
112 Girocco::Notify::ref_change($proj, $user, $ref, $oldrev, $newrev);
113 return 0;
116 while (1) {
117 unless (accept(Client, Server)) {
118 logmsg "accept failed: $!";
119 next;
121 logmsg "connection on $NAME";
122 spawn sub {
123 my $inp = <>;
124 chomp $inp;
125 $inp or exit 0; # ignore empty connects
126 my ($cmd, $arg) = $inp =~ /^([a-zA-Z0-9-]+)\s+(.*)$/;
127 if ($cmd eq 'clone') {
128 clone($arg);
129 } elsif ($cmd eq 'ref-change') {
130 ref_change($arg);
131 } else {
132 die "unknown command: $cmd";
135 close Client;
136 sleep 1;