Inferno-RegMgr

 view release on metacpan or  search on metacpan

lib/Inferno/RegMgr/Service.pm  view on Meta::CPAN

package Inferno::RegMgr::Service;
use 5.010001;
use warnings;
use strict;
use utf8;
use Carp;

our $VERSION = 'v1.0.0';

use Scalar::Util qw( weaken );
use EV;


use constant RETRY => 1;    # sec, delay between re-connections


sub new {
    my ($class, $opt) = @_;
    my $self = {
        name    => $opt->{name},
        attr    => $opt->{attr},
        manager => undef,
        io      => undef,
        t       => undef,
    };
    return bless $self, $class;
}

sub START {
    my ($self) = @_;
    $self->{io} = $self->{manager}{registry}->open_new({
        name    => $self->{name},
        attr    => $self->{attr},
        cb      => $self,
        method  => '_cb_new',
    });
    weaken( $self->{io} );
    return;
}

sub _cb_new { ## no critic(ProhibitUnusedPrivateSubroutines)
    my ($self, $err) = @_;
    $self->{t} = EV::timer RETRY, 0, sub { $self->START() };
    return;
}

sub STOP {
    my ($self) = @_;
    if (defined $self->{io}) {
        $self->{io}->close();
    }
    $self->{t} = undef;
    return;
}

sub update {
    my ($self, $attrs) = @_;
    while (my ($attr, $val) = each %{ $attrs }) {
        $self->{attr}{ $attr } = $val;
    }
    if (defined $self->{io}) {
        $self->{manager}{registry}->update($self->{io}, $attrs);
    }
    return;
}

sub REFRESH {}


1; # Magic true value required at end of module
__END__

=encoding utf8

=head1 NAME

Inferno::RegMgr::Service - Register your service in OS Inferno's registry(4)


=head1 VERSION

This document describes Inferno::RegMgr::Service version v1.0.0


=head1 SYNOPSIS

See L<Inferno::RegMgr> for usage example.


=head1 DESCRIPTION

This module designed as task plugin for Inferno::RegMgr and can't be used without Inferno::RegMgr.

To register your service set service name and attributes while creating
new() Inferno::RegMgr::Service object, then attach() it to Inferno::RegMgr. You may wanna
keep reference to Inferno::RegMgr::Service object to update() service attributes
and/or unregister service using detach().


=head1 INTERFACE 

=over

=item new()

Create and return Inferno::RegMgr::Service object.

Accept HASHREF with options:

 name       REQUIRED service name
 attr       OPTIONAL hash with service attrs


=item update()

Update service attributes.



( run in 0.912 second using v1.01-cache-2.11-cpan-bbe5e583499 )