2017-12-20 14:39:09 +01:00
|
|
|
/* === This file is part of Calamares - <https://github.com/calamares> ===
|
2014-06-27 15:34:05 +02:00
|
|
|
*
|
2015-03-06 18:55:02 +01:00
|
|
|
* Copyright 2014-2015, Teo Mrnjavac <teo@kde.org>
|
2018-06-15 11:59:11 +02:00
|
|
|
* Copyright 2018, Adriaan de Groot <groot@kde.org>
|
2014-06-27 15:34:05 +02:00
|
|
|
*
|
|
|
|
* Calamares is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calamares is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calamares. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "JobQueue.h"
|
2014-07-08 17:04:39 +02:00
|
|
|
|
2020-02-12 10:55:36 +01:00
|
|
|
#include "CalamaresConfig.h"
|
2014-07-21 17:08:06 +02:00
|
|
|
#include "GlobalStorage.h"
|
2019-08-04 22:24:55 +02:00
|
|
|
#include "Job.h"
|
2014-07-23 12:04:27 +02:00
|
|
|
#include "utils/Logger.h"
|
2014-07-08 17:04:39 +02:00
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
|
2014-06-27 15:34:05 +02:00
|
|
|
namespace Calamares
|
|
|
|
{
|
|
|
|
|
2014-07-08 17:04:39 +02:00
|
|
|
class JobThread : public QThread
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
JobThread( JobQueue* queue )
|
2015-06-13 21:31:06 +02:00
|
|
|
: QThread( queue )
|
|
|
|
, m_queue( queue )
|
|
|
|
, m_jobIndex( 0 )
|
2014-07-08 17:04:39 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-04-18 12:16:27 +02:00
|
|
|
virtual ~JobThread() override;
|
2019-06-07 11:46:08 +02:00
|
|
|
|
2019-06-15 17:42:47 +02:00
|
|
|
void setJobs( JobList&& jobs )
|
2014-07-08 17:04:39 +02:00
|
|
|
{
|
|
|
|
m_jobs = jobs;
|
2019-06-28 19:32:35 +02:00
|
|
|
|
|
|
|
qreal totalJobsWeight = 0.0;
|
2019-08-04 22:24:55 +02:00
|
|
|
for ( auto job : m_jobs )
|
2019-06-28 19:32:35 +02:00
|
|
|
{
|
|
|
|
totalJobsWeight += job->getJobWeight();
|
|
|
|
}
|
2019-08-04 22:24:55 +02:00
|
|
|
for ( auto job : m_jobs )
|
2019-06-28 19:32:35 +02:00
|
|
|
{
|
|
|
|
qreal jobWeight = qreal( job->getJobWeight() / totalJobsWeight );
|
2019-08-04 22:24:55 +02:00
|
|
|
m_jobWeights.append( jobWeight );
|
2019-06-28 19:32:35 +02:00
|
|
|
}
|
2014-07-08 17:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void run() override
|
|
|
|
{
|
2018-06-15 17:45:02 +02:00
|
|
|
bool anyFailed = false;
|
|
|
|
QString message;
|
|
|
|
QString details;
|
|
|
|
|
2014-07-23 12:04:27 +02:00
|
|
|
m_jobIndex = 0;
|
2019-08-04 22:24:55 +02:00
|
|
|
for ( auto job : m_jobs )
|
2014-07-08 17:04:39 +02:00
|
|
|
{
|
2018-06-15 17:45:02 +02:00
|
|
|
if ( anyFailed && !job->isEmergency() )
|
|
|
|
{
|
|
|
|
cDebug() << "Skipping non-emergency job" << job->prettyName();
|
2020-03-24 15:06:34 +01:00
|
|
|
++m_jobIndex;
|
2018-06-15 17:45:02 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-07-23 12:04:27 +02:00
|
|
|
emitProgress();
|
2020-03-16 17:25:27 +01:00
|
|
|
cDebug() << "Starting" << ( anyFailed ? "EMERGENCY JOB" : "job" ) << job->prettyName() << " (there are"
|
|
|
|
<< m_jobs.count() << " left)";
|
2014-07-23 12:04:27 +02:00
|
|
|
connect( job.data(), &Job::progress, this, &JobThread::emitProgress );
|
2014-07-10 14:46:08 +02:00
|
|
|
JobResult result = job->exec();
|
2018-06-15 17:45:02 +02:00
|
|
|
if ( !anyFailed && !result )
|
2014-07-10 14:46:08 +02:00
|
|
|
{
|
2018-06-15 17:45:02 +02:00
|
|
|
anyFailed = true;
|
|
|
|
message = result.message();
|
|
|
|
details = result.details();
|
2014-07-10 14:46:08 +02:00
|
|
|
}
|
2020-03-24 15:06:34 +01:00
|
|
|
emitProgress( 1.0 );
|
|
|
|
++m_jobIndex;
|
2014-07-08 17:04:39 +02:00
|
|
|
}
|
2018-06-15 17:45:02 +02:00
|
|
|
if ( anyFailed )
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2018-06-15 17:45:02 +02:00
|
|
|
emitFailed( message, details );
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2018-06-15 17:45:02 +02:00
|
|
|
else
|
2019-08-04 22:24:55 +02:00
|
|
|
{
|
2018-06-15 17:45:02 +02:00
|
|
|
emitProgress();
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2014-07-23 10:58:08 +02:00
|
|
|
emitFinished();
|
2014-07-08 17:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-11-03 15:10:37 +01:00
|
|
|
JobList m_jobs;
|
2019-06-28 19:32:35 +02:00
|
|
|
QList< qreal > m_jobWeights;
|
2014-07-08 17:04:39 +02:00
|
|
|
JobQueue* m_queue;
|
2014-07-23 12:04:27 +02:00
|
|
|
int m_jobIndex;
|
2014-07-08 17:04:39 +02:00
|
|
|
|
2014-07-23 12:04:27 +02:00
|
|
|
void emitProgress( qreal jobPercent = 0 )
|
2014-07-08 17:04:39 +02:00
|
|
|
{
|
2014-07-23 12:04:27 +02:00
|
|
|
// Make sure jobPercent is reasonable, in case a job messed up its
|
|
|
|
// percentage computations.
|
|
|
|
jobPercent = qBound( qreal( 0 ), jobPercent, qreal( 1 ) );
|
|
|
|
|
|
|
|
int jobCount = m_jobs.size();
|
2019-08-04 22:24:55 +02:00
|
|
|
QString message = m_jobIndex < jobCount ? m_jobs.at( m_jobIndex )->prettyStatusMessage() : tr( "Done" );
|
2014-07-23 12:04:27 +02:00
|
|
|
|
2020-03-16 17:25:27 +01:00
|
|
|
qreal percent = 1.0; // Pretend we're done, since the if will reset it
|
2019-08-04 22:24:55 +02:00
|
|
|
if ( m_jobIndex < jobCount )
|
2019-06-28 19:32:35 +02:00
|
|
|
{
|
2020-03-16 17:25:27 +01:00
|
|
|
qreal cumulativeProgress = 0.0;
|
|
|
|
for ( auto jobWeight : m_jobWeights.mid( 0, m_jobIndex ) )
|
|
|
|
{
|
|
|
|
cumulativeProgress += jobWeight;
|
|
|
|
}
|
|
|
|
percent = cumulativeProgress + ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent );
|
|
|
|
|
|
|
|
Logger::CDebug( Logger::LOGVERBOSE )
|
|
|
|
<< "[JOBQUEUE]: Progress for Job[" << m_jobIndex << "]: " << ( jobPercent * 100 ) << "% completed";
|
|
|
|
Logger::CDebug( Logger::LOGVERBOSE )
|
|
|
|
<< "[JOBQUEUE]: Progress Overall: " << ( cumulativeProgress * 100 ) << "% (accumulated) + "
|
|
|
|
<< ( ( ( m_jobWeights.at( m_jobIndex ) ) * jobPercent ) * 100 )
|
|
|
|
<< "% (this job) = " << ( percent * 100 ) << "% (total)";
|
2019-06-28 19:32:35 +02:00
|
|
|
}
|
2019-08-04 22:24:55 +02:00
|
|
|
QMetaObject::invokeMethod(
|
|
|
|
m_queue, "progress", Qt::QueuedConnection, Q_ARG( qreal, percent ), Q_ARG( QString, message ) );
|
2014-07-08 17:04:39 +02:00
|
|
|
}
|
2014-07-10 14:46:08 +02:00
|
|
|
|
|
|
|
void emitFailed( const QString& message, const QString& details )
|
|
|
|
{
|
2019-08-04 22:24:55 +02:00
|
|
|
QMetaObject::invokeMethod(
|
|
|
|
m_queue, "failed", Qt::QueuedConnection, Q_ARG( QString, message ), Q_ARG( QString, details ) );
|
2014-07-10 14:46:08 +02:00
|
|
|
}
|
2014-07-23 10:58:08 +02:00
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
void emitFinished() { QMetaObject::invokeMethod( m_queue, "finished", Qt::QueuedConnection ); }
|
2014-07-08 17:04:39 +02:00
|
|
|
};
|
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
JobThread::~JobThread() {}
|
2019-04-18 12:16:27 +02:00
|
|
|
|
2014-07-03 18:00:40 +02:00
|
|
|
|
2014-06-27 16:21:45 +02:00
|
|
|
JobQueue* JobQueue::s_instance = nullptr;
|
|
|
|
|
2014-07-03 18:00:40 +02:00
|
|
|
|
2014-06-27 16:21:45 +02:00
|
|
|
JobQueue*
|
|
|
|
JobQueue::instance()
|
|
|
|
{
|
|
|
|
return s_instance;
|
|
|
|
}
|
|
|
|
|
2014-07-03 18:00:40 +02:00
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
GlobalStorage*
|
|
|
|
JobQueue::globalStorage() const
|
|
|
|
{
|
|
|
|
return m_storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-27 15:34:05 +02:00
|
|
|
JobQueue::JobQueue( QObject* parent )
|
|
|
|
: QObject( parent )
|
2014-07-08 17:04:39 +02:00
|
|
|
, m_thread( new JobThread( this ) )
|
2014-07-21 17:08:06 +02:00
|
|
|
, m_storage( new GlobalStorage() )
|
2014-06-27 15:34:05 +02:00
|
|
|
{
|
2014-07-08 17:04:39 +02:00
|
|
|
Q_ASSERT( !s_instance );
|
|
|
|
s_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
JobQueue::~JobQueue()
|
|
|
|
{
|
2019-06-07 11:46:08 +02:00
|
|
|
if ( m_thread->isRunning() )
|
|
|
|
{
|
|
|
|
m_thread->terminate();
|
2019-08-04 22:24:55 +02:00
|
|
|
if ( !m_thread->wait( 300 ) )
|
|
|
|
{
|
2019-06-07 11:46:08 +02:00
|
|
|
cError() << "Could not terminate job thread (expect a crash now).";
|
2019-08-04 22:24:55 +02:00
|
|
|
}
|
2019-06-07 11:46:08 +02:00
|
|
|
delete m_thread;
|
|
|
|
}
|
|
|
|
|
2014-07-21 17:08:06 +02:00
|
|
|
delete m_storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-08 17:04:39 +02:00
|
|
|
void
|
|
|
|
JobQueue::start()
|
|
|
|
{
|
|
|
|
Q_ASSERT( !m_thread->isRunning() );
|
2019-06-15 17:42:47 +02:00
|
|
|
m_thread->setJobs( std::move( m_jobs ) );
|
2014-07-17 17:57:55 +02:00
|
|
|
m_jobs.clear();
|
2014-07-08 17:04:39 +02:00
|
|
|
m_thread->start();
|
2014-06-27 15:34:05 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 18:00:40 +02:00
|
|
|
|
2014-06-30 18:02:19 +02:00
|
|
|
void
|
2015-09-09 18:59:31 +02:00
|
|
|
JobQueue::enqueue( const job_ptr& job )
|
2014-06-30 18:02:19 +02:00
|
|
|
{
|
2014-07-08 17:04:39 +02:00
|
|
|
Q_ASSERT( !m_thread->isRunning() );
|
|
|
|
m_jobs.append( job );
|
2015-03-06 18:55:02 +01:00
|
|
|
emit queueChanged( m_jobs );
|
2014-06-30 18:02:19 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 18:00:40 +02:00
|
|
|
|
|
|
|
void
|
2017-11-03 15:10:37 +01:00
|
|
|
JobQueue::enqueue( const JobList& jobs )
|
2014-07-03 18:00:40 +02:00
|
|
|
{
|
2014-07-08 17:04:39 +02:00
|
|
|
Q_ASSERT( !m_thread->isRunning() );
|
|
|
|
m_jobs.append( jobs );
|
2015-03-06 18:55:02 +01:00
|
|
|
emit queueChanged( m_jobs );
|
2014-07-03 18:00:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-04 22:24:55 +02:00
|
|
|
} // namespace Calamares
|