1 #ifndef TASKMANAGER_H
2 #define TASKMANAGER_H
3 
4 #include "backuptask.h"
5 #include "error.h"
6 #include "persistentmodel/archive.h"
7 #include "persistentmodel/job.h"
8 #include "tarsnaptask.h"
9 
10 #include <QDateTime>
11 #include <QMap>
12 #include <QObject>
13 #include <QQueue>
14 #include <QSharedPointer>
15 #include <QThread>
16 #include <QThreadPool>
17 #include <QUrl>
18 #include <QUuid>
19 
20 /*!
21  * \ingroup background-tasks
22  * \brief The TaskManager is a QObject which manages background tasks.
23  */
24 class TaskManager : public QObject
25 {
26     Q_OBJECT
27 
28 public:
29     //! Constructor.
30     TaskManager();
31     ~TaskManager();
32 
33 public slots:
34     //! Checks if any scheduled jobs need to run now; if so, adds them to
35     //! the queue.  If there are no scheduled jobs, quit the app immediately.
36     void runScheduledJobs();
37     void stopTasks(bool interrupt, bool running, bool queued);
38     //! Load Archives from the PersistentStore.
39     void loadArchives();
40     //! Load Jobs from the PersistentStore.
41     void loadJobs();
42     //! Delete a Job, and potentially all associated Archives.
43     void deleteJob(JobPtr job, bool purgeArchives);
44     //! Load the list of archives belonging to a specific Job (specified
45     //! via Qt's `sender()` function call).
46     void loadJobArchives();
47     //! Emit \ref taskInfo signal with information regarding running and queued
48     //! tasks.
49     void getTaskInfo();
50     //! Add a job to the Jobs list.
51     void addJob(JobPtr job);
52 
53     // Tarsnap tasks
54     //! tarsnap --version
55     void getTarsnapVersion(QString tarsnapPath);
56     //! tarsnap-keygen.  If the key already exists, run --fsck-prune first.
57     void registerMachine(QString user, QString password, QString machine,
58                          QString key, QString tarsnapPath, QString cachePath);
59     //! tarsnap -c -f \<name\>
60     void backupNow(BackupTaskPtr backupTask);
61     //! tarsnap --list-archives -vv
62     void getArchives();
63     //! tarsnap --print-stats -f \<name\>
64     void getArchiveStats(ArchivePtr archive);
65     //! tarsnap --tv -f \<name\>
66     void getArchiveContents(ArchivePtr archive);
67     //! tarsnap -d -f \<name\>
68     void deleteArchives(QList<ArchivePtr> archives);
69     //! tarsnap --print-stats
70     void getOverallStats();
71     //! tarsnap --fsck or --fsck-prune
72     void fsck(bool prune = false);
73     //! tarsnap --nuke
74     void nuke();
75     //! tarsnap -x -f \<name\>, with options.
76     void restoreArchive(ArchivePtr archive, ArchiveRestoreOptions options);
77     void getKeyId(QString key);
78     //! Ensure that the cache directory has been created.
79     void initializeCache();
80     void findMatchingArchives(QString jobPrefix);
81 
82 signals:
83     // Tarsnap task notifications
84     //! Are there no running tasks?
85     void idle(bool status);
86     //! Result of tarsnap --version.
87     void tarsnapVersion(QString versionString);
88     //! Result of tarsnap-keygen.
89     void registerMachineStatus(TaskStatus status, QString reason);
90     //! A list of all Archives.
91     void archiveList(QList<ArchivePtr> archives);
92     void addArchive(ArchivePtr archive);
93     //! Result of tarsnap --print-stats.
94     void overallStats(quint64 sizeTotal, quint64 sizeCompressed,
95                       quint64 sizeUniqueTotal, quint64 sizeUniqueCompressed,
96                       quint64 archiveCount);
97     //! A list of all Jobs.
98     void jobsList(QMap<QString, JobPtr> jobs);
99     //! A status message should be shown to the user.
100     //! \param msg: main text to display.
101     //! \param detail: display this text as a mouse-over tooltip.
102     void message(QString msg, QString detail = "");
103     //! A message that should be shown as a desktop notification (if enabled).
104     void displayNotification(QString message);
105     //! Information about running tasks.
106     //! \param backupTaskRunning: is a backup task currently running?
107     //! \param runningTasks: the number of running tasks.
108     //! \param queuedTasks: the number of queued tasks
109     void taskInfo(bool backupTaskRunning, int runningTasks, int queuedTasks);
110     //! The tarsnap CLI experienced an error.
111     void error(TarsnapError error);
112     void keyId(QString key, int id);
113     void matchingArchives(QList<ArchivePtr> archives);
114 
115 private slots:
116     // post Tarsnap task processing
117     void getTarsnapVersionFinished(QVariant data, int exitCode, QString stdOut,
118                                    QString stdErr);
119     void backupTaskFinished(QVariant data, int exitCode, QString stdOut,
120                             QString stdErr);
121     void backupTaskStarted(QVariant data);
122     void registerMachineFinished(QVariant data, int exitCode, QString stdOut,
123                                  QString stdErr);
124     void getArchiveListFinished(QVariant data, int exitCode, QString stdOut,
125                                 QString stdErr);
126     void getArchiveStatsFinished(QVariant data, int exitCode, QString stdOut,
127                                  QString stdErr);
128     void getArchiveContentsFinished(QVariant data, int exitCode, QString stdOut,
129                                     QString stdErr);
130     void deleteArchivesFinished(QVariant data, int exitCode, QString stdOut,
131                                 QString stdErr);
132     void overallStatsFinished(QVariant data, int exitCode, QString stdOut,
133                               QString stdErr);
134     void fsckFinished(QVariant data, int exitCode, QString stdOut,
135                       QString stdErr);
136     void nukeFinished(QVariant data, int exitCode, QString stdOut,
137                       QString stdErr);
138     void restoreArchiveFinished(QVariant data, int exitCode, QString stdOut,
139                                 QString stdErr);
140     void notifyBackupTaskUpdate(QUuid uuid, const TaskStatus &status);
141     void notifyArchivesDeleted(QList<ArchivePtr> archives, bool done);
142     void getKeyIdFinished(QVariant data, int exitCode, QString stdOut,
143                           QString stdErr);
144 
145     // general task management
146     void queueTask(TarsnapTask *task, bool exclusive = false);
147     void startTask(TarsnapTask *task);
148     void dequeueTask();
149 
150 private:
151     void parseError(QString tarsnapOutput);
152     void parseGlobalStats(QString tarsnapOutput);
153     void parseArchiveStats(QString tarsnapOutput, bool newArchiveOutput,
154                            ArchivePtr archive);
155     QString makeTarsnapCommand(QString cmd);
156     void initTarsnapArgs(QStringList &args);
157 
158     QMap<QUuid, BackupTaskPtr> _backupTaskMap;
159     QMap<QString, ArchivePtr>  _archiveMap;
160     QList<TarsnapTask *>  _runningTasks;
161     QQueue<TarsnapTask *> _taskQueue; // mutually exclusive tasks
162     QThreadPool *         _threadPool;
163     QMap<QString, JobPtr> _jobMap;
164 };
165 
166 #endif // TASKMANAGER_H
167