#------------------------------------------------------------
# Copyright (C) 2006 RoyaltyShare, Inc.   All Rights Reserved
#------------------------------------------------------------
package Job::Queue;

use strict;
use warnings;

use Sys::Hostname;

use lib '/app/tools/common/lib';
use Common::Assert;
use Common::Util;
use Common::Log;
use Common::XMLObject;
use base 'Common::XMLObject';

use lib '/app/tools/job/lib';
use Job::Job;
use Job::Status;
use Job::DB::Item::Job;

# !!! I think I'll remove this interface.
# !!! How does the Distribution stuff re-queue, anyway?
# !!! It makes more sense to either provide a 'requeue' method
# !!! to the Job::Executable class, or just have the call 'enqueue'.
#
# !!! Well... not so sure about that.   You sort of want to be able to pass in
# !!! a Job object, actually.
#
# !!! No, on _third_ thought, I will get rid of this interface.
# !!! Instead, I will add a 'copy constructor' to the Job class that
# !!! will take a Job object and return a copy, which you can then 'enqueue'.
#
sub DEPRECATED_RequeueJob {
    my $job = shift;
    assert($job);

    return Job::Queue::SubmitJob(
        hostname     => $job->{HostName},
        class        => $job->{Class},
        subclass     => $job->{Subclass},
        client_id    => $job->{ClientID},
        command_line => $job->{CommandLine},
        command_xml  => $job->{CommandXML},
        priority     => $job->{Priority}
    );
}

# --- The XML object methods, so we can render the queue.
#
# In this context, the Queue is essentially a list of Jobs.
#
# !!! We'll expand this to allow for additional filters.
#
sub _init {
    my ( $self, %args ) = @_;

    $self->SUPER::_init(%args);

    # Valid options to pass to search are:  maxAge, hideChildren, hostname, class, subclass, clientID

    $self->{Jobs} = [];

    my $allJobs = Job::DB::Item::Job->Search(%args);

    while ( my $job = $allJobs->next() ) {
        my $jobObj = Job::Job->new( _dbItem => $job );
        push @{ $self->{Jobs} }, $jobObj;
    }

    return $self;
}

sub GetNumQueued {
    return Job::DB::Item::Job->GetNumQueued();
}

1;
