-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit3.pas
More file actions
381 lines (342 loc) · 14.3 KB
/
Copy pathunit3.pas
File metadata and controls
381 lines (342 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
{------------------------------------------------------------------------------
pg_timetable_gui
Copyright © 2026 John Buoro, Harvey Norman Holdings Limited. All Rights Reserved.
------------------------------------------------------------------------------}
unit Unit3;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, DBCtrls,
StdCtrls, ComCtrls, Buttons, PQConnection, SQLDB, RegExpr, lclintf,
SynHighlighterSQL, SynEditMarkupHighAll, SynEditTypes, SynEdit;
type
{ TForm3 }
TForm3 = class(TForm)
BitBtnClose: TBitBtn;
BitBtnSave: TBitBtn;
CheckBoxSelfDestruct: TCheckBox;
CheckBoxLive: TCheckBox;
CheckBoxExclusive: TCheckBox;
CronHelpLabel1: TLabel;
edCron: TEdit;
Label1: TLabel;
Label2: TLabel;
CronHelpLabel2: TLabel;
LabeledEditChainName: TLabeledEdit;
LabeledEditChainID: TLabeledEdit;
LabeledEditClient: TLabeledEdit;
LabeledEditMaxInst: TLabeledEdit;
LabeledEditTimeout: TLabeledEdit;
lblDayMonth: TLabel;
lblHour: TLabel;
lblMinute: TLabel;
lblMonth: TLabel;
lblNextRuns: TLabel;
lblWeekday: TLabel;
mmRuns: TMemo;
StatusBar1: TStatusBar;
SynEditOnError: TSynEdit;
SynSQLSyn1: TSynSQLSyn;
procedure BitBtnCloseClick(Sender: TObject);
procedure CronHelpLabel2Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SaveChain(Sender: TObject);
procedure ShowCronHelp(Sender: TObject);
procedure UpdateCronTimes(Sender: TObject);
function IsCronValueValid(const S: string): boolean;
function SelectSQL(const sql: string; params: array of string; out Output: string): boolean;
private
public
chain_id : integer;
end;
var
Form3: TForm3;
MarkCaretSynEditOnError : TSynEditMarkupHighlightAllCaret;
IsCronHelp : boolean = False;
implementation
uses Unit1;
{$R *.lfm}
{ TForm3 }
procedure TForm3.FormCreate(Sender: TObject);
begin
{Set editor style}
MarkCaretSynEditOnError := TSynEditMarkupHighlightAllCaret(SynEditOnError.MarkupByClass[TSynEditMarkupHighlightAllCaret]);
MarkCaretSynEditOnError.MarkupInfo.Background := clYellow;
MarkCaretSynEditOnError.WaitTime := 10;
MarkCaretSynEditOnError.SearchOptions := [ssoWholeWord, ssoSelectedOnly, ssoMatchCase];
{Adjust font colour for certain objects to cope with potential theme changes}
Form3.CronHelpLabel1.Font.Color := Form1.GetContrastColour(clForm, Form3.CronHelpLabel1.Font.Color);
Form3.CronHelpLabel2.Font.Color := Form1.GetContrastColour(clForm, Form3.CronHelpLabel2.Font.Color);
end;
procedure TForm3.FormShow(Sender: TObject);
begin
SynEditOnError.Lines.TextLineBreakStyle := tlbsLF;
{If new chain}
if chain_id = -1 then
begin
LabeledEditChainID.Text := 'NEW';
LabeledEditChainName.Text := '';
CheckBoxLive.Checked := False;
edCron.Text := '* * * * *';
LabeledEditClient.Text := '';
LabeledEditMaxInst.Text := '1';
CheckBoxSelfDestruct.Checked := False;
CheckBoxExclusive.Checked := False;
LabeledEditTimeout.Text := '0';
Exit;
end;
{Modifying instead}
{Update fields directly from grid}
LabeledEditChainID.Text := Form1.DataSourceChains.DataSet.FieldByName('Chain ID').AsString;
LabeledEditChainName.Text := Form1.DataSourceChains.DataSet.FieldByName('Chain Name').AsString;
CheckBoxLive.Checked := Form1.DataSourceChains.DataSet.FieldByName('Live').AsBoolean;
edCron.Text := Form1.DataSourceChains.DataSet.FieldByName('Run At').AsString;
LabeledEditClient.Text := Form1.DataSourceChains.DataSet.FieldByName('Client').AsString;
LabeledEditMaxInst.Text := Form1.DataSourceChains.DataSet.FieldByName('Max Instances').AsString;
CheckBoxExclusive.Checked := Form1.DataSourceChains.DataSet.FieldByName('Exclusive').AsBoolean;
CheckBoxSelfDestruct.Checked := Form1.DataSourceChains.DataSet.FieldByName('Self Destruct').AsBoolean;
LabeledEditTimeout.Text := Form1.DataSourceChains.DataSet.FieldByName('Timeout (ms)').AsString;
{Get other fields directly via SQL}
try
Form1.SQLQueryAdhoc.SQL.Clear;
Form1.SQLQueryAdhoc.SQL.Add('SELECT on_error FROM timetable.chain WHERE chain_id = ' + IntToStr(chain_id));
Form1.SQLQueryAdhoc.ParamCheck := False;
Form1.SQLQueryAdhoc.Open;
SynEditOnError.Lines.Text := Form1.SQLQueryAdhoc.FieldByName('on_error').AsString;
finally
Form1.SQLQueryAdhoc.Close;
end;
UpdateCronTimes(Sender);
end;
procedure TForm3.SaveChain(Sender: TObject);
var
sql : string;
i : integer;
task_id : integer;
begin
{Insert chain directly via SQL}
if chain_id = -1 then
begin
{Test to see if chain name already exists}
try
Form1.SQLQueryAdhoc.Close;
Form1.SQLQueryAdhoc.SQL.Clear;
Form1.SQLQueryAdhoc.ParamCheck := true;
sql := 'SELECT count(1) as cnt FROM timetable.chain WHERE chain_name ILIKE :chain_name';
Form1.SQLQueryAdhoc.SQL.Add(sql);
Form1.SQLQueryAdhoc.ParamByName('chain_name').AsString := LabeledEditChainName.Text;
Form1.SQLQueryAdhoc.Open;
i := Form1.SQLQueryAdhoc.FieldByName('cnt').AsInteger;
finally
Form1.SQLQueryAdhoc.Close;
end;
{showmessage('cnt = ' + inttostr(i));}
if i > 0 then
begin
MessageDlg('Insert Chain...', 'Cannot add this chain because the Chain Name [' + LabeledEditChainName.Text + '] already exsits.', mtWarning, [mbOk], 0);
Exit;
end
else
try
{Insert the chain}
Form1.SQLQueryAdhoc.Close;
Form1.SQLQueryAdhoc.SQL.Clear;
Form1.SQLQueryAdhoc.ParamCheck := true;
sql := 'INSERT INTO timetable.chain(chain_name, run_at, max_instances, timeout, live, self_destruct, exclusive_execution, client_name, on_error) VALUES (:chain_name, :run_at, :max_instances, :timeout, :live, :self_destruct, :exclusive_execution, :client_name, :on_error) RETURNING chain_id;';
Form1.SQLQueryAdhoc.SQL.Add(sql);
Form1.SQLQueryAdhoc.ParamByName('chain_name').AsString := LabeledEditChainName.Text;
Form1.SQLQueryAdhoc.ParamByName('live').AsBoolean := CheckBoxLive.Checked;
Form1.SQLQueryAdhoc.ParamByName('run_at').AsString := edCron.Text;
Form1.SQLQueryAdhoc.ParamByName('exclusive_execution').AsBoolean := CheckBoxExclusive.Checked;
Form1.SQLQueryAdhoc.ParamByName('self_destruct').AsBoolean := CheckBoxSelfDestruct.Checked;
Form1.SQLQueryAdhoc.ParamByName('timeout').AsInteger := StrToIntDef(LabeledEditTimeout.Text, 0);
if Length(Trim(LabeledEditClient.Text)) = 0 then
Form1.SQLQueryAdhoc.ParamByName('client_name').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('client_name').AsString := LabeledEditClient.Text;
if Length(Trim(SynEditOnError.Lines.Text)) = 0 then
Form1.SQLQueryAdhoc.ParamByName('on_error').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('on_error').AsString := SynEditOnError.Lines.Text;
if LabeledEditMaxInst.Text = '' then
Form1.SQLQueryAdhoc.ParamByName('max_instances').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('max_instances').AsInteger := StrToInt(LabeledEditMaxInst.Text);
Form1.SQLQueryAdhoc.Open;
chain_id := Form1.SQLQueryAdhoc.FieldByName('chain_id').AsInteger;
LabeledEditChainID.Text := IntToStr(chain_id);
{showmessage('chain_id = ' + IntToStr(chain_id));}
{Insert a default task}
Form1.SQLQueryAdhoc.Close;
Form1.SQLQueryAdhoc.SQL.Clear;
Form1.SQLQueryAdhoc.ParamCheck := true;
sql := 'INSERT INTO timetable.task (chain_id, task_order, task_name, kind, command) VALUES (:chain_id, 10, ''No Op'', ''BUILTIN'', ''NoOp'') RETURNING task_id;';
Form1.SQLQueryAdhoc.SQL.Add(sql);
Form1.SQLQueryAdhoc.ParamByName('chain_id').AsInteger := chain_id;
Form1.SQLQueryAdhoc.Open;
task_id := Form1.SQLQueryAdhoc.FieldByName('task_id').AsInteger;
{showmessage('task_id = ' + IntToStr(task_id));}
finally
Form1.SQLQueryAdhoc.Close;
StatusBar1.SimpleText := 'Record added.';
Close;
end;
end
{Update chain directly via SQL}
else if chain_id <> -1 then
begin
try
Form1.SQLQueryAdhoc.Close;
Form1.SQLQueryAdhoc.SQL.Clear;
Form1.SQLQueryAdhoc.ParamCheck := true;
sql := 'UPDATE timetable.chain SET chain_name = :chain_name, run_at = :run_at, max_instances = :max_instances, timeout = :timeout, live = :live, self_destruct = :self_destruct, exclusive_execution = :exclusive_execution, client_name = :client_name, on_error = :on_error WHERE chain_id = ' + IntToStr(chain_id);
Form1.SQLQueryAdhoc.SQL.Add(sql);
Form1.SQLQueryAdhoc.ParamByName('chain_name').AsString := LabeledEditChainName.Text;
Form1.SQLQueryAdhoc.ParamByName('live').AsBoolean := CheckBoxLive.Checked;
Form1.SQLQueryAdhoc.ParamByName('run_at').AsString := edCron.Text;
Form1.SQLQueryAdhoc.ParamByName('exclusive_execution').AsBoolean := CheckBoxExclusive.Checked;
Form1.SQLQueryAdhoc.ParamByName('self_destruct').AsBoolean := CheckBoxSelfDestruct.Checked;
Form1.SQLQueryAdhoc.ParamByName('timeout').AsInteger := StrToIntDef(LabeledEditTimeout.Text, 0);
if Length(Trim(LabeledEditClient.Text)) = 0 then
Form1.SQLQueryAdhoc.ParamByName('client_name').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('client_name').AsString := LabeledEditClient.Text;
if Length(Trim(SynEditOnError.Lines.Text)) = 0 then
Form1.SQLQueryAdhoc.ParamByName('on_error').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('on_error').AsString := SynEditOnError.Lines.Text;
if LabeledEditMaxInst.Text = '' then
Form1.SQLQueryAdhoc.ParamByName('max_instances').Value := null
else
Form1.SQLQueryAdhoc.ParamByName('max_instances').AsInteger := StrToInt(LabeledEditMaxInst.Text);
Form1.SQLQueryAdhoc.ExecSQL;
finally
Form1.SQLQueryAdhoc.Close;
StatusBar1.SimpleText := 'Record updated.';
Close;
end;
end;
end;
procedure TForm3.ShowCronHelp(Sender: TObject);
begin
if not IsCronHelp then
begin
mmRuns.Clear;
mmRuns.Append('CRON HELP:');
mmRuns.Append('');
mmRuns.Append('Example: Daily at 04:45:');
mmRuns.Append('45 4 * * *');
mmRuns.Append('Example: Every 5 minutes:');
mmRuns.Append('*/5 * * * *');
mmRuns.Append('@every 5 minutes');
mmRuns.Append('Example: After 1 hour:');
mmRuns.Append('@after 1 hour');
mmRuns.Append('Example: Execute after pg_timetable restarts:');
mmRuns.Append('@reboot');
IsCronHelp := True;
end
else
begin
IsCronHelp := False;
UpdateCronTimes(Sender);
end;
end;
procedure TForm3.BitBtnCloseClick(Sender: TObject);
begin
Close;
end;
procedure TForm3.CronHelpLabel2Click(Sender: TObject);
begin
OpenURL('https://crontab.guru');
end;
{Code taken from https://github.com/cybertec-postgresql/pg_timetable_gui (Pavlo Golub)}
procedure TForm3.UpdateCronTimes(Sender: TObject);
var
ValidCron: boolean;
S: string;
Output: string;
const
cronRE = '^(((\d+,)+\d+|(\d+(\/|-)\d+)|(\*(\/|-)\d+)|\d+|\*) +){4}'+
'(((\d+,)+\d+|(\d+(\/|-)\d+)|(\*(\/|-)\d+)|\d+|\*) ?)$';
sqlCronRuns = 'SELECT to_char(r.r, ''FMDay, FMDD FMMon YYYY at HH24:MI:SS'') FROM '+
'generate_series(now(), now() + 10 * :cron :: interval, :cron :: interval) AS r(r) LIMIT 10';
sqlIntervalRuns = 'SELECT to_char(r.r, ''FMDay, FMDD FMMon YYYY at HH24:MI:SS'') FROM '+
'timetable.cron_runs(now(), :cron) AS r(r) LIMIT 10';
minIntervalLen = length('@every '); // @ modifier and at least one space char
begin
S := edCron.Text;
ValidCron := S.Trim() = '@reboot';
if not ValidCron then
if S.StartsWith('@every ') or S.StartsWith('@after ') then //special values
ValidCron := (S.Length > minIntervalLen)
and IsCronValueValid(S)
and SelectSQL(sqlCronRuns, [S.Substring(minIntervalLen)], Output)
else
with TRegExpr.Create(cronRE) do
try
ValidCron := Exec(S) and SelectSQL(sqlIntervalRuns, [S], Output);
finally
Free();
end;
mmRuns.Text := Output;
if ValidCron then edCron.Color := clDefault else edCron.Color := clRed;
end;
{Code taken from https://github.com/cybertec-postgresql/pg_timetable_gui (Pavlo Golub)}
function TForm3.IsCronValueValid(const S: string): boolean;
var
Q: TSQLQuery;
begin
Result := True;
Q := TSQLQuery.Create(nil);
try
Q.DataBase := Form1.PQConnection1;
Q.SQL.Text := 'SELECT CAST(:cron AS timetable.cron)';
Q.ParamByName('cron').AsString := S;
try
Q.Open;
except
Exit(False);
end;
Q.Close;
finally
FreeAndNil(Q);
end;
end;
{Code taken from https://github.com/cybertec-postgresql/pg_timetable_gui (Pavlo Golub)}
function TForm3.SelectSQL(const sql: string; params: array of string; out Output: string): boolean;
var
Q: TSQLQuery;
i: Integer;
begin
Result := True;
Output := '';
Q := TSQLQuery.Create(nil);
try
Q.DataBase := Form1.PQConnection1;
Q.SQL.Text := sql;
for i := Low(params) to High(params) do
Q.Params[i].AsString := params[i];
try
Q.Open;
while not Q.EOF do
begin
Output := Output + Q.Fields[0].AsString + LineEnding;
Q.Next;
end;
except
on E: Exception do
begin
Result := False;
if E is EPQDatabaseError then
Output := EPQDatabaseError(E).MESSAGE_PRIMARY
else
Output := E.Message;
end;
end;
Q.Close;
finally
FreeAndNil(Q);
end;
end;
end.