diff --git a/service/tasks.py b/service/tasks.py index 3dd0a9d..0849551 100644 --- a/service/tasks.py +++ b/service/tasks.py @@ -228,6 +228,7 @@ def raschet(): update_fields_working = update_fields.loc[update_fields.status == 'Working'].reset_index(drop=True) update_fields_working = update_fields_working.fillna(0) + connection.close() except Exception as e: log_to_telegram(f'Ошибка при обновлении полей в базе данных: {e}') log_to_telegram('Начинается обновление полей в базе') @@ -242,74 +243,76 @@ def raschet(): ) cursor = conn2.cursor() except: + conn2 = None log_to_telegram('Не удалось подключиться к базе данных') - # prediction_current - update_records1 = [] - for i in range(0, len(update_fields)): - update_records1.append((int(update_fields.prediction_current[i]), int(update_fields.id[i]))) - sql_update_query = """Update service_placementpoint set prediction_current = %s where id = %s""" - try: - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records1) - conn2.commit() - except Exception: - cursor.execute('ROLLBACK') - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records1) - conn2.commit() - - # plan_first - update_records2 = [] - for i in range(0, len(update_fields_working)): - update_records2.append((int(update_fields_working.plan_first[i]), int(update_fields_working.id[i]))) - sql_update_query = """Update service_placementpoint set plan_first = %s where id = %s""" - try: - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records2) - conn2.commit() - except Exception: - cursor.execute('ROLLBACK') - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records2) - conn2.commit() - - # plan_current - update_records3 = [] - for i in range(0, len(update_fields_working)): - update_records3.append((int(update_fields_working.plan_current[i]), int(update_fields_working.id[i]))) - sql_update_query = """Update service_placementpoint set plan_current = %s where id = %s""" - try: - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records3) - conn2.commit() - except Exception: - cursor.execute('ROLLBACK') - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records3) - conn2.commit() - - # delta_first - update_records4 = [] - for i in range(0, len(update_fields_working)): - update_records4.append((int(update_fields_working.delta_first[i]), int(update_fields_working.id[i]))) - sql_update_query = """Update service_placementpoint set delta_first = %s where id = %s""" - try: - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records4) - conn2.commit() - except Exception: - cursor.execute('ROLLBACK') - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records4) - conn2.commit() - - # delta_current - update_records5 = [] - for i in range(0, len(update_fields_working)): - update_records5.append((int(update_fields_working.delta_current[i]), int(update_fields_working.id[i]))) - sql_update_query = """Update service_placementpoint set delta_current = %s where id = %s""" - try: - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records5) - conn2.commit() - except Exception: - cursor.execute('ROLLBACK') - psycopg2.extras.execute_batch(cursor, sql_update_query, update_records5) - conn2.commit() - cache.clear() - log_to_telegram('end raschet') + if conn2 is not None: + update_records1 = [] + for i in range(0, len(update_fields)): + update_records1.append((int(update_fields.prediction_current[i]), int(update_fields.id[i]))) + sql_update_query = """Update service_placementpoint set prediction_current = %s where id = %s""" + try: + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records1) + conn2.commit() + except Exception: + cursor.execute('ROLLBACK') + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records1) + conn2.commit() + + # plan_first + update_records2 = [] + for i in range(0, len(update_fields_working)): + update_records2.append((int(update_fields_working.plan_first[i]), int(update_fields_working.id[i]))) + sql_update_query = """Update service_placementpoint set plan_first = %s where id = %s""" + try: + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records2) + conn2.commit() + except Exception: + cursor.execute('ROLLBACK') + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records2) + conn2.commit() + + # plan_current + update_records3 = [] + for i in range(0, len(update_fields_working)): + update_records3.append((int(update_fields_working.plan_current[i]), int(update_fields_working.id[i]))) + sql_update_query = """Update service_placementpoint set plan_current = %s where id = %s""" + try: + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records3) + conn2.commit() + except Exception: + cursor.execute('ROLLBACK') + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records3) + conn2.commit() + + # delta_first + update_records4 = [] + for i in range(0, len(update_fields_working)): + update_records4.append((int(update_fields_working.delta_first[i]), int(update_fields_working.id[i]))) + sql_update_query = """Update service_placementpoint set delta_first = %s where id = %s""" + try: + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records4) + conn2.commit() + except Exception: + cursor.execute('ROLLBACK') + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records4) + conn2.commit() + + # delta_current + update_records5 = [] + for i in range(0, len(update_fields_working)): + update_records5.append((int(update_fields_working.delta_current[i]), int(update_fields_working.id[i]))) + sql_update_query = """Update service_placementpoint set delta_current = %s where id = %s""" + try: + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records5) + conn2.commit() + except Exception: + cursor.execute('ROLLBACK') + psycopg2.extras.execute_batch(cursor, sql_update_query, update_records5) + conn2.commit() + conn2.close() + cache.clear() + log_to_telegram('end raschet') @shared_task