@@ -195,7 +195,7 @@ BEGIN
195
195
' dates' , ' Set of exact dates when comman will be executed' ,
196
196
' use_same_transaction' , ' if set of commans should be executed within the same transaction' ,
197
197
' last_start_available' , ' for how long could command execution be postponed in format of interval type' ,
198
- ' max_run_time' , ' how long task could be executed, NULL - infinite' ,
198
+ ' max_run_time' , ' how long job could be executed, NULL - infinite' ,
199
199
' max_instances' , ' the number of instances run at the same time' ,
200
200
' onrollback' , ' statement to be executed after rollback if one occured' ,
201
201
' next_time_statement' , ' statement to be executed last to calc next execution time'
@@ -241,68 +241,108 @@ LANGUAGE plpgsql;
241
241
CREATE FUNCTION schedule ._get_cron_from_attrs(params jsonb) RETURNS jsonb AS
242
242
$BODY$
243
243
DECLARE
244
- tdates text [];
245
- dates text [];
246
- cron jsonb;
244
+ dates text [];
245
+ cron jsonb;
246
+ clean_cron jsonb;
247
+ N integer ;
248
+ name text ;
247
249
BEGIN
248
250
249
- IF params?' cron' THEN
250
- EXECUTE ' SELECT schedule.cron2jsontext($1::cstring)::jsonb'
251
- INTO cron
252
- USING params- >> ' cron' ;
253
- ELSIF params?' rule' THEN
254
- cron := params- > ' rule' ;
255
- ELSIF NOT params?' date' AND NOT params?' dates' THEN
256
- RAISE EXCEPTION ' There is no information about task ' ' s schedule'
257
- USING HINT = ' Use ' ' cron' ' - cron string, ' ' rule' ' - json to set schedule rules or ' ' date' ' and ' ' dates' ' to set exact date(s)' ;
258
- END IF;
251
+ IF params?' cron' THEN
252
+ EXECUTE ' SELECT schedule.cron2jsontext($1::cstring)::jsonb'
253
+ INTO cron
254
+ USING params- >> ' cron' ;
255
+ ELSIF params?' rule' THEN
256
+ cron := params- > ' rule' ;
257
+ ELSIF NOT params?' date' AND NOT params?' dates' THEN
258
+ RAISE EXCEPTION ' There is no information about job ' ' s schedule'
259
+ USING HINT = ' Use ' ' cron' ' - cron string, ' ' rule' ' - json to set schedule rules or ' ' date' ' and ' ' dates' ' to set exact date(s)' ;
260
+ END IF;
259
261
260
- IF cron IS NOT NULL AND cron?' dates' THEN
261
- EXECUTE ' SELECT array_agg(value)::text[] from jsonb_array_elements_text($1) as X'
262
- INTO tdates
263
- USING cron- > ' dates' ;
264
- ELSE
265
- tdates := ' {}' ::text [];
266
- END IF;
262
+ IF cron IS NOT NULL THEN
263
+ IF cron?' date' THEN
264
+ dates := schedule ._get_array_from_jsonb (dates, cron- > ' date' );
265
+ END IF;
266
+ IF cron?' dates' THEN
267
+ dates := schedule ._get_array_from_jsonb (dates, cron- > ' dates' );
268
+ END IF;
269
+ END IF;
267
270
268
- IF params?' date' THEN
269
- tdates := array_append(tdates, params- >> ' date' );
270
- END IF;
271
+ IF params?' date' THEN
272
+ dates := schedule ._get_array_from_jsonb (dates, params- > ' date' );
273
+ END IF;
274
+ IF params?' dates' THEN
275
+ dates := schedule ._get_array_from_jsonb (dates, params- > ' dates' );
276
+ END IF;
277
+ N := array_length(dates, 1 );
278
+
279
+ IF N > 0 THEN
280
+ EXECUTE ' SELECT array_agg(lll) FROM (SELECT distinct(date_trunc(' ' min' ' , unnest::timestamp with time zone)) as lll FROM unnest($1) ORDER BY date_trunc(' ' min' ' , unnest::timestamp with time zone)) as Z'
281
+ INTO dates USING dates;
282
+ cron := COALESCE(cron, ' {}' ::jsonb) || json_build_object(' dates' , array_to_json(dates))::jsonb;
283
+ END IF;
284
+
285
+ clean_cron := ' {}' ::jsonb;
286
+ FOR name IN SELECT * FROM unnest(' {dates, crontab, onstart, days, hours, wdays, mounths, minutes}' ::text [])
287
+ LOOP
288
+ IF cron?name THEN
289
+ clean_cron := jsonb_set(clean_cron, array_append(' {}' ::text [], name), cron- > name);
290
+ END IF;
291
+ END LOOP;
292
+ RETURN clean_cron;
293
+ END
294
+ $BODY$
295
+ LANGUAGE plpgsql;
271
296
272
- IF params?' dates' THEN
273
- EXECUTE ' SELECT array_agg(value)::text[] from jsonb_array_elements_text($1) as X'
274
- INTO dates
275
- USING params- > ' dates' ;
276
- tdates := array_cat(tdates, dates);
277
- END IF;
297
+ CREATE FUNCTION schedule ._get_array_from_jsonb(dst text [], value jsonb) RETURNS text [] AS
298
+ $BODY$
299
+ DECLARE
300
+ vtype text ;
301
+ BEGIN
302
+ IF value IS NULL THEN
303
+ RETURN dst;
304
+ END IF;
278
305
279
- IF tdates IS NOT NULL AND array_length(tdates, 1 ) > 0 THEN
280
- EXECUTE ' SELECT array_agg(lll) FROM (SELECT distinct(date_trunc(' ' min' ' , unnest::timestamp with time zone)) as lll FROM unnest($1) ORDER BY date_trunc(' ' min' ' , unnest::timestamp with time zone)) as Z'
281
- INTO dates
282
- USING tdates;
283
- cron := COALESCE(cron, ' {}' ::jsonb) || json_build_object(' dates' , array_to_json(dates))::jsonb;
284
- END IF;
285
- RETURN cron;
306
+ EXECUTE ' SELECT jsonb_typeof($1)'
307
+ INTO vtype
308
+ USING value;
309
+ IF vtype = ' string' THEN
310
+ -- EXECUTE 'SELECT array_append($1, jsonb_set(''{"a":""}''::jsonb, ''{a}'', $2)->>''a'')'
311
+ EXECUTE ' SELECT array_append($1, $2->>0)'
312
+ INTO dst
313
+ USING dst, value;
314
+ ELSIF vtype = ' array' THEN
315
+ EXECUTE ' SELECT $1 || array_agg(value)::text[] from jsonb_array_elements_text($2)'
316
+ INTO dst
317
+ USING dst, value;
318
+ ELSE
319
+ RAISE EXCEPTION ' The value could be only ' ' string' ' or ' ' array' ' type' ;
320
+ END IF;
321
+
322
+ RETURN dst;
286
323
END
287
324
$BODY$
288
325
LANGUAGE plpgsql;
289
326
290
327
CREATE FUNCTION schedule ._get_commands_from_attrs(params jsonb) RETURNS text [] AS
291
328
$BODY$
292
329
DECLARE
293
- commands text [];
330
+ commands text [];
331
+ N integer ;
294
332
BEGIN
295
- IF params?' command' THEN
296
- EXECUTE ' SELECT array_append(' ' {}' ' ::text[], $1)'
297
- INTO commands
298
- USING params- >> ' command' ;
299
- ELSIF params?' commands' THEN
300
- EXECUTE ' SELECT array_agg(value)::text[] from jsonb_array_elements_text($1) as X'
301
- INTO commands
302
- USING params- > ' commands' ;
303
- ELSE
304
- RAISE EXCEPTION ' There is no information about what task to execute'
305
- USING HINT = ' Use ' ' command' ' or ' ' commands' ' key to transmit information' ;
333
+ N := 0 ;
334
+ IF params?' command' THEN
335
+ commands := schedule ._get_array_from_jsonb (commands, params- > ' command' );
336
+ END IF;
337
+
338
+ IF params?' commands' THEN
339
+ commands := schedule ._get_array_from_jsonb (commands, params- > ' commands' );
340
+ END IF;
341
+
342
+ N := array_length(commands, 1 );
343
+ IF N is NULL or N = 0 THEN
344
+ RAISE EXCEPTION ' There is no information about what job to execute'
345
+ USING HINT = ' Use ' ' command' ' or ' ' commands' ' key to transmit information' ;
306
346
END IF;
307
347
308
348
RETURN commands;
0 commit comments