<commit_before> use MooseX::Declare;

use 5.010;

class Artemis::MCP::Scheduler::Controller
{
        use Perl6::Junction qw/ any /;
        use Artemis::Model 'model';
        use aliased 'Artemis::MCP::Scheduler::Algorithm';
        use aliased 'Artemis::MCP::Scheduler::MergedQueue';

        has hostlist  => (is => 'rw', isa => 'ArrayRef');
        has algorithm => (is => 'rw',
                          isa => 'Artemis::MCP::Scheduler::Algorithm',
                          default => sub {
                                          Algorithm->new_with_traits
                                          (
                                           traits => ['Artemis::MCP::Scheduler::Algorithm::WFQ']
                                          );
                                         }
                         );
        has merged_queue => (is => 'rw', isa => MergedQueue, default => sub { MergedQueue->new });

        method BUILD {
                $self->merged_queue->wanted_length( $self->algorithm->queue_count );
        }

        method fill_merged_queue()
        {
                my $count_missing_jobs = $self->merged_queue->wanted_length - $self->merged_queue->length;
                my %queues;
                my $queue_rs = model('TestrunDB')->resultset('Queue');
                foreach my $queue_name( map {$_->name} $queue_rs->all ) {
                        $queues{$queue_name} = 1;
                }

                # fill up to wanted merged_queue length, only accept shorter merged_queue if no queue has jobs to offer
                while($count_missing_jobs > 0)
                {
                        my $queue = $self->algorithm->get_next_queue();
                        my $testrun_rs = $queue->queued_testruns;
                        my $job   = $testrun_rs->first;
                        if ($job) {
                                $count_missing_jobs--;
                                $self->merged_queue->add($job);
                        } else {
                                delete $queues{$queue->name};
                        }
                        last if not %queues;
                }
        }

        method adapt_merged_queue_length(Any $job, ArrayRef $free_hosts) {
                if (not $job) {
                        # increase merged_queue length if no job found,
                        if ($self->merged_queue->wanted_length - $self->merged_queue->length < 1) # not longer than current length + 1
                        {
                                my $lookup_queue = $self->algorithm->lookup_next_queue();
                                my $queuehosts   = $lookup_queue->queuehosts;

                                if ( grep {$_->{host}->queuehosts->count == 0 } @$free_hosts   ) { # increase if at least one host is free for all queues
                                        $self->merged_queue->wanted_length( $self->merged_queue->wanted_length + 1 );

                                } elsif ($queuehosts->count) {                                     # increase if at least one host is bound to next queue
                                        my @queuehosts = map {$_->host->name}         $queuehosts->all;
                                        my @free_hosts = map {$_->{host}->name} @$free_hosts;
                                        if (any(@queuehosts) eq any(@free_hosts)) {
                                                $self->merged_queue->wanted_length( $self->merged_queue->wanted_length + 1 );
                                        }
                                } else {
                                        $self->algorithm->get_next_queue;
                                }
                        }
                } else {
                        # count down merged_queue again on success,
                        # but not smaller that count queues
                        $self->merged_queue->wanted_length( $self->merged_queue->wanted_length - 1 )
                            if $self->merged_queue->wanted_length > $self->algorithm->queue_count;
                }
        }

        method overfill_merged_queue()
        {
                my $queue_rs = model('TestrunDB')->resultset('Queue');
                my %queues;
                foreach my $queue( $queue_rs->all ) {
                        $queues{$queue->name} = $queue;
                }
                
                my $jobs = $self->merged_queue->get_testrequests;
                foreach my $tr( $jobs->all() ) {
                        delete($queues{$tr->queue->name}) if defined $queues{$tr->queue->name};
                }
                
                foreach my $queue_name (keys %queues) {
                        my $queue      = $queues{$queue_name};
                        my $testrun_rs = $queue->queued_testruns;
                        my $job        = $testrun_rs->first;
                        $self->merged_queue->add($job) if $job;
                }
        }


        method get_next_job(Any %args) {
                my ($queue, $job);

                do {{

                        $self->fill_merged_queue;
                        my $free_hosts = Artemis::Model::free_hosts_with_features();
                        return if not ($free_hosts and @$free_hosts);
                        $job = $self->merged_queue->get_first_fitting($free_hosts);
                        $self->overfill_merged_queue() if not $job;<commit_msg>MCP:  askip queue in filling merged_queue if queue is not active<commit_after> use MooseX::Declare;

use 5.010;

class Artemis::MCP::Scheduler::Controller
{
        use Perl6::Junction qw/ any /;
        use Artemis::Model 'model';
        use aliased 'Artemis::MCP::Scheduler::Algorithm';
        use aliased 'Artemis::MCP::Scheduler::MergedQueue';

        has hostlist  => (is => 'rw', isa => 'ArrayRef');
        has algorithm => (is => 'rw',
                          isa => 'Artemis::MCP::Scheduler::Algorithm',
                          default => sub {
                                          Algorithm->new_with_traits
                                          (
                                           traits => ['Artemis::MCP::Scheduler::Algorithm::WFQ']
                                          );
                                         }
                         );
        has merged_queue => (is => 'rw', isa => MergedQueue, default => sub { MergedQueue->new });

        method BUILD {
                $self->merged_queue->wanted_length( $self->algorithm->queue_count );
        }

        method fill_merged_queue()
        {
                my $count_missing_jobs = $self->merged_queue->wanted_length - $self->merged_queue->length;
                my %queues;
                my $queue_rs = model('TestrunDB')->resultset('Queue');
                foreach my $queue_name( map {$_->name} $queue_rs->all ) {
                        $queues{$queue_name} = 1;
                }

                # fill up to wanted merged_queue length, only accept shorter merged_queue if no queue has jobs to offer
                while($count_missing_jobs > 0)
                {
                        my $queue;
                        do {
                                $queue = $self->algorithm->get_next_queue();
                        } while ($queue and not $queue->active);
                        my $testrun_rs = $queue->queued_testruns;
                        my $job   = $testrun_rs->first;
                        if ($job) {
                                $count_missing_jobs--;
                                $self->merged_queue->add($job);
                        } else {
                                delete $queues{$queue->name};
                        }
                        last if not %queues;
                }
        }

        method adapt_merged_queue_length(Any $job, ArrayRef $free_hosts) {
                if (not $job) {
                        # increase merged_queue length if no job found,
                        if ($self->merged_queue->wanted_length - $self->merged_queue->length < 1) # not longer than current length + 1
                        {
                                my $lookup_queue = $self->algorithm->lookup_next_queue();
                                my $queuehosts   = $lookup_queue->queuehosts;

                                if ( grep {$_->{host}->queuehosts->count == 0 } @$free_hosts   ) { # increase if at least one host is free for all queues
                                        $self->merged_queue->wanted_length( $self->merged_queue->wanted_length + 1 );

                                } elsif ($queuehosts->count) {                                     # increase if at least one host is bound to next queue
                                        my @queuehosts = map {$_->host->name}         $queuehosts->all;
                                        my @free_hosts = map {$_->{host}->name} @$free_hosts;
                                        if (any(@queuehosts) eq any(@free_hosts)) {
                                                $self->merged_queue->wanted_length( $self->merged_queue->wanted_length + 1 );
                                        }
                                } else {
                                        $self->algorithm->get_next_queue;
                                }
                        }
                } else {
                        # count down merged_queue again on success,
                        # but not smaller that count queues
                        $self->merged_queue->wanted_length( $self->merged_queue->wanted_length - 1 )
                            if $self->merged_queue->wanted_length > $self->algorithm->queue_count;
                }
        }

        method overfill_merged_queue()
        {
                my $queue_rs = model('TestrunDB')->resultset('Queue');
                my %queues;
                foreach my $queue( $queue_rs->all ) {
                        $queues{$queue->name} = $queue;
                }

                my $jobs = $self->merged_queue->get_testrequests;
                foreach my $tr( $jobs->all() ) {
                        delete($queues{$tr->queue->name}) if defined $queues{$tr->queue->name};
                }

                foreach my $queue_name (keys %queues) {
                        my $queue      = $queues{$queue_name};
                        my $testrun_rs = $queue->queued_testruns;
                        my $job        = $testrun_rs->first;
                        $self->merged_queue->add($job) if $job;
                }
        }


        method get_next_job(Any %args) {
                my ($queue, $job);

                do {{

                        $self->fill_merged_queue;
                        my $free_hosts = Artemis::Model::free_hosts_with_features();
                        return if not ($free_hosts and @$free_hosts);
                        $job = $self->merged_queue->get_first_fitting($free_hosts);
                        $self->overfill_merged_queue() if not $job;
{'fraction_non_alphanumeric': 0.10628106642566651, 'fraction_numerical': 0.002349751468594668, 'mean_word_length': 1.0918714555765596, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 176, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 54, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2351768', 'n_tokens_mistral': 2843, 'n_tokens_neox': 2552, 'n_words': 562}