#!/usr/bin/perl -w
######################################################################
#
# $Id: webjob-jqd-delete-job,v 1.19 2012/01/07 08:01:18 mavrik Exp $
#
######################################################################
#
# Copyright 2007-2012 The WebJob Project, All Rights Reserved.
#
######################################################################
#
# Purpose: Remove one or more jobs from the specified job queues.
#
######################################################################

use FindBin qw($Bin $RealBin); use lib ("$Bin/../lib/perl5/site_perl", "$RealBin/../lib/perl5/site_perl", "/usr/local/webjob/lib/perl5/site_perl", "/opt/local/webjob/lib/perl5/site_perl");

use strict;
use File::Basename;
use Getopt::Std;
use WebJob::JqdRoutines 1.024;
use WebJob::KvpRoutines 1.029;
use WebJob::Properties 1.035;

BEGIN
{
  ####################################################################
  #
  # The Properties hash is essentially private. Those parts of the
  # program that wish to access or modify the data in this hash need
  # to call GetProperties() to obtain a reference.
  #
  ####################################################################

  my (%hProperties);

  ####################################################################
  #
  # Initialize regex variables.
  #
  ####################################################################

  $hProperties{'CommonRegexes'} = PropertiesGetGlobalRegexes();

  ####################################################################
  #
  # Initialize platform-specific variables.
  #
  ####################################################################

  if ($^O =~ /MSWin(32|64)/i)
  {
    $hProperties{'OsClass'} = "WINX";
  }
  else
  {
    $hProperties{'OsClass'} = "UNIX";
  }

  ####################################################################
  #
  # Define helper routines.
  #
  ####################################################################

  sub GetProperties
  {
    return \%hProperties;
  }
}

######################################################################
#
# Main Routine
#
######################################################################

  ####################################################################
  #
  # Punch in and go to work.
  #
  ####################################################################

  my ($phProperties);

  $phProperties = GetProperties();

  $$phProperties{'Program'} = basename(__FILE__);

  ####################################################################
  #
  # Get Options.
  #
  ####################################################################

  my (%hOptions);

  if (!getopts('d:e:G:i:l:s:', \%hOptions))
  {
    Usage($$phProperties{'Program'});
  }

  ####################################################################
  #
  # A job queue directory, '-d', is optional.
  #
  ####################################################################

  $$phProperties{'JobQueueDirectory'} = (exists($hOptions{'d'})) ? $hOptions{'d'} : "/var/webjob/spool/jqd";

  ####################################################################
  #
  # A comma delimited list of excludes, '-e', is optional.
  #
  ####################################################################

  $$phProperties{'ExcludeList'} = (exists($hOptions{'e'})) ? $hOptions{'e'} : "";

  ####################################################################
  #
  # A group file, '-G', is optional.
  #
  ####################################################################

  $$phProperties{'GroupsFile'} = (exists($hOptions{'G'})) ? $hOptions{'G'} : "/var/webjob/config/jqd/groups";

  ####################################################################
  #
  # A comma delimited list of includes, '-i', is required.
  #
  ####################################################################

  my (@aIncludes);

  $$phProperties{'IncludeList'} = (exists($hOptions{'i'})) ? $hOptions{'i'} : "";

  if ($$phProperties{'IncludeList'} eq "")
  {
    Usage($$phProperties{'Program'});
  }

  ####################################################################
  #
  # A log file, '-l', is optional.
  #
  ####################################################################

  $$phProperties{'LogFile'} = (exists($hOptions{'l'})) ? $hOptions{'l'} : "/var/webjob/logfiles/jqd.log";

  ####################################################################
  #
  # A comma delimited list of job states, '-s', is optional.
  #
  ####################################################################

  $$phProperties{'OldStateList'} = (exists($hOptions{'s'})) ? $hOptions{'s'} : "todo,sent,open";

  ####################################################################
  #
  # If there isn't at least one argument left, it's an error.
  #
  ####################################################################

  if (scalar(@ARGV) < 1)
  {
    Usage($$phProperties{'Program'});
  }

  ####################################################################
  #
  # Finalize the old/new states.
  #
  ####################################################################

  my (%hOldStates);

  if ($$phProperties{'OldStateList'} =~ /^all$/i)
  {
    foreach my $sOldState ("hold", "todo", "sent", "open", "done", "pass", "fail", "foul")
    {
      $hOldStates{$sOldState}++;
    }
  }
  else
  {
    foreach my $sOldState (split(/,/, $$phProperties{'OldStateList'}))
    {
      if ($sOldState =~ /^(hold|todo|sent|open|done|pass|fail|foul)$/i)
      {
        $hOldStates{lc($1)}++;
      }
      else
      {
        print STDERR "$$phProperties{'Program'}: Error='Invalid job state ($sOldState).'\n";
        exit(2);
      }
    }
  }

  @{$$phProperties{'OldStates'}} = sort(keys(%hOldStates));
  if (scalar(@{$$phProperties{'OldStates'}}) < 1)
  {
    print STDERR "$$phProperties{'Program'}: Error='At least one state must be specified.'\n";
    exit(2);
  }

  $$phProperties{'NewState'} = undef;

  ####################################################################
  #
  # Make sure the job queue directory exists.
  #
  ####################################################################

  if (!-d $$phProperties{'JobQueueDirectory'})
  {
    print STDERR "$$phProperties{'Program'}: Error='Base directory ($$phProperties{'JobQueueDirectory'}) does not exist.'\n";
    exit(2);
  }

  ####################################################################
  #
  # Create forward/reverse queue maps.
  #
  ####################################################################

  my (%hForwardQueueMap, %hQueueMapArgs, %hReverseQueueMap);

  %hQueueMapArgs =
  (
    'ForwardQueueMap'   => \%hForwardQueueMap,
    'JobQueueDirectory' => $$phProperties{'JobQueueDirectory'},
    'KeyRegex'          => "(?:" . $$phProperties{'CommonRegexes'}{'ClientId'} . "|" . $$phProperties{'CommonRegexes'}{'ClientSuppliedFilename'} . ")",
    'ReverseQueueMap'   => \%hReverseQueueMap,
    'SharedLock'        => 1,
    'ValueRegex'        => "\\d+",
  );
  if (!JqdSetupQueueMaps(\%hQueueMapArgs))
  {
    print STDERR "$$phProperties{'Program'}: Error='$hQueueMapArgs{'Error'}'\n";
    exit(2);
  }

  ####################################################################
  #
  # Create group queue map, and conditionally fill it with groups.
  #
  ####################################################################

  my (%hGroupMap);

  %hGroupMap = ();

  if (-f $$phProperties{'GroupsFile'})
  {
    my $sKeyRegex = $$phProperties{'CommonRegexes'}{'Group'};
    my $sUnitRegex = "(?:" . $$phProperties{'CommonRegexes'}{'GroupToken'} . "|" . $$phProperties{'CommonRegexes'}{'ClientId'} . ")";
    my $sValueRegex = "(?:|$sUnitRegex(?:,$sUnitRegex)*)"; # Empty groups are allowed.
    my %hLArgs =
    (
      'File'           => $$phProperties{'GroupsFile'},
      'Properties'     => \%hGroupMap,
      'Template'       => { $sKeyRegex => $sValueRegex, },
      'VerifyValues'   => 1,
    );
    if (!KvpGetKvps(\%hLArgs))
    {
      print STDERR "$$phProperties{'Program'}: Error='$hLArgs{'Error'}'\n";
      exit(2);
    }
  }

  ####################################################################
  #
  # Resolve the include and exclude lists.
  #
  ####################################################################

  my (%hExcludes, %hIncludes, %hResolverArgs);

  %hResolverArgs =
  (
    'ForwardMap'     => \%hForwardQueueMap,
    'GroupMap'       => \%hGroupMap,
    'QueueMap'       => \%hIncludes,
    'QueueList'      => $$phProperties{'IncludeList'},
    'RecursionStack' => [],
  );
  if (!JqdResolveQueueList(\%hResolverArgs))
  {
    print STDERR "$$phProperties{'Program'}: IncludeList='$$phProperties{'IncludeList'}' Error='$hResolverArgs{'Error'}'\n";
    exit(2);
  }

  %hResolverArgs =
  (
    'ForwardMap'     => \%hForwardQueueMap,
    'GroupMap'       => \%hGroupMap,
    'QueueMap'       => \%hExcludes,
    'QueueList'      => $$phProperties{'ExcludeList'},
    'RecursionStack' => [],
  );
  if (!JqdResolveQueueList(\%hResolverArgs))
  {
    print STDERR "$$phProperties{'Program'}: ExcludeList='$$phProperties{'ExcludeList'}' Error='$hResolverArgs{'Error'}'\n";
    exit(2);
  }

  ####################################################################
  #
  # Bash the excludes against the includes, and remove the overlap.
  #
  ####################################################################

  foreach my $sExclude (sort(keys(%hExcludes)))
  {
    if (exists($hIncludes{$sExclude}))
    {
      delete($hIncludes{$sExclude});
    }
  }

  ####################################################################
  #
  # Set the job remover.
  #
  ####################################################################

  my ($sRemoverUsername, $sRemoverUid);

  if ($$phProperties{'OsClass'} =~ /^UNIX$/)
  {
    my $sRemoverTty = `tty`;
    if (defined($sRemoverTty) && $sRemoverTty =~ /^\/dev\//)
    {
      $sRemoverTty =~ s/[\r\n]*$//;
      $sRemoverUid = (stat($sRemoverTty))[4];
      if (!defined($sRemoverUid))
      {
        print STDERR "$$phProperties{'Program'}: Error='Unable to determine user ID (UID).'\n";
        exit(2);
      }
    }
    else
    {
      $sRemoverUid = $<;
    }
  }
  else
  {
    $sRemoverUid = $<;
  }

  $sRemoverUsername = (getpwuid($sRemoverUid))[0];
  if (!defined($sRemoverUid))
  {
    print STDERR "$$phProperties{'Program'}: Error='Unable to determine username.'\n";
    exit(2);
  }

  ####################################################################
  #
  # Do some work.
  #
  ####################################################################

  my (@aResults, %hLogArgs, %hQueueLockArgs);

  %hLogArgs =
  (
    'Creator'       => $sRemoverUsername,
    'LogFile'       => $$phProperties{'LogFile'},
    'Pid'           => $$,
    'Program'       => $$phProperties{'Program'},
  );

  foreach my $sJobNameRegex (@ARGV)
  {
    foreach my $sQueue (sort(keys(%hIncludes)))
    {
      ################################################################
      #
      # Set the queue directory.
      #
      ################################################################

      my ($sJqdQueueDirectory);

      $sJqdQueueDirectory = $$phProperties{'JobQueueDirectory'} . "/" . $sQueue;
      if (!-d $sJqdQueueDirectory)
      {
        next;
      }

      ################################################################
      #
      # Acquire the change lock. This is an exclusive lock.
      #
      ################################################################

      %hQueueLockArgs =
      (
        'LockFile' => $sJqdQueueDirectory . "/" . "change.lock",
        'LockMode' => "+<", # The file must exist or this will fail.
      );
      if (!JqdLockFile(\%hQueueLockArgs))
      {
        print STDERR "$$phProperties{'Program'}: Queue='$sQueue' Error='$hQueueLockArgs{'Error'}'\n";
        next;
      }

      ################################################################
      #
      # If the queue is in the locked state, we're done since no jobs
      # may be removed.
      #
      ################################################################

      if (JqdIsQueueLocked({ 'Directory' => $sJqdQueueDirectory }))
      {
        print STDERR "$$phProperties{'Program'}: Queue='$sQueue' Warning='Queue is locked. No jobs may be removed.'\n";
        next;
      }

      ################################################################
      #
      # Remove jobs that match the specified expression, but only do
      # this for the specified job states.
      #
      ################################################################

      foreach my $sOldState (@{$$phProperties{'OldStates'}})
      {
        my $sStateDirectory = $sJqdQueueDirectory . "/" . $sOldState;
        if (!opendir(DIR, $sStateDirectory))
        {
          print STDERR "$$phProperties{'Program'}: Queue='$sQueue' Error='Directory ($sStateDirectory) could not be opened ($!).'\n";
          next;
        }
        my @aJobFiles = map("$sStateDirectory/$_", sort(grep(/$sJobNameRegex/, readdir(DIR))));
        closedir(DIR);
        foreach my $sJobFile (@aJobFiles)
        {
          if (-f $sJobFile)
          {
#FIXME Move this code block into a subroutine. <<EOF
            $hLogArgs{'NewQueueState'} = $$phProperties{'NewState'};
            $hLogArgs{'OldQueueState'} = $sOldState;
            $hLogArgs{'Queue'} = $sQueue;
            $hLogArgs{'QueueTag'} = basename($sJobFile);
            $hLogArgs{'Result'} = "fail";

            my (%hJobProperties, %hLArgs);

            %hLArgs =
            (
              'File'           => $sJobFile,
              'Properties'     => \%hJobProperties,
#FIXME Which keys should be required?
              'RequiredKeys'   => ['Command', 'CommandPath', 'JobGroup'],
              'RequireAllKeys' => 0,
              'Template'       => PropertiesGetGlobalTemplates()->{'jqd.job'},
              'VerifyValues'   => 1,
            );
            if (!KvpGetKvps(\%hLArgs))
            {
              $hLogArgs{'Message'} = $hLArgs{'Error'};
              JqdLogMessage(\%hLogArgs);
              print STDERR "$$phProperties{'Program'}: Queue='$sQueue' Error='$hLogArgs{'Message'}'\n";
              push(@aResults, "fail|$sJobFile");
              next;
            }

            $hLogArgs{'Command'} = $hJobProperties{'Command'},
            $hLogArgs{'CommandSize'} = $hJobProperties{'CommandSize'},
            $hLogArgs{'JobGroup'} = $hJobProperties{'JobGroup'},
            $hLogArgs{'PoundName'} = basename($hJobProperties{'CommandPath'});
#FIXME EOF

            if (!unlink($sJobFile))
            {
              $hLogArgs{'Message'} = "Job file ($sJobFile) could not be deleted ($!).";
              print STDERR "$$phProperties{'Program'}: Queue='$sQueue' Error='$hLogArgs{'Message'}'\n";
              push(@aResults, "fail|$sJobFile");
              next;
            }
            else
            {
              $hLogArgs{'Result'} = "pass";
              $hLogArgs{'Message'} = "Job deleted successfully.";
              push(@aResults, "pass|$sJobFile");
            }
            JqdLogMessage(\%hLogArgs);
          }
        }
      }
    }
    continue
    {
      ################################################################
      #
      # Release the change lock.
      #
      ################################################################

      JqdUnlockFile(\%hQueueLockArgs);
    }
  }

  ####################################################################
  #
  # Report the results.
  #
  ####################################################################

  foreach my $sResult (@aResults)
  {
    print $sResult, "\n";
  }

  ####################################################################
  #
  # Shutdown and go home.
  #
  ####################################################################

  1;


######################################################################
#
# Usage
#
######################################################################

sub Usage
{
  my ($sProgram) = @_;
  print STDERR "\n";
  print STDERR "Usage: $sProgram [-d jqd-dir] [-e queue[,queue[...]]] [-G group-file] [-l log-file] [-s {all|state[,state[...]]}] -i queue[,queue[...]] job-regex [job-regex ...]\n";
  print STDERR "\n";
  exit(1);
}


=pod

=head1 NAME

webjob-jqd-delete-job - Remove one or more jobs from the specified job queues.

=head1 SYNOPSIS

B<webjob-jqd-delete-job> B<[-d jqd-dir]> B<[-e queue[,queue[...]]]> B<[-G group-file]> B<[-l log-file]> B<[-s {all|state[,state[...]]}]> B<-i queue[,queue[...]]> B<job-regex [job-regex ...]>

=head1 DESCRIPTION

This utility removes one or more jobs from the specified job queues.
Each B<job-regex> argument is intepreted as a Perl regular expression.

=head1 OPTIONS

=over 4

=item B<-d jqd-dir>

Specifies the base directory where queues live.  This directory must
exist.  The default value is /var/webjob/spool/jqd.

=item B<-e queue[,queue[...]]>

Specifies one or more queues or queue groups that are to be excluded
from the operation.  Note that group names must be prefixed with '%'.
For example, a group called 'test' must be specified as '%test'.

=item B<-G group-file>

Specifies an alternate group file.  The default group file is
/var/webjob/config/jqd/groups.

=item B<-i queue[,queue[...]]>

Specifies one or more queues or queue groups that are to be included
in the operation.  Note that group names must be prefixed with '%'.
For example, a group called 'test' must be specified as '%test'.

=item B<-l log-file>

Specifies an alternate log file.  The default value is
/var/webjob/logfiles/jqd.log.

=item B<-s {all|state[,state[...]]}>

Restrict the delete operation to the specified list of job states.
Currently, the following states are supported: 'hold', 'todo', 'sent',
'open', 'done', 'pass', 'fail', and 'foul'.  You may also use the
keyword 'all' as an alias for all states.

=back

=head1 AUTHOR

Klayton Monroe

=head1 SEE ALSO

webjob-jqd-create-job(1), webjob-jqd-list-jobs(1)

=head1 LICENSE

All documentation and code are distributed under same terms and
conditions as B<WebJob>.

=cut
