diff --git a/app/commands.py b/app/commands.py index 918a25242..41f58c4dd 100644 --- a/app/commands.py +++ b/app/commands.py @@ -654,3 +654,33 @@ def populate_notification_postage(start_date): total_updated += result.rowcount current_app.logger.info('Total inserted/updated records = {}'.format(total_updated)) + + +@notify_command(name='archive-jobs-created-between-dates') +@click.option('-s', '--start_date', required=True, help="start date inclusive", type=click_dt(format='%Y-%m-%d')) +@click.option('-e', '--end_date', required=True, help="end date inclusive", type=click_dt(format='%Y-%m-%d')) +@statsd(namespace="tasks") +def update_jobs_archived_flag(start_date, end_date): + current_app.logger.info('Archiving jobs created between {} to {}'.format(start_date, end_date)) + + process_date = start_date + total_updated = 0 + + while process_date < end_date: + start_time = datetime.utcnow() + sql = """update + jobs set archived = true + where + created_at >= (date :start + time '00:00:00') at time zone 'Europe/London' + at time zone 'UTC' + and created_at < (date :end + time '00:00:00') at time zone 'Europe/London' at time zone 'UTC'""" + + result = db.session.execute(sql, {"start": process_date, "end": process_date + timedelta(days=1)}) + db.session.commit() + current_app.logger.info('jobs: --- Completed took {}ms. Archived {} jobs for {}'.format( + datetime.now() - start_time, result.rowcount, process_date)) + + process_date += timedelta(days=1) + + total_updated += result.rowcount + current_app.logger.info('Total archived jobs = {}'.format(total_updated)) diff --git a/app/dao/jobs_dao.py b/app/dao/jobs_dao.py index c4a3fc585..6e3d2f25b 100644 --- a/app/dao/jobs_dao.py +++ b/app/dao/jobs_dao.py @@ -66,6 +66,7 @@ def dao_get_jobs_by_service_id(service_id, limit_days=None, page=1, page_size=50 def dao_get_job_by_id(job_id): return Job.query.filter_by(id=job_id).one() + def dao_archive_job(job): job.archived = True db.session.add(job) diff --git a/tests/app/celery/test_scheduled_tasks.py b/tests/app/celery/test_scheduled_tasks.py index fadc68136..02007195f 100644 --- a/tests/app/celery/test_scheduled_tasks.py +++ b/tests/app/celery/test_scheduled_tasks.py @@ -305,8 +305,8 @@ def test_will_remove_csv_files_for_jobs_older_than_seven_days( call(job2_to_delete.service_id, job2_to_delete.id), call(job3_to_delete.service_id, job3_to_delete.id) ] - assert job1_to_delete.archived == True - assert dont_delete_me_1.archived == False + assert job1_to_delete.archived is True + assert dont_delete_me_1.archived is False @freeze_time('2016-10-18T10:00:00')