Skip to content

Instantly share code, notes, and snippets.

@berekuk
Created May 26, 2011 11:31
Show Gist options
  • Save berekuk/992960 to your computer and use it in GitHub Desktop.
Save berekuk/992960 to your computer and use it in GitHub Desktop.
ubic restart on code update
# vim: ft=perl
# this is ~/ubic/service/daxim-example
use strict;
use warnings;
use autodie;
use parent qw(Ubic::Service::SimpleDaemon);
use Ubic::Result qw(result);
sub _mtime {
my $self = shift;
my $mtime = (stat($self->{bin}))[9];
return $mtime;
}
sub _save_mtime {
my $self = shift;
my $mtime = $self->_mtime;
open my $fh, '>', "/Users/mmcleric/ubic/script.mtime";
print {$fh} $mtime;
close $fh;
}
sub _load_mtime {
my $self = shift;
open my $fh, '<', "/Users/mmcleric/ubic/script.mtime";
my $mtime = <$fh>;
return $mtime;
}
sub start_impl {
my $self = shift;
$self->_save_mtime;
$self->SUPER::start_impl();
}
sub status_impl {
my $self = shift;
my $status = $self->SUPER::status_impl();
if ($status->status eq 'running') {
my $mtime = $self->_mtime;
my $saved_mtime = $self->_load_mtime;
if ($saved_mtime != $mtime) {
return result('broken', "mtime = $mtime, saved_mtime = $saved_mtime");
}
}
return $status;
}
__PACKAGE__->new(
bin => "/Users/mmcleric/ubic/daxim-script.pl",
stdout => '/Users/mmcleric/ubic/daxim.stdout',
stderr => '/Users/mmcleric/ubic/daxim.stderr',
ubic_log => '/Users/mmcleric/ubic/daxim.ubic',
);
#!/usr/bin/env perl
# this is ~/ubic/daxim-script.pl
use strict;
use warnings;
sleep 1000;
mmcleric@dhcp175-143-red:~/ubic$ ubic start daxim-example
Starting daxim-example... started (pid 36838)
mmcleric@dhcp175-143-red:~/ubic$ ubic-watchdog daxim-example
mmcleric@dhcp175-143-red:~/ubic$ touch daxim-script.pl
mmcleric@dhcp175-143-red:~/ubic$ ubic-watchdog daxim-example
[Thu May 26 15:29:41 2011] daxim-example is broken, restarting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment