Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Job.cs
1using System;
3using System.Threading.Tasks;
4using Waher.Content;
5using Waher.Groups;
10using Waher.Things;
14
16{
20 public class Job : JobNode, IGroup
21 {
22 private DateTime? executionTime = null;
23 private Duration? period = null;
24 private Duration? burstInterval = null;
25 private DateTime? expires = null;
26 private int burstCount = 1;
27
31 public Job()
32 {
33 }
34
38 [Header(3, "Scheduled execution time:", 10)]
39 [Page(2, "Job", 0)]
40 [ToolTip(4, "When the job is scheduled to execute.")]
41 [DefaultValueNull]
42 public DateTime? ExecutionTime
43 {
44 get => this.executionTime;
45 set => this.executionTime = value;
46 }
47
51 [Header(5, "Job Period:", 20)]
52 [Page(2, "Job", 0)]
53 [ToolTip(6, "Time between job executions. Defines a repetitive job.")]
54 [DefaultValueNull]
56 {
57 get => this.period;
58 set => this.period = value;
59 }
60
64 [Header(7, "Burst count:", 30)]
65 [Page(2, "Job", 0)]
66 [ToolTip(8, "Number of times the job will be executed each period.")]
67 [DefaultValue(1)]
68 public int BurstCount
69 {
70 get => this.burstCount;
71 set => this.burstCount = value;
72 }
73
77 [Header(9, "Burst Interval:", 40)]
78 [Page(2, "Job", 0)]
79 [ToolTip(10, "Time between job executions in a burst.")]
80 [DefaultValueNull]
82 {
83 get => this.burstInterval;
84 set => this.burstInterval = value;
85 }
86
90 [Header(15, "Expires:", 40)]
91 [Page(2, "Job", 0)]
92 [ToolTip(16, "Repetitive job is not scheduled after this point in time.")]
93 [DefaultValueNull]
94 public DateTime? Expires
95 {
96 get => this.expires;
97 set => this.expires = value;
98 }
99
103 protected override Task NodeUpdated()
104 {
105 return this.NodeUpdated(true);
106 }
107
112 private async Task NodeUpdated(bool ScheduleJob)
113 {
114 await base.NodeUpdated();
115
116 if (ScheduleJob)
117 _ = Task.Run(async () => await JobScheduler.Schedule(this, true));
118 }
119
125 public override Task<string> GetTypeNameAsync(Language Language)
126 {
127 return Language.GetStringAsync(typeof(Job), 2, "Job");
128 }
129
135 public override Task<bool> AcceptsChildAsync(INode Child)
136 {
137 return Task.FromResult(Child is JobTaskNode);
138 }
139
145 public override Task<bool> AcceptsParentAsync(INode Parent)
146 {
147 return Task.FromResult(Parent is Root);
148 }
149
153 public async Task<T[]> FindNodes<T>()
154 where T : INode
155 {
156 ChunkedList<T> Nodes = new ChunkedList<T>();
157 await this.FindNodes(Nodes);
158 return Nodes.ToArray();
159 }
160
165 public async Task FindNodes<T>(ChunkedList<T> Nodes)
166 where T : INode
167 {
168 IEnumerable<INode> Children = await this.ChildNodes;
169 if (Children is null)
170 return;
171
172 foreach (INode Child in Children)
173 {
174 if (Child is T Typed)
175 Nodes.Add(Typed);
176 else if (Child is IGroup Group)
177 await Group.FindNodes(Nodes);
178 }
179 }
180
184 public override Task<IEnumerable<ICommand>> Commands => this.GetCommands();
185
186 private async Task<IEnumerable<ICommand>> GetCommands()
187 {
189 {
190 new ExecuteJob(this)
191 };
192
193 Commands.AddRange(await base.Commands);
194
195 return Commands.ToArray();
196 }
197
201 public Task ExecuteJob()
202 {
203 return this.ExecuteJob(null);
204 }
205
211 {
212 return this.ExecuteJob(null, Language, JobReportDetail.None);
213 }
214
222 JobReportDetail ReportDetail)
223 {
225
226 using JobExecutionStatus State = new JobExecutionStatus(this, Query, Language, ReportDetail);
227 JobTaskNode[] Tasks = await this.FindNodes<JobTaskNode>();
228
229 foreach (JobTaskNode Task in Tasks)
230 await Task.ExecuteTask(State);
231 }
232
237 internal async Task ExecutionTimeUpdated(DateTime? ExecutionTime)
238 {
239 this.executionTime = ExecutionTime;
240 await this.NodeUpdated(false);
241 }
242
249 public override async Task<IEnumerable<Parameter>> GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
250 {
251 LinkedList<Parameter> Parameters = (LinkedList<Parameter>)await base.GetDisplayableParametersAsync(Language, Caller);
252
253 if (this.executionTime.HasValue)
254 {
255 Parameters.AddLast(new DateTimeParameter("ExecutionTime",
256 await Language.GetStringAsync(typeof(Job), 11, "Execution Time"),
257 this.executionTime.Value));
258 }
259
260 if (this.period.HasValue)
261 {
262 Parameters.AddLast(new DurationParameter("Period",
263 await Language.GetStringAsync(typeof(Job), 12, "Period"),
264 this.period.Value));
265 }
266
267 if (this.burstInterval.HasValue)
268 {
269 Parameters.AddLast(new DurationParameter("BurstInterval",
270 await Language.GetStringAsync(typeof(Job), 13, "Burst Interval"),
271 this.burstInterval.Value));
272
273 Parameters.AddLast(new Int32Parameter("BurstCount",
274 await Language.GetStringAsync(typeof(Job), 14, "Burst Count"),
275 this.burstCount));
276 }
277
278 if (this.expires.HasValue)
279 {
280 Parameters.AddLast(new DateTimeParameter("Expires",
281 await Language.GetStringAsync(typeof(Job), 17, "Expires"),
282 this.expires.Value));
283 }
284
285 return Parameters;
286 }
287
292 public override Task AddedToRoot(Root Root)
293 {
294 return JobScheduler.Schedule(this, true);
295 }
296
301 public override Task RemovedFromRoot(Root Root)
302 {
303 return JobScheduler.Remove(this);
304 }
305 }
306}
Contains information about the execution of a job.
Base class for all job nodes.
Definition: JobNode.cs:30
INode Parent
Parent Node, or null if a root node.
Definition: JobNode.cs:621
NodeState State
Current overall state of the node.
Definition: JobNode.cs:685
Task< IEnumerable< INode > > ChildNodes
Child nodes. If no child nodes are available, null is returned.
Definition: JobNode.cs:695
Module that manages the scheduling and execution of jobs.
Definition: JobScheduler.cs:18
static async Task< bool > Remove(Job Job)
Removes any scheduled activity for the job.
static async Task Schedule(Job Job, bool OnlyIfDifferent)
Checks the schedule of a job.
Definition: JobScheduler.cs:75
Represents a job.
Definition: Job.cs:21
Duration? Period
Gets or sets the scheduled execution time of the job.
Definition: Job.cs:56
async Task< T[]> FindNodes< T >()
Finds nodes referenced by the group node.
Definition: Job.cs:153
DateTime? Expires
Gets or sets the scheduled execution time of the job.
Definition: Job.cs:95
int BurstCount
Number of executions each period.
Definition: Job.cs:69
Duration? BurstInterval
Gets or sets the scheduled execution time of the job.
Definition: Job.cs:82
override Task< IEnumerable< ICommand > > Commands
Available command objects. If no commands are available, null is returned.
Definition: Job.cs:184
DateTime? ExecutionTime
Gets or sets the scheduled execution time of the job.
Definition: Job.cs:43
Task ExecuteJob(Language Language)
Executes the job.
Definition: Job.cs:210
override Task< bool > AcceptsChildAsync(INode Child)
If the node accepts a presumptive child, i.e. can receive as a child (if that child accepts the node ...
Definition: Job.cs:135
override Task< bool > AcceptsParentAsync(INode Parent)
If the node accepts a presumptive parent, i.e. can be added to that parent (if that parent accepts th...
Definition: Job.cs:145
Job()
Represents a job.
Definition: Job.cs:31
override Task< string > GetTypeNameAsync(Language Language)
Gets the type name of the node.
Definition: Job.cs:125
override Task NodeUpdated()
Persists changes to the node, and generates a node updated event.
Definition: Job.cs:103
override async Task< IEnumerable< Parameter > > GetDisplayableParametersAsync(Language Language, RequestOrigin Caller)
Gets displayable parameters.
Definition: Job.cs:249
override Task RemovedFromRoot(Root Root)
Node has been removed from the root node.
Definition: Job.cs:301
async Task ExecuteJob(Query Query, Language Language, JobReportDetail ReportDetail)
Executes the job.
Definition: Job.cs:221
Task ExecuteJob()
Executes the job.
Definition: Job.cs:201
override Task AddedToRoot(Root Root)
Node has been added to the root node.
Definition: Job.cs:292
Abstract bast class for job tasks.
Definition: JobTaskNode.cs:13
abstract Task ExecuteTask(JobExecutionStatus Status)
Executes the task.
Class for the root node of the Jobs data source.
Definition: Root.cs:11
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
T[] ToArray()
Returns an array containing all elements of the collection.
Contains information about a language.
Definition: Language.cs:17
Task< string > GetStringAsync(Type Type, int Id, string Default)
Gets the string value of a string ID. If no such string exists, a string is created with the default ...
Definition: Language.cs:209
Basic access point for runtime language localization.
Definition: Translator.cs:16
static async Task< Language > GetDefaultLanguageAsync()
Gets the default language.
Definition: Translator.cs:223
Class handling the reception of data from a query.
Definition: Query.cs:12
Tokens available in request.
Definition: RequestOrigin.cs:9
Base Interface for all metering nodes.
Definition: IGroup.cs:11
Interface for nodes that are published through the concentrator interface.
Definition: INode.cs:49
Definition: ImplTypes.g.cs:58
JobReportDetail
How much detail to include in job reports.
Represents a duration value, as defined by the xsd:duration data type: http://www....
Definition: Duration.cs:14