From 1e436555a854fe5514eeef36f18d6f88a2c21119 Mon Sep 17 00:00:00 2001 From: Dimitri Fontaine Date: Thu, 29 Jun 2017 14:08:52 +0200 Subject: [PATCH] Refactor PostgreSQL conditions. Use a single deftype postgresql-unavailable rather than copy/pasting the same list of conditions in several places. --- src/package.lisp | 1 + src/pgsql/connection.lisp | 37 +++++++++++++++--------------- src/pgsql/copy-from-queue.lisp | 11 +-------- src/sources/common/db-methods.lisp | 11 +-------- 4 files changed, 21 insertions(+), 39 deletions(-) diff --git a/src/package.lisp b/src/package.lisp index 7d2e955..4b4b569 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -362,6 +362,7 @@ #:pgsql-execute #:pgsql-execute-with-timing #:pgsql-connect-and-execute-with-timing + #:postgresql-unavailable ;; postgresql schema facilities #:truncate-tables diff --git a/src/pgsql/connection.lisp b/src/pgsql/connection.lisp index 69cfe8b..65528d7 100644 --- a/src/pgsql/connection.lisp +++ b/src/pgsql/connection.lisp @@ -48,6 +48,22 @@ (defvar *pgsql-client-key* "~/.postgresql/postgresql.key" "File where to read the PostgreSQL Client Side SSL Private Key.") +;;; +;;; +;;; +(deftype postgresql-unavailable () + "It might happen that PostgreSQL becomes unavailable in the middle of + our processing: it being restarted is an example." + `(or + cl-postgres-error::server-shutdown + cl-postgres-error::admin-shutdown + cl-postgres-error::crash-shutdown + cl-postgres-error::operator-intervention + cl-postgres-error::cannot-connect-now + cl-postgres-error::database-connection-error + cl-postgres-error::database-connection-lost + cl-postgres-error::database-socket-error)) + ;;; ;;; We need to distinguish some special cases of PostgreSQL errors within ;;; Class 53 — Insufficient Resources: in case of "too many connections" we @@ -141,15 +157,7 @@ appropriate log level." `(handler-bind (((and cl-postgres:database-error - (not (or - cl-postgres-error::server-shutdown - cl-postgres-error::admin-shutdown - cl-postgres-error::crash-shutdown - cl-postgres-error::operator-intervention - cl-postgres-error::cannot-connect-now - cl-postgres-error::database-connection-error - cl-postgres-error::database-connection-lost - cl-postgres-error::database-socket-error))) + (not postgresql-unavailable)) #'(lambda (e) (log-message :error "~a" e))) (cl-postgres:postgresql-warning @@ -249,16 +257,7 @@ (pomo:with-transaction () (pgsql-execute-with-timing section label sql :count count))) - ((or - cl-postgres-error::server-shutdown - cl-postgres-error::admin-shutdown - cl-postgres-error::crash-shutdown - cl-postgres-error::operator-intervention - cl-postgres-error::cannot-connect-now - cl-postgres-error::database-connection-error - cl-postgres-error::database-connection-lost - cl-postgres-error::database-socket-error) - (condition) + (postgresql-unavailable (condition) (log-message :error "~a" condition) (log-message :error "Reconnecting to PostgreSQL") diff --git a/src/pgsql/copy-from-queue.lisp b/src/pgsql/copy-from-queue.lisp index 0116aa5..8b10dc5 100644 --- a/src/pgsql/copy-from-queue.lisp +++ b/src/pgsql/copy-from-queue.lisp @@ -87,16 +87,7 @@ (log-message :error "~a" condition) (retry-batch table columns batch batch-rows condition)))) - ((or - cl-postgres-error::server-shutdown - cl-postgres-error::admin-shutdown - cl-postgres-error::crash-shutdown - cl-postgres-error::operator-intervention - cl-postgres-error::cannot-connect-now - cl-postgres-error::database-connection-error - cl-postgres-error::database-connection-lost - cl-postgres-error::database-socket-error) - (condition) + (postgresql-unavailable (condition) (log-message :error "~a" condition) (log-message :error "Copy Batch reconnecting to PostgreSQL") diff --git a/src/sources/common/db-methods.lisp b/src/sources/common/db-methods.lisp index dd8ebe8..9b888f1 100644 --- a/src/sources/common/db-methods.lisp +++ b/src/sources/common/db-methods.lisp @@ -173,16 +173,7 @@ :section :post :label "Install Comments")) - ((or - cl-postgres-error::server-shutdown - cl-postgres-error::admin-shutdown - cl-postgres-error::crash-shutdown - cl-postgres-error::operator-intervention - cl-postgres-error::cannot-connect-now - cl-postgres-error::database-connection-error - cl-postgres-error::database-connection-lost - cl-postgres-error::database-socket-error) - (condition) + (postgresql-unavailable (condition) (log-message :error "~a" condition) (log-message :error