Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 31ea678

Browse files
author
Vladimir Ershov
committed
date/dates && command/commands function merge
1 parent 637ae9c commit 31ea678

File tree

1 file changed

+89
-49
lines changed

1 file changed

+89
-49
lines changed

pgpro_scheduler--1.0.sql

Lines changed: 89 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ BEGIN
195195
'dates', 'Set of exact dates when comman will be executed',
196196
'use_same_transaction', 'if set of commans should be executed within the same transaction',
197197
'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',
199199
'max_instances', 'the number of instances run at the same time',
200200
'onrollback', 'statement to be executed after rollback if one occured',
201201
'next_time_statement', 'statement to be executed last to calc next execution time'
@@ -241,68 +241,108 @@ LANGUAGE plpgsql;
241241
CREATE FUNCTION schedule._get_cron_from_attrs(params jsonb) RETURNS jsonb AS
242242
$BODY$
243243
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;
247249
BEGIN
248250

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;
259261

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;
267270

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;
271296

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;
278305

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;
286323
END
287324
$BODY$
288325
LANGUAGE plpgsql;
289326

290327
CREATE FUNCTION schedule._get_commands_from_attrs(params jsonb) RETURNS text[] AS
291328
$BODY$
292329
DECLARE
293-
commands text[];
330+
commands text[];
331+
N integer;
294332
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';
306346
END IF;
307347

308348
RETURN commands;

0 commit comments

Comments
 (0)