App-Netdisco

 view release on metacpan or  search on metacpan

lib/App/Netdisco/JobQueue/PostgreSQL.pm  view on Meta::CPAN


      schema(vars->{'tenant'})->resultset('Admin')
        ->search({ job => $job->id }, { for => 'update' })
        ->update({
          status => $job->status,
          log    => (ref($job->log) eq ref('')) ? $job->log : '',
          started  => $job->started,
          finished => $job->finished,
          (($job->action eq 'hook') ? (subaction => $job->subaction) : ()),
          ($job->only_namespace ? (action => ($job->action .'::'. $job->only_namespace)) : ()),
        });
    });
    $happy = true;
  }
  catch {
    # use DDP; p $job;
    error $_;
  };

  return $happy;
}

sub jq_log {
  return schema(vars->{'tenant'})->resultset('Admin')->search({
    (param('backend') ? ('me.backend' => param('backend')) : ()),
    (param('action') ? ('me.action' => param('action')) : ()),
    (param('device') ? (
      -or => [
        { 'me.device' => param('device') },
        { 'target.ip' => param('device') },
      ],
    ) : ()),
    (param('username') ? ('me.username' => param('username')) : ()),
    (param('status') ? (
      (param('status') eq 'Running') ? (
      -and => [
        { 'me.backend' => { '!=' => undef } },
        { 'me.status'  => 'queued' },
      ],
      ) : (
      'me.status' => lc(param('status'))
      )
    ) : ()),
    (param('duration') ? (
      -bool => [
        -or => [
          {
            'me.finished' => undef,
            'me.started'  => { '<' => \[q{(CURRENT_TIMESTAMP - ? ::interval)}, param('duration') .' minutes'] },
          },
          -and => [
            { 'me.started'  => { '!=' => undef } },
            { 'me.finished' => { '!=' => undef } },
            \[ q{ (me.finished - me.started) > ? ::interval }, param('duration') .' minutes'],
          ],
        ],
      ],
    ) : ()),
    'me.log' => [
      { '=' => undef },
      { '-not_like' => 'duplicate of %' },
    ],
  }, {
    prefetch => 'target',
    order_by => { -desc => [qw/entered device action/] },
    rows     => (setting('jobs_qdepth') || 50),
  })->with_times->hri->all;
}

sub jq_userlog {
  my $user = shift;
  return schema(vars->{'tenant'})->resultset('Admin')->search({
    username => $user,
    log      => { '-not_like' => 'duplicate of %' },
    finished => { '>' => \"(CURRENT_TIMESTAMP - interval '5 seconds')" },
  })->with_times->all;
}

sub jq_insert {
  my $jobs = shift;
  $jobs = [$jobs] if ref [] ne ref $jobs;

  my $happy = false;
  try {
    schema(vars->{'tenant'})->txn_do(sub {
      if (scalar @$jobs == 1 and defined $jobs->[0]->{device} and
          scalar grep {$_ eq $jobs->[0]->{action}} @{ setting('_inline_actions') || [] }) {

          # bit of a hack for heroku hosting to avoid DB overload
          return true if setting('defanged_admin') ne 'admin';

          my $spec = $jobs->[0];
          my $row = undef;

          if ($spec->{port}) {
              $row = schema(vars->{'tenant'})->resultset('DevicePort')
                                             ->find($spec->{port}, $spec->{device});
              undef $row unless
                scalar grep {('cf_'. $_) eq $spec->{action}}
                            grep {defined}
                            map {$_->{name}}
                            @{ setting('custom_fields')->{device_port} || [] };
          }
          else {
              $row = schema(vars->{'tenant'})->resultset('Device')
                                             ->find($spec->{device});
              undef $row unless
                scalar grep {('cf_'. $_) eq $spec->{action}}
                            grep {defined}
                            map {$_->{name}}
                            @{ setting('custom_fields')->{device} || [] };
          }

          die 'failed to find row for custom field update' unless $row;

          my $coder = JSON::PP->new->utf8(0)->allow_nonref(1)->allow_unknown(1);
          $spec->{subaction} = $coder->encode( $spec->{extra} || $spec->{subaction} );
          $spec->{action} =~ s/^cf_//;
          $row->make_column_dirty('custom_fields');
          $row->update({
            custom_fields => \['jsonb_set(custom_fields, ?, ?)'
                              => (qq{{$spec->{action}}}, $spec->{subaction}) ]
            })->discard_changes();
      }
      else {
          schema(vars->{'tenant'})->resultset('Admin')->populate([
            map {{
                device     => $_->{device},
                device_key => $_->{device_key},
                port       => $_->{port},
                action     => $_->{action},
                subaction  => ($_->{extra} || $_->{subaction}),
                username   => $_->{username},
                userip     => $_->{userip},



( run in 0.721 second using v1.01-cache-2.11-cpan-39bf76dae61 )