diff --git a/swh/scheduler/backend.py b/swh/scheduler/backend.py --- a/swh/scheduler/backend.py +++ b/swh/scheduler/backend.py @@ -292,13 +292,23 @@ return cursor.fetchall() @autocommit - def set_status_tasks(self, task_ids, status='disabled', cursor=None): - """Set the tasks' status whose ids are listed.""" + def set_status_tasks(self, task_ids, + status='disabled', next_run=None, cursor=None): + """Set the tasks' status whose ids are listed. + + If given, also set the next_run date. + """ if not task_ids: return - query = "UPDATE task SET status = %s WHERE id IN %s" - cursor.execute(query, (status, tuple(task_ids),)) - return None + query = ["UPDATE task SET status = %s"] + args = [status] + if next_run: + query.append(', next_run = %s') + args.append(next_run) + query.append(" WHERE id IN %s") + args.append(tuple(task_ids)) + + cursor.execute(''.join(query), args) @autocommit def disable_tasks(self, task_ids, cursor=None): diff --git a/swh/scheduler/cli.py b/swh/scheduler/cli.py --- a/swh/scheduler/cli.py +++ b/swh/scheduler/cli.py @@ -342,6 +342,37 @@ click.echo('\n'.join(output)) +@task.command('respawn') +@click.argument('task-ids', required=True, nargs=-1) +@click.option('--next-run', '-n', required=False, type=DATETIME, + default=None, + help='Re spawn the selected tasks at tis date') +@click.pass_context +def respawn_tasks(ctx, task_ids, next_run): + """Respawn tasks. + + Respawn the give tasks (by their id, see the 'task list' command to + find task ids) at the given date (immediately by default). + + Eg. + + swh-scheduler task respawn 1 3 12 + """ + scheduler = ctx.obj['scheduler'] + if not scheduler: + raise ValueError('Scheduler class (local/remote) must be instantiated') + if next_run is None: + next_run = arrow.utcnow() + output = [] + + scheduler.set_status_tasks( + task_ids, status='next_run_not_scheduled', next_run=next_run) + output.append('Respawn tasks %s\n' % ( + task_ids)) + + click.echo('\n'.join(output)) + + @task.command('archive') @click.option('--before', '-b', default=None, help='''Task whose ended date is anterior will be archived.