Kstars

greedyscheduler.h
1/* Ekos Scheduler Greedy Algorithm
2 SPDX-FileCopyrightText: Hy Murveit <hy@murveit.com>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#pragma once
8
9#include <QList>
10#include <QMap>
11#include <QDateTime>
12#include <QObject>
13#include <QString>
14#include <QVector>
15
16namespace Ekos
17{
18
19class Scheduler;
20class SchedulerJob;
21class ModuleLogger;
22
23class GreedyScheduler : public QObject
24{
26
27 public:
28
29 // Result of a scheduling operation. Mostly useful for testing
30 // or logging, as the true schedule is stored in the Qlist<SchedulerJob*>
31 // returned by scheduleJobs().
32 struct JobSchedule
33 {
34 SchedulerJob *job;
35 QDateTime startTime;
36 QDateTime stopTime;
37 QString stopReason;
38 JobSchedule(SchedulerJob *j, const QDateTime &start, const QDateTime &stop, const QString &r = "")
39 : job(j), startTime(start), stopTime(stop), stopReason(r) {}
40 };
41
42 GreedyScheduler();
43 /**
44 * @brief setParams Sets parameters, usually stored as KStars Options to the scheduler.
45 * @param restartImmediately Aborted jobs should attempt to be restarted right after they were suspended.
46 * @param restartQueue Aborted jobs should attempt to be restarted after a delay, given below.
47 * @param rescheduleErrors Jobs that failed because of errors should be restarted after a delay.
48 * @param abortDelay The minimum delay (seconds) for restarting aborted jobs.
49 * @param errorHandlingDelay The minimum delay (seconds) for restarting jobs that failed with errors.
50 */
51 void setParams(bool restartImmediately, bool restartQueue,
52 bool rescheduleErrors, int abortDelay,
53 int errorHandlingDelay);
54 /**
55 * @brief scheduleJobs Computes the schedule for job to be run.
56 * @param jobs A list of SchedulerJobs
57 * @param now The time at which the scheduling should start.
58 * @param capturedFramesCount A structure, computed by the scheduler, which keeps track of previous job progress.
59 * @param scheduler A pointer to the module logging, useful for notifying the user. Can be nullptr.
60 */
61 void scheduleJobs(const QList<SchedulerJob *> &jobs,
62 const QDateTime &now,
63 const QMap<QString, uint16_t> &capturedFramesCount,
64 ModuleLogger *logger);
65 /**
66 * @brief checkJob Checks to see if a job should continue running.
67 * @param jobs A list of SchedulerJobs
68 * @param now The time at which the decision should be made.
69 * @param currentJob The currently running job, which may be continued or aborted.
70 * @return returns true if the job should continue to run.
71 */
72 bool checkJob(const QList<SchedulerJob *> &jobs,
73 const QDateTime &now,
74 const SchedulerJob * const currentJob);
75 /**
76 * @brief getScheduledJob Returns the first job scheduled. Must be called after scheduleJobs().
77 * @return returns the first job scheduled by scheduleJobs(), or nullptr.
78 */
79 SchedulerJob *getScheduledJob() const
80 {
81 return scheduledJob;
82 }
83 /**
84 * @brief getSchedule Returns the QList<JobSchedule> computed by scheduleJobs().
85 * @return returns the previously computed schedule.
86 */
87 const QList<JobSchedule> &getSchedule() const
88 {
89 return schedule;
90 }
91 /**
92 * @brief setRescheduleAbortsImmediate sets the rescheduleAbortsImmediate parameter.
93 */
94 void setRescheduleAbortsImmediate(bool value)
95 {
96 rescheduleAbortsImmediate = value;
97 }
98 /**
99 * @brief setRescheduleAbortsQueue sets the rescheduleAbortsQueue parameter.
100 */
101 void setRescheduleAbortsQueue(bool value)
102 {
103 rescheduleAbortsQueue = value;
104 }
105 /**
106 * @brief setRescheduleErrors sets the rescheduleErrors parameter.
107 */
108 void setRescheduleErrors(bool value)
109 {
110 rescheduleErrors = value;
111 }
112 /**
113 * @brief setAbortDelaySeconds sets the abortDelaySeconds parameter.
114 */
115 void setAbortDelaySeconds(int value)
116 {
117 abortDelaySeconds = value;
118 }
119 /**
120 * @brief setErrorDelaySeconds sets the errorDelaySeconds parameter.
121 */
122 void setErrorDelaySeconds(int value)
123 {
124 errorDelaySeconds = value;
125 }
126
127 // For debugging
128 static void printJobs(const QList<SchedulerJob *> &jobs, const QDateTime &time, const QString &label = "");
129 static void printSchedule(const QList<JobSchedule> &schedule);
130 static QString jobScheduleString(const JobSchedule &jobSchedule);
131
132 private:
133
134 // Changes the states of the jobs on the list, deciding which ones
135 // can be scheduled by scheduleJobs(). This includes setting runnable
136 // jobs to the JOB_EVALUATION state and updating their estimated time.
137 // In addition, jobs with no remaining time are marked JOB_COMPLETED,
138 // jobs with invalid sequence file as JOB_INVALID.
139 void prepareJobsForEvaluation(const QList<SchedulerJob *> &jobs, const QDateTime &now,
140 const QMap<QString, uint16_t> &capturedFramesCount, ModuleLogger *scheduler, bool reestimateJobTime = true) const;
141
142 // Removes the EVALUATION state, after eval is done.
143 void unsetEvaluation(const QList<SchedulerJob *> &jobs) const;
144
145 typedef enum {
146 DONT_SIMULATE = 0,
147 SIMULATE,
148 SIMULATE_EACH_JOB_ONCE
149 } SimulationType;
150
151 // If currentJob is nullptr, this is used to find the next job
152 // to schedule. It returns a pointer to a job in jobs, or nullptr.
153 // If currentJob is a pointer to a job in jobs, then it will return
154 // either currentJob if it shouldn't be interrupted, or a pointer
155 // to a job that should interrupt it. If simType is not DONT_SIMULATE,
156 // {@see #simulate()} is invoked to update the schedule.
157 SchedulerJob *selectNextJob(const QList<SchedulerJob *> &jobs,
158 const QDateTime &now,
159 const SchedulerJob * const currentJob,
160 SimulationType simType,
161 QDateTime *when = nullptr,
162 QDateTime *nextInterruption = nullptr,
163 QString *interruptReason = nullptr,
164 const QMap<QString, uint16_t> *capturedFramesCount = nullptr);
165
166 // Simulate the running of the scheduler from time to endTime by appending
167 // JobSchedule entries to the schedule.
168 // Used to find which jobs will be run in the future.
169 // Returns the end time of the simulation.
170 QDateTime simulate(const QList<SchedulerJob *> &jobs, const QDateTime &time,
171 const QDateTime &endTime,
172 const QMap<QString, uint16_t> *capturedFramesCount,
173 SimulationType simType);
174
175 // Error/Abort restart parameters.
176 // Defaults don't matter much, will be set by UI.
177 bool rescheduleAbortsImmediate { false };
178 bool rescheduleAbortsQueue { true };
179 bool rescheduleErrors {false};
180 int abortDelaySeconds { 3600 };
181 int errorDelaySeconds { 3600 };
182
183 // These are values computed by scheduleJobs(), stored, and returned
184 // by getScheduledJob() and getSchedule().
185 SchedulerJob *scheduledJob { nullptr };
186 QList<JobSchedule> schedule;
187
188 // The amount of time it took to simulate.
189 // If it's quick, we may do it in checkJob to keep the schedule up-to-date.
190 double m_SimSeconds = 0;
191 // The time of the last simulation in checkJob().
192 // We don't simulate too frequently.
193 QDateTime m_LastCheckJobSim;
194};
195
196} // namespace Ekos
Ekos is an advanced Astrophotography tool for Linux.
Definition align.cpp:83
KGuiItem stop()
Q_OBJECTQ_OBJECT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:41 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.