File indexing completed on 2023-05-30 09:06:28

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2011 Dennis Nienhüser <nienhueser@kde.org>
0004 //
0005 
0006 #include "jobqueue.h"
0007 #include "logger.h"
0008 
0009 #include <QDebug>
0010 #include <QThreadPool>
0011 
0012 JobQueue::JobQueue(QObject *parent) :
0013     QObject(parent), m_maxConcurrentJobs(1)
0014 {
0015     // nothing to do
0016 }
0017 
0018 void JobQueue::addJob(Job *newJob)
0019 {
0020     QList<Job*> const allJobs = m_jobs + m_runningJobs;
0021     for(Job* job: allJobs) {
0022         if (*job == *newJob) {
0023             qDebug() << "Ignoring job, still running";
0024             return;
0025         }
0026     }
0027 
0028     connect(newJob, SIGNAL(finished(Job*)), this, SLOT(removeJob(Job*)));
0029 
0030     if (m_runningJobs.size()<m_maxConcurrentJobs) {
0031         startJob(newJob);
0032     } else {
0033         Logger::instance().setStatus(newJob->region().id() + QLatin1Char('_') + newJob->transport(), newJob->region().name() + QLatin1String(" (") + newJob->transport() + QLatin1Char(')'), "waiting", "Queued.");
0034         m_jobs << newJob;
0035     }
0036 }
0037 
0038 void JobQueue::setMaxConcurrentJobs(int size)
0039 {
0040     m_maxConcurrentJobs = size;
0041 }
0042 
0043 void JobQueue::removeJob(Job *job)
0044 {
0045     m_runningJobs.removeAll(job);
0046 
0047     while (m_runningJobs.size()<m_maxConcurrentJobs && !m_jobs.isEmpty()) {
0048         Job* newJob = m_jobs.first();
0049         m_jobs.removeFirst();
0050         startJob(newJob);
0051     }
0052 }
0053 
0054 void JobQueue::startJob(Job *job)
0055 {
0056     m_runningJobs << job;
0057     QThreadPool::globalInstance()->start(job);
0058 }
0059 
0060 #include "moc_jobqueue.cpp"