def __setitem__(self, connection, key, value): class LastErrorEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, type): return repr(obj) return json.JSONEncoder.default(self, obj) try: single_value_sql( connection, """SELECT app_name FROM crontabber WHERE app_name = %s""", (key, )) # the key exists, do an update next_sql = """UPDATE crontabber SET next_run = %(next_run)s, first_run = %(first_run)s, last_run = %(last_run)s, last_success = %(last_success)s, depends_on = %(depends_on)s, error_count = %(error_count)s, last_error = %(last_error)s WHERE app_name = %(app_name)s""" except SQLDidNotReturnSingleValue: # the key does not exist, do an insert next_sql = """ INSERT INTO crontabber ( app_name, next_run, first_run, last_run, last_success, depends_on, error_count, last_error ) VALUES ( %(app_name)s, %(next_run)s, %(first_run)s, %(last_run)s, %(last_success)s, %(depends_on)s, %(error_count)s, %(last_error)s )""" parameters = { 'app_name': key, 'next_run': value['next_run'], 'first_run': value['first_run'], 'last_run': value['last_run'], 'last_success': value.get('last_success'), 'depends_on': value['depends_on'], 'error_count': value['error_count'], 'last_error': json.dumps(value['last_error'], cls=LastErrorEncoder) } execute_no_results(connection, next_sql, parameters)
def __delitem__(self, connection, key): """remove the item by key or raise KeyError""" try: # result intentionally ignored single_value_sql( connection, """SELECT app_name FROM crontabber WHERE app_name = %s""", (key, )) except SQLDidNotReturnSingleValue: raise KeyError(key) # item exists execute_no_results( connection, """DELETE FROM crontabber WHERE app_name = %s""", (key, ))
def __delitem__(self, connection, key): """remove the item by key or raise KeyError""" try: # result intentionally ignored single_value_sql( connection, """SELECT app_name FROM crontabber WHERE app_name = %s""", (key,) ) except SQLDidNotReturnSingleValue: raise KeyError(key) # item exists execute_no_results( connection, """DELETE FROM crontabber WHERE app_name = %s""", (key,) )
def __setitem__(self, connection, key, value): class LastErrorEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, type): return repr(obj) return json.JSONEncoder.default(self, obj) try: single_value_sql( connection, """SELECT ongoing FROM crontabber WHERE app_name = %s FOR UPDATE NOWAIT """, (key, )) # If the above single_value_sql() didn't raise a # SQLDidNotReturnSingleValue exception, it means # there is a row by this app_name. # Therefore, the next SQL is an update. next_sql = """ UPDATE crontabber SET next_run = %(next_run)s, first_run = %(first_run)s, last_run = %(last_run)s, last_success = %(last_success)s, depends_on = %(depends_on)s, error_count = %(error_count)s, last_error = %(last_error)s, ongoing = %(ongoing)s WHERE app_name = %(app_name)s """ except OperationalError as exception: if 'could not obtain lock' in exception.args[0]: raise RowLevelLockError(exception.args[0]) else: raise except SQLDidNotReturnSingleValue: # the key does not exist, do an insert next_sql = """ INSERT INTO crontabber ( app_name, next_run, first_run, last_run, last_success, depends_on, error_count, last_error, ongoing ) VALUES ( %(app_name)s, %(next_run)s, %(first_run)s, %(last_run)s, %(last_success)s, %(depends_on)s, %(error_count)s, %(last_error)s, %(ongoing)s ) """ parameters = { 'app_name': key, 'next_run': value['next_run'], 'first_run': value['first_run'], 'last_run': value['last_run'], 'last_success': value.get('last_success'), 'depends_on': value['depends_on'], 'error_count': value['error_count'], 'last_error': json.dumps(value['last_error'], cls=LastErrorEncoder), 'ongoing': value.get('ongoing'), } try: execute_no_results(connection, next_sql, parameters) except IntegrityError as exception: # See CREATE_CRONTABBER_APP_NAME_UNIQUE_INDEX for why # we know to look for this mentioned in the error message. if 'crontabber_unique_app_name_idx' in exception.args[0]: raise RowLevelLockError(exception.args[0]) raise
def __setitem__(self, connection, key, value): class LastErrorEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, type): return repr(obj) return json.JSONEncoder.default(self, obj) try: single_value_sql( connection, """SELECT app_name FROM crontabber WHERE app_name = %s""", (key,) ) # the key exists, do an update next_sql = """UPDATE crontabber SET next_run = %(next_run)s, first_run = %(first_run)s, last_run = %(last_run)s, last_success = %(last_success)s, depends_on = %(depends_on)s, error_count = %(error_count)s, last_error = %(last_error)s WHERE app_name = %(app_name)s""" except SQLDidNotReturnSingleValue: # the key does not exist, do an insert next_sql = """ INSERT INTO crontabber ( app_name, next_run, first_run, last_run, last_success, depends_on, error_count, last_error ) VALUES ( %(app_name)s, %(next_run)s, %(first_run)s, %(last_run)s, %(last_success)s, %(depends_on)s, %(error_count)s, %(last_error)s )""" parameters = { 'app_name': key, 'next_run': value['next_run'], 'first_run': value['first_run'], 'last_run': value['last_run'], 'last_success': value.get('last_success'), 'depends_on': value['depends_on'], 'error_count': value['error_count'], 'last_error': json.dumps(value['last_error'], cls=LastErrorEncoder) } execute_no_results( connection, next_sql, parameters )