| 1 | package Ubic::Service::Common; | ||
| 2 | $Ubic::Service::Common::VERSION = '1.59'; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | # ABSTRACT: common way to construct new service by specifying several callbacks | ||
| 7 | |||
| 8 | |||
| 9 | use Params::Validate qw(:all); | ||
| 10 | |||
| 11 | use parent qw(Ubic::Service::Skeleton); | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my $class = shift; | ||
| 17 | my $params = validate(@_, { | ||
| 18 | start => { type => CODEREF }, | ||
| 19 | stop => { type => CODEREF }, | ||
| 20 | status => { type => CODEREF }, | ||
| 21 | name => { type => SCALAR, regex => qr/^[\w-]+$/, optional => 1 }, # violates Ubic::Service encapsulation... | ||
| 22 | port => { type => SCALAR, regex => qr/^\d+$/, optional => 1 }, | ||
| 23 | custom_commands => { type => HASHREF, default => {} }, | ||
| 24 | user => { type => SCALAR, optional => 1 }, | ||
| 25 | group => { type => SCALAR | ARRAYREF, optional => 1 }, | ||
| 26 | timeout_options => { type => HASHREF, default => {} }, | ||
| 27 | }); | ||
| 28 | if ($params->{custom_commands}) { | ||
| 29 | for (keys %{$params->{custom_commands}}) { | ||
| 30 | ref($params->{custom_commands}{$_}) eq 'CODE' or croak "Callback expected at custom command $_"; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | my $self = bless {%$params} => $class; | ||
| 34 | return $self; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub port { | ||
| 38 | my $self = shift; | ||
| 39 | return $self->{port}; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub status_impl { | ||
| 43 | my $self = shift; | ||
| 44 | return $self->{status}->(); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub start_impl { | ||
| 48 | my $self = shift; | ||
| 49 | return $self->{start}->(); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub stop_impl { | ||
| 53 | my $self = shift; | ||
| 54 | return $self->{stop}->(); | ||
| 55 | } | ||
| 56 | |||
| 57 | sub timeout_options { | ||
| 58 | my $self = shift; | ||
| 59 | return $self->{timeout_options}; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub custom_commands { | ||
| 63 | my $self = shift; | ||
| 64 | return keys %{$self->{custom_commands}}; | ||
| 65 | } | ||
| 66 | |||
| 67 | sub user { | ||
| 68 | my $self = shift; | ||
| 69 | return $self->{user} if defined $self->{user}; | ||
| 70 | return $self->SUPER::user(); | ||
| 71 | } | ||
| 72 | |||
| 73 | # copypasted from Ubic::Service::SimpleDaemon... maybe we need moose after all | ||
| 74 | sub group { | ||
| 75 | my $self = shift; | ||
| 76 | my $groups = $self->{group}; | ||
| 77 | return $self->SUPER::group() if not defined $groups; | ||
| 78 | return @$groups if ref $groups eq 'ARRAY'; | ||
| 79 | return $groups; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub do_custom_command { | ||
| 83 | my ($self, $command) = @_; | ||
| 84 | unless (exists $self->{custom_commands}{$command}) { | ||
| 85 | croak "Command '$command' not implemented"; | ||
| 86 | } | ||
| 87 | $self->{custom_commands}{$command}->(); | ||
| 88 | } | ||
| 89 | |||
| 90 | 1; | ||
| 91 | |||
| 92 | __END__ |