App-mqtt2job

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

   "name" : "App-mqtt2job",
   "prereqs" : {
      "configure" : {
         "requires" : {
            "ExtUtils::MakeMaker" : "0"
         }
      },
      "runtime" : {
         "requires" : {
            "Capture::Tiny" : "0",
            "DateTime" : "0",
            "Exporter" : "0",
            "File::Temp" : "0",
            "JSON" : "0",
            "Moose" : "0",
            "MooseX::Getopt" : "0",
            "Net::MQTT::Simple" : "0",
            "Template" : "0",
            "Try::Tiny" : "0",
            "strict" : "0",
            "warnings" : "0"

META.yml  view on Meta::CPAN

  ExtUtils::MakeMaker: '0'
dynamic_config: 0
generated_by: 'Dist::Zilla version 6.032, CPAN::Meta::Converter version 2.150010'
license: perl
meta-spec:
  url: http://module-build.sourceforge.net/META-spec-v1.4.html
  version: '1.4'
name: App-mqtt2job
requires:
  Capture::Tiny: '0'
  DateTime: '0'
  Exporter: '0'
  File::Temp: '0'
  JSON: '0'
  Moose: '0'
  MooseX::Getopt: '0'
  Net::MQTT::Simple: '0'
  Template: '0'
  Try::Tiny: '0'
  strict: '0'
  warnings: '0'

Makefile.PL  view on Meta::CPAN

    "ExtUtils::MakeMaker" => 0
  },
  "DISTNAME" => "App-mqtt2job",
  "EXE_FILES" => [
    "bin/mqtt2job"
  ],
  "LICENSE" => "perl",
  "NAME" => "App::mqtt2job",
  "PREREQ_PM" => {
    "Capture::Tiny" => 0,
    "DateTime" => 0,
    "Exporter" => 0,
    "File::Temp" => 0,
    "JSON" => 0,
    "Moose" => 0,
    "MooseX::Getopt" => 0,
    "Net::MQTT::Simple" => 0,
    "Template" => 0,
    "Try::Tiny" => 0,
    "strict" => 0,
    "warnings" => 0

Makefile.PL  view on Meta::CPAN

  },
  "VERSION" => "0.03",
  "test" => {
    "TESTS" => "t/*.t"
  }
);


my %FallbackPrereqs = (
  "Capture::Tiny" => 0,
  "DateTime" => 0,
  "Exporter" => 0,
  "File::Temp" => 0,
  "JSON" => 0,
  "Moose" => 0,
  "MooseX::Getopt" => 0,
  "Net::MQTT::Simple" => 0,
  "Template" => 0,
  "Test::More" => 0,
  "Try::Tiny" => 0,
  "lib" => 0,

bin/mqtt2job  view on Meta::CPAN

#!/usr/bin/env perl
use strict;
use warnings;

use Net::MQTT::Simple;
use JSON;
use DateTime;
use Try::Tiny;
use File::Temp;
use Capture::Tiny ':all';

use App::mqtt2job qw/ helper_v1 ha_helper_cfg /;

package MQTT::Job::Options;

use Moose;
with 'MooseX::Getopt';

bin/mqtt2job  view on Meta::CPAN

    warn( sprintf("Did not find %s in %s directory\n", $opt->cmd, $opt->job_dir) ) unless _cmd_ok($opt->cmd);
    print ha_helper_cfg({ task => $opt->task, cmd => $opt->cmd, args => $opt->args, base_topic => $opt->base_topic });
    $on_exit->(1);
}

$mqtt->subscribe($opt->base_topic, \&_on_message);
$mqtt->run;

sub _on_message {
    my ($topic, $message) = @_;
    my $dt = DateTime->now();
    my $obj = undef;

    try {
        $obj = decode_json($message);
    };

    my $msg = "";

    # check command and that it exists where expected
    if ($obj->{cmd} && _cmd_ok($obj->{cmd})) {

lib/App/mqtt2job.pm  view on Meta::CPAN


sub _helper_v1 {
    my $tpl = undef;
    $tpl = <<'_RUNNER_TPL';
#![% shebang %]

use strict;
use warnings;

use Net::MQTT::Simple;
use DateTime;
use JSON; 
use Capture::Tiny ':all';

my $dt_start = DateTime->now();
my $mqtt = Net::MQTT::Simple->new("[% mqtt_server %]:[% mqtt_port %]");

$mqtt->retain("[% base_topic %]/status/" . "[% task || "unknown" %]", encode_json({ status => "initiated", dt => "$dt_start", msg => "[% cmd %]" }) );
my $real_cmd = "[% job_dir %]/[% cmd %]";
my $real_args = "[% args %]";

print STDERR "$dt_start: [MQTT TASK] $real_cmd $real_args\n";

my ($output, $exit) = tee_merged {
    my @args = split(" ", $real_args);
    system($real_cmd, @args);
};

my $msg = ($exit == 0) ? "ok" : "failed";

my $dt_end = DateTime->now();
my $dt_elapsed_obj = $dt_end - $dt_start;
my $dt_elapsed = $dt_elapsed_obj->in_units("seconds");

my @split_output = split("\n", $output);
my $last_line = $split_output[$#split_output];

$output =~ s/\n//g;

$mqtt->retain("[% base_topic %]/status/" . "[% task || "unknown" %]", encode_json({ status => "completed", dt => "$dt_end", last_line => "$last_line", output => "$output", elapsed => "$dt_elapsed", msg => "$msg" }) );
print STDERR "$dt_end: [MQTT TASK] $real_cmd $real_args (${dt_elapsed}s) Exit: $exit\n";



( run in 0.294 second using v1.01-cache-2.11-cpan-05444aca049 )