Kstars

greedyscheduler.h
1 /* Ekos Scheduler Greedy Algorithm
2  SPDX-FileCopyrightText: Hy Murveit <[email protected]>
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 <QString>
13 #include <QVector>
14 #include "schedulerjob.h"
15 
16 namespace Ekos
17 {
18 
19 class Scheduler;
20 
21 class GreedyScheduler : public QObject
22 {
23  Q_OBJECT
24 
25  public:
26 
27  // Result of a scheduling operation. Mostly useful for testing
28  // or logging, as the true schedule is stored in the Qlist<SchedulerJob*>
29  // returned by scheduleJobs().
30  struct JobSchedule
31  {
32  SchedulerJob *job;
33  QDateTime startTime;
34  QDateTime stopTime;
35  QString stopReason;
36  JobSchedule(SchedulerJob *j, const QDateTime &start, const QDateTime &stop, const QString &r = "")
37  : job(j), startTime(start), stopTime(stop), stopReason(r) {}
38  };
39 
40  GreedyScheduler();
41  /**
42  * @brief setParams Sets parameters, usually stored as KStars Options to the scheduler.
43  * @param restartImmediately Aborted jobs should attempt to be restarted right after they were suspended.
44  * @param restartQueue Aborted jobs should attempt to be restarted after a delay, given below.
45  * @param rescheduleErrors Jobs that failed because of errors should be restarted after a delay.
46  * @param abortDelay The minimum delay (seconds) for restarting aborted jobs.
47  * @param errorHandlingDelay The minimum delay (seconds) for restarting jobs that failed with errors.
48  */
49  void setParams(bool restartImmediately, bool restartQueue,
50  bool rescheduleErrors, int abortDelay,
51  int errorHandlingDelay);
52  /**
53  * @brief scheduleJobs Computes the schedule for job to be run.
54  * @param jobs A list of SchedulerJobs
55  * @param now The time at which the scheduling should start.
56  * @param capturedFramesCount A structure, computed by the scheduler, which keeps track of previous job progress.
57  * @param scheduler A pointer to the scheduler object, useful for notifying the user. Can be nullptr.
58  * @return returns a possibly sorted list of the same jobs input, but with state and start/end time changes.
59  */
60  QList<SchedulerJob *> scheduleJobs(const QList<SchedulerJob *> &jobs,
61  const QDateTime &now,
62  const QMap<QString, uint16_t> &capturedFramesCount,
63  Scheduler *scheduler);
64  /**
65  * @brief checkJob Checks to see if a job should continue running.
66  * @param jobs A list of SchedulerJobs
67  * @param now The time at which the decision should be made.
68  * @param currentJob The currently running job, which may be continued or aborted.
69  * @return returns true if the job should continue to run.
70  */
71  bool checkJob(const QList<SchedulerJob *> &jobs,
72  const QDateTime &now,
73  SchedulerJob *currentJob);
74  /**
75  * @brief getScheduledJob Returns the first job scheduled. Must be called after scheduleJobs().
76  * @return returns the first job scheduled by scheduleJobs(), or nullptr.
77  */
78  SchedulerJob *getScheduledJob()
79  {
80  return scheduledJob;
81  }
82  /**
83  * @brief getSchedule Returns the QList<JobSchedule> computed by scheduleJobs().
84  * @return returns the previously computed schedule.
85  */
86  const QList<JobSchedule> &getSchedule()
87  {
88  return schedule;
89  }
90  /**
91  * @brief setRescheduleAbortsImmediate sets the rescheduleAbortsImmediate parameter.
92  */
93  void setRescheduleAbortsImmediate(bool value)
94  {
95  rescheduleAbortsImmediate = value;
96  }
97  /**
98  * @brief setRescheduleAbortsQueue sets the rescheduleAbortsQueue parameter.
99  */
100  void setRescheduleAbortsQueue(bool value)
101  {
102  rescheduleAbortsQueue = value;
103  }
104  /**
105  * @brief setRescheduleErrors sets the rescheduleErrors parameter.
106  */
107  void setRescheduleErrors(bool value)
108  {
109  rescheduleErrors = value;
110  }
111  /**
112  * @brief setAbortDelaySeconds sets the abortDelaySeconds parameter.
113  */
114  void setAbortDelaySeconds(int value)
115  {
116  abortDelaySeconds = value;
117  }
118  /**
119  * @brief setErrorDelaySeconds sets the errorDelaySeconds parameter.
120  */
121  void setErrorDelaySeconds(int value)
122  {
123  errorDelaySeconds = value;
124  }
125 
126  // For debugging
127  static void printJobs(const QList<SchedulerJob *> &jobs, const QDateTime &time, const QString &label = "");
128  static void printSchedule(const QList<JobSchedule> &schedule);
129  static QString jobScheduleString(const JobSchedule &jobSchedule);
130 
131  private:
132 
133  // Changes the states of the jons on the list, deciding which ones
134  // can be scheduled by scheduleJobs().
135  QList<SchedulerJob *> prepareJobsForEvaluation(
136  const QList<SchedulerJob *> &jobs, const QDateTime &now,
137  const QMap<QString, uint16_t> &capturedFramesCount, Scheduler *scheduler, bool reestimateJobTime = true);
138 
139  // Removes the EVALUATION state, after eval is done.
140  void unsetEvaluation(const QList<SchedulerJob *> &jobs);
141 
142  // If currentJob is nullptr, this is used to find the next job
143  // to schedule. It returns a pointer to a job in jobs, or nullptr.
144  // If currentJob is a pointer to a job in jobs, then it will return
145  // either currentJob if it shouldn't be interrupted, or a pointer
146  // to a job that should interrupt it.
147  SchedulerJob *selectNextJob(const QList<SchedulerJob *> &jobs,
148  const QDateTime &now,
149  SchedulerJob *currentJob = nullptr,
150  bool fullSchedule = false,
151  QDateTime *when = nullptr,
152  QDateTime *nextInterruption = nullptr,
153  QString *interruptReason = nullptr,
154  const QMap<QString, uint16_t> *capturedFramesCount = nullptr);
155 
156  // Simulate the running of the scheduler from time to endTime.
157  // Used to find which jobs will be run in the future.
158  void simulate(const QList<SchedulerJob *> &jobs, const QDateTime &time,
159  const QDateTime &endTime = QDateTime(),
160  const QMap<QString, uint16_t> *capturedFramesCount = nullptr);
161 
162  // Error/Abort restart parameters.
163  // Defaults don't matter much, will be set by UI.
164  bool rescheduleAbortsImmediate { false };
165  bool rescheduleAbortsQueue { true };
166  bool rescheduleErrors {false};
167  int abortDelaySeconds { 3600 };
168  int errorDelaySeconds { 3600 };
169 
170  // These are values computed by scheduleJobs(), stored, and returned
171  // by getScheduledJob() and getSchedule().
172  SchedulerJob *scheduledJob { nullptr };
173  QList<JobSchedule> schedule;
174 };
175 
176 } // namespace Ekos
Q_OBJECTQ_OBJECT
Ekos is an advanced Astrophotography tool for Linux. It is based on a modular extensible framework to...
Definition: align.cpp:70
KGuiItem stop()
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Fri Aug 12 2022 04:00:54 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.