@@ -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 longtask could be executed, NULL - infinite' ,
198+ ' max_run_time' ,' how longjob 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;
241241CREATE FUNCTION schedule ._get_cron_from_attrs(params jsonb) RETURNS jsonbAS
242242$BODY$
243243DECLARE
244- tdatestext [];
245- datestext [];
246- cron jsonb;
244+ datestext [];
245+ cron jsonb;
246+ clean_cron jsonb;
247+ Ninteger ;
248+ nametext ;
247249BEGIN
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 abouttask ' ' 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 aboutjob ' ' 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 cronIS 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 cronIS 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 nameIN SELECT * FROM unnest(' {dates, crontab, onstart, days, hours, wdays, months, 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(dsttext [], value jsonb) RETURNStext []AS
298+ $BODY$
299+ DECLARE
300+ vtypetext ;
301+ BEGIN
302+ IF value ISNULL THEN
303+ RETURN dst;
304+ END IF;
278305
279- IF tdatesIS 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;
286323END
287324$BODY$
288325LANGUAGE plpgsql;
289326
290327CREATE FUNCTION schedule ._get_commands_from_attrs(params jsonb) RETURNStext []AS
291328$BODY$
292329DECLARE
293- commandstext [];
330+ commandstext [];
331+ Ninteger ;
294332BEGIN
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 isNULL 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;
@@ -764,7 +804,7 @@ $BODY$
764804LANGUAGE plpgsql
765805 SECURITY DEFINER;
766806
767- CREATE FUNCTION schedule .get_user_owned_cron () RETURNS SETOFschedule .cron_rec AS
807+ CREATE FUNCTION schedule .get_owned_cron () RETURNS SETOFschedule .cron_rec AS
768808$BODY$
769809DECLARE
770810iischedule .cron ;
@@ -780,7 +820,8 @@ $BODY$
780820LANGUAGE plpgsql
781821 SECURITY DEFINER;
782822
783- CREATE FUNCTION schedule .get_user_owned_cron(usenametext ) RETURNS SETOFschedule .cron_rec AS
823+
824+ CREATE FUNCTION schedule .get_owned_cron(usenametext ) RETURNS SETOFschedule .cron_rec AS
784825$BODY$
785826DECLARE
786827iischedule .cron ;
@@ -800,6 +841,26 @@ $BODY$
800841LANGUAGE plpgsql
801842 SECURITY DEFINER;
802843
844+ CREATE FUNCTION schedule .get_user_owned_cron() RETURNS SETOFschedule .cron_rec AS
845+ $BODY$
846+ BEGIN
847+ RETURN QUERYSELECT * from schedule .get_owned_cron ();
848+ END
849+ $BODY$
850+ LANGUAGE plpgsql
851+ SECURITY DEFINER;
852+
853+ CREATE FUNCTION schedule .get_user_owned_cron(usenametext ) RETURNS SETOFschedule .cron_rec AS
854+ $BODY$
855+ BEGIN
856+ RETURN QUERYSELECT * from schedule .get_owned_cron (usename);
857+ END
858+ $BODY$
859+ LANGUAGE plpgsql
860+ SECURITY DEFINER;
861+
862+
863+
803864CREATE FUNCTION schedule .get_user_cron() RETURNS SETOFschedule .cron_rec AS
804865$BODY$
805866DECLARE
@@ -870,6 +931,16 @@ $BODY$
870931LANGUAGE plpgsql
871932 SECURITY DEFINER;
872933
934+ CREATE FUNCTION schedule .get_active_jobs(usenametext ) RETURNS SETOFschedule .cron_job AS
935+ $BODY$
936+ DECLARE
937+ BEGIN
938+ RETURN QUERYSELECT * FROM schedule .get_user_active_jobs (usename);
939+ END
940+ $BODY$
941+ LANGUAGE plpgsql
942+ SECURITY DEFINER;
943+
873944CREATE FUNCTION schedule .get_active_jobs() RETURNS SETOFschedule .cron_job AS
874945$BODY$
875946DECLARE
@@ -943,6 +1014,15 @@ $BODY$
9431014LANGUAGE plpgsql
9441015 SECURITY DEFINER;
9451016
1017+ CREATE FUNCTION schedule .get_log(usenametext ) RETURNS SETOFschedule .cron_job AS
1018+ $BODY$
1019+ BEGIN
1020+ RETURN QUERYSELECT * FROM schedule .get_user_log (usename);
1021+ END
1022+ $BODY$
1023+ LANGUAGE plpgsql
1024+ SECURITY DEFINER;
1025+
9461026CREATE FUNCTION schedule .get_log() RETURNS SETOFschedule .cron_job AS
9471027$BODY$
9481028BEGIN