Changeset View
Changeset View
Standalone View
Standalone View
swh/scheduler/sql/40-func.sql
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | as $$ | ||||
order by t.next_run | order by t.next_run | ||||
limit num_tasks_priority | limit num_tasks_priority | ||||
for update skip locked; | for update skip locked; | ||||
$$; | $$; | ||||
comment on function swh_scheduler_peek_tasks_with_priority(text, timestamptz, bigint, task_priority) | comment on function swh_scheduler_peek_tasks_with_priority(text, timestamptz, bigint, task_priority) | ||||
is 'Retrieve tasks with a given priority'; | is 'Retrieve tasks with a given priority'; | ||||
create or replace function swh_scheduler_peek_priority_tasks (task_type text, ts timestamptz default now(), | |||||
num_tasks_priority bigint default NULL) | |||||
returns setof task | |||||
language plpgsql | |||||
as $$ | |||||
declare | |||||
r record; | |||||
count_row bigint; | |||||
nb_diff bigint; | |||||
nb_high bigint; | |||||
nb_normal bigint; | |||||
nb_low bigint; | |||||
begin | |||||
-- expected values to fetch | |||||
select swh_scheduler_nb_priority_tasks(num_tasks_priority, 'high') into nb_high; | |||||
select swh_scheduler_nb_priority_tasks(num_tasks_priority, 'normal') into nb_normal; | |||||
select swh_scheduler_nb_priority_tasks(num_tasks_priority, 'low') into nb_low; | |||||
nb_diff := 0; | |||||
count_row := 0; | |||||
for r in select * from swh_scheduler_peek_tasks_with_priority(task_type, ts, nb_high, 'high') | |||||
loop | |||||
count_row := count_row + 1; | |||||
return next r; | |||||
end loop; | |||||
if count_row < nb_high then | |||||
nb_normal := nb_normal + nb_high - count_row; | |||||
end if; | |||||
count_row := 0; | |||||
for r in select * from swh_scheduler_peek_tasks_with_priority(task_type, ts, nb_normal, 'normal') | |||||
loop | |||||
count_row := count_row + 1; | |||||
return next r; | |||||
end loop; | |||||
if count_row < nb_normal then | |||||
nb_low := nb_low + nb_normal - count_row; | |||||
end if; | |||||
return query select * from swh_scheduler_peek_tasks_with_priority(task_type, ts, nb_low, 'low'); | |||||
end | |||||
$$; | |||||
comment on function swh_scheduler_peek_priority_tasks(text, timestamptz, bigint) | |||||
is 'Retrieve priority tasks'; | |||||
create or replace function swh_scheduler_peek_ready_tasks (task_type text, ts timestamptz default now(), | |||||
num_tasks bigint default NULL, num_tasks_priority bigint default NULL) | |||||
returns setof task | |||||
language plpgsql | |||||
as $$ | |||||
declare | |||||
r record; | |||||
count_row bigint; | |||||
nb_diff bigint; | |||||
nb_tasks bigint; | |||||
begin | |||||
count_row := 0; | |||||
for r in select * from swh_scheduler_peek_priority_tasks(task_type, ts, num_tasks_priority) | |||||
order by priority, next_run | |||||
loop | |||||
count_row := count_row + 1; | |||||
return next r; | |||||
end loop; | |||||
if count_row < num_tasks_priority then | |||||
nb_tasks := num_tasks + num_tasks_priority - count_row; | |||||
else | |||||
nb_tasks := num_tasks; | |||||
end if; | |||||
for r in select * from swh_scheduler_peek_no_priority_tasks(task_type, ts, nb_tasks) | |||||
order by priority, next_run | |||||
loop | |||||
return next r; | |||||
end loop; | |||||
return; | |||||
end | |||||
$$; | |||||
comment on function swh_scheduler_peek_ready_tasks(text, timestamptz, bigint, bigint) | |||||
is 'Retrieve tasks with/without priority in order'; | |||||
create or replace function swh_scheduler_grab_ready_tasks (task_type text, ts timestamptz default now(), | create or replace function swh_scheduler_grab_ready_tasks (task_type text, ts timestamptz default now(), | ||||
num_tasks bigint default NULL, | num_tasks bigint default NULL) | ||||
num_tasks_priority bigint default NULL) | |||||
returns setof task | returns setof task | ||||
language sql | language sql | ||||
as $$ | as $$ | ||||
update task | update task | ||||
set status='next_run_scheduled' | set status='next_run_scheduled' | ||||
from ( | from ( | ||||
select id from swh_scheduler_peek_ready_tasks(task_type, ts, num_tasks, num_tasks_priority) | select id from swh_scheduler_peek_no_priority_tasks(task_type, ts, num_tasks) | ||||
) next_tasks | ) next_tasks | ||||
where task.id = next_tasks.id | where task.id = next_tasks.id | ||||
returning task.*; | returning task.*; | ||||
$$; | $$; | ||||
comment on function swh_scheduler_grab_ready_tasks (text, timestamptz, bigint, bigint) | comment on function swh_scheduler_grab_ready_tasks (text, timestamptz, bigint) | ||||
is 'Grab tasks ready for scheduling and change their status'; | is 'Grab (no priority) tasks ready for scheduling and change their status'; | ||||
create or replace function swh_scheduler_peek_any_ready_priority_tasks ( | create or replace function swh_scheduler_peek_any_ready_priority_tasks ( | ||||
task_type text, ts timestamptz default now(), | task_type text, ts timestamptz default now(), | ||||
num_tasks bigint default NULL | num_tasks bigint default NULL | ||||
) | ) | ||||
returns setof task | returns setof task | ||||
language sql stable | language sql stable | ||||
as $$ | as $$ | ||||
▲ Show 20 Lines • Show All 272 Lines • Show Last 20 Lines |