Build a command to archive old jobs

This commit is contained in:
Pea Tyczynska
2018-11-22 17:49:04 +00:00
committed by Alexey Bezhan
parent 50811c3b8e
commit fd06924f3a
3 changed files with 33 additions and 2 deletions

View File

@@ -654,3 +654,33 @@ def populate_notification_postage(start_date):
total_updated += result.rowcount total_updated += result.rowcount
current_app.logger.info('Total inserted/updated records = {}'.format(total_updated)) 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))

View File

@@ -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): def dao_get_job_by_id(job_id):
return Job.query.filter_by(id=job_id).one() return Job.query.filter_by(id=job_id).one()
def dao_archive_job(job): def dao_archive_job(job):
job.archived = True job.archived = True
db.session.add(job) db.session.add(job)

View File

@@ -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(job2_to_delete.service_id, job2_to_delete.id),
call(job3_to_delete.service_id, job3_to_delete.id) call(job3_to_delete.service_id, job3_to_delete.id)
] ]
assert job1_to_delete.archived == True assert job1_to_delete.archived is True
assert dont_delete_me_1.archived == False assert dont_delete_me_1.archived is False
@freeze_time('2016-10-18T10:00:00') @freeze_time('2016-10-18T10:00:00')