This repository has been archived by the owner on Apr 13, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from techman83/status_tracking
Status tracking
- Loading branch information
Showing
13 changed files
with
642 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package App::KSP_CKAN::Status; | ||
|
||
use v5.010; | ||
use strict; | ||
use warnings; | ||
use autodie; | ||
use Method::Signatures 20140224; | ||
use Carp qw( croak ); | ||
use File::Slurper qw(read_text write_text); | ||
use Try::Tiny; | ||
use JSON; | ||
use File::Path qw(mkpath); | ||
use App::KSP_CKAN::Status::NetKAN; | ||
use Moo; | ||
use MooX::HandlesVia; | ||
use namespace::clean; | ||
|
||
# ABSTRACT: NetKAN Status Tracker | ||
|
||
# VERSION: Generated by DZP::OurPkg:Version | ||
|
||
=head1 SYNOPSIS | ||
use App::KSP_CKAN::Status; | ||
my $status = App::KSP_CKAN::Status->new( | ||
config => $config, | ||
); | ||
=head1 DESCRIPTION | ||
Status object for tracking status of at least initially, NetKAN | ||
indexing. | ||
=cut | ||
|
||
my $Ref = sub { | ||
croak("auth isn't a 'App::KSP_CKAN::Tools::Config' object!") unless $_[0]->DOES("App::KSP_CKAN::Tools::Config"); | ||
}; | ||
|
||
has 'config' => ( is => 'ro', required => 1, isa => $Ref ); | ||
has 'status_file' => ( is => 'ro', default => sub { "netkan.json" } ); | ||
has 'status_path' => ( is => 'ro', lazy => 1, builder => 1 ); | ||
has '_status_file' => ( is => 'ro', lazy => 1, builder => 1 ); | ||
has '_data' => ( is => 'ro', lazy => 1, builder => 1 ); | ||
has '_json' => ( is => 'ro', lazy => 1, builder => 1 ); | ||
|
||
has 'status' => ( | ||
is => 'rw', | ||
handles_via => 'Hash', | ||
handles => { | ||
get_val => 'get', | ||
set_val => 'set', | ||
all_keys => 'keys' | ||
}, | ||
default => sub { { } }, | ||
); | ||
|
||
method _build__json { | ||
return JSON->new->allow_blessed(1)->convert_blessed(1); | ||
} | ||
|
||
method _build_status_path { | ||
return $self->config->working."/status"; | ||
} | ||
|
||
method _build__status_file { | ||
if ( ! -d $self->status_path ) { | ||
mkpath($self->status_path); | ||
} | ||
|
||
return $self->status_path."/".$self->status_file; | ||
} | ||
|
||
method _build__data { | ||
my $raw; | ||
if (! -e $self->_status_file) { | ||
$raw = "{}"; | ||
} else { | ||
$raw = read_text($self->_status_file); | ||
} | ||
|
||
my $data; | ||
try { | ||
$data = $self->_json->decode($raw); | ||
} catch { | ||
$self->logdie("JSON Status failed to parse"); | ||
}; | ||
|
||
return $data; | ||
} | ||
|
||
=method get_status | ||
my $netkan_status = $status->get_status( "BaconLabs" ); | ||
Will return a reference to the "BaconLabs" status object. Creating | ||
it if necessary or loading it from the available data if it exists. | ||
=cut | ||
|
||
method get_status($name) { | ||
if ($self->_data->{$name}) { | ||
$self->set_val($name, App::KSP_CKAN::Status::NetKAN->new($self->_data->{$name})); | ||
} else { | ||
$self->set_val($name, App::KSP_CKAN::Status::NetKAN->new(name => $name)); | ||
} | ||
return $self->get_val($name); | ||
} | ||
|
||
=method write_json | ||
$status->write_json | ||
Writes our status file out to disk. | ||
=cut | ||
|
||
method write_json { | ||
my $json = $self->_json->encode($self->status); | ||
write_text($self->_status_file, $json); | ||
} | ||
|
||
1; |
Oops, something went wrong.