Mercurial > gemma
comparison pkg/imports/queue.go @ 5712:6270951dda28 revive-cleanup
/interface{}/any/
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 20 Feb 2024 22:37:51 +0100 |
parents | 2dd155cc95ec |
children | 0542aec69375 |
comparison
equal
deleted
inserted
replaced
5711:2dd155cc95ec | 5712:6270951dda28 |
---|---|
37 type ( | 37 type ( |
38 // Feedback is passed to the Do method of a Job to log | 38 // Feedback is passed to the Do method of a Job to log |
39 // informations, warnings or errors. | 39 // informations, warnings or errors. |
40 Feedback interface { | 40 Feedback interface { |
41 // Info logs informations. | 41 // Info logs informations. |
42 Info(fmt string, args ...interface{}) | 42 Info(fmt string, args ...any) |
43 // Warn logs warnings. | 43 // Warn logs warnings. |
44 Warn(fmt string, args ...interface{}) | 44 Warn(fmt string, args ...any) |
45 // Error logs errors. | 45 // Error logs errors. |
46 Error(fmt string, args ...interface{}) | 46 Error(fmt string, args ...any) |
47 } | 47 } |
48 | 48 |
49 // UnchangedError may be issued by Do of a Job to indicate | 49 // UnchangedError may be issued by Do of a Job to indicate |
50 // That the database has not changed. | 50 // That the database has not changed. |
51 UnchangedError string | 51 UnchangedError string |
59 // feedback can be used to log the import process. | 59 // feedback can be used to log the import process. |
60 // If no error is return the import is assumed to | 60 // If no error is return the import is assumed to |
61 // be successfull. The non-error return value is | 61 // be successfull. The non-error return value is |
62 // serialized as a JSON string into the database as | 62 // serialized as a JSON string into the database as |
63 // a summary to the import to be used by the review process. | 63 // a summary to the import to be used by the review process. |
64 Do(ctx context.Context, id int64, conn *sql.Conn, feedback Feedback) (interface{}, error) | 64 Do(ctx context.Context, id int64, conn *sql.Conn, feedback Feedback) (any, error) |
65 // CleanUp is called to clean up ressources hold by the import. | 65 // CleanUp is called to clean up ressources hold by the import. |
66 // It is called whether the import succeeded or not. | 66 // It is called whether the import succeeded or not. |
67 CleanUp() error | 67 CleanUp() error |
68 } | 68 } |
69 | 69 |
293 func (rj *reviewedJob) Do( | 293 func (rj *reviewedJob) Do( |
294 ctx context.Context, | 294 ctx context.Context, |
295 importID int64, | 295 importID int64, |
296 conn *sql.Conn, | 296 conn *sql.Conn, |
297 _ Feedback, | 297 _ Feedback, |
298 ) (interface{}, error) { | 298 ) (any, error) { |
299 | 299 |
300 tx, err := conn.BeginTx(ctx, nil) | 300 tx, err := conn.BeginTx(ctx, nil) |
301 if err != nil { | 301 if err != nil { |
302 return nil, err | 302 return nil, err |
303 } | 303 } |
747 // to the given function. | 747 // to the given function. |
748 func All(fn func(JobKind, JobCreator)) { iqueue.All(fn) } | 748 func All(fn func(JobKind, JobCreator)) { iqueue.All(fn) } |
749 | 749 |
750 type logFeedback int64 | 750 type logFeedback int64 |
751 | 751 |
752 func (lf logFeedback) log(kind, format string, args ...interface{}) { | 752 func (lf logFeedback) log(kind, format string, args ...any) { |
753 ctx := context.Background() | 753 ctx := context.Background() |
754 err := auth.RunAs(ctx, queueUser, func(conn *sql.Conn) error { | 754 err := auth.RunAs(ctx, queueUser, func(conn *sql.Conn) error { |
755 _, err := conn.ExecContext( | 755 _, err := conn.ExecContext( |
756 ctx, logMessageSQL, int64(lf), kind, fmt.Sprintf(format, args...)) | 756 ctx, logMessageSQL, int64(lf), kind, fmt.Sprintf(format, args...)) |
757 return err | 757 return err |
759 if err != nil { | 759 if err != nil { |
760 log.Errorf("logging failed: %v\n", err) | 760 log.Errorf("logging failed: %v\n", err) |
761 } | 761 } |
762 } | 762 } |
763 | 763 |
764 func (lf logFeedback) Info(format string, args ...interface{}) { | 764 func (lf logFeedback) Info(format string, args ...any) { |
765 lf.log("info", format, args...) | 765 lf.log("info", format, args...) |
766 } | 766 } |
767 | 767 |
768 func (lf logFeedback) Warn(format string, args ...interface{}) { | 768 func (lf logFeedback) Warn(format string, args ...any) { |
769 lf.log("warn", format, args...) | 769 lf.log("warn", format, args...) |
770 } | 770 } |
771 | 771 |
772 func (lf logFeedback) Error(format string, args ...interface{}) { | 772 func (lf logFeedback) Error(format string, args ...any) { |
773 lf.log("error", format, args...) | 773 lf.log("error", format, args...) |
774 } | 774 } |
775 | 775 |
776 func survive(fn func() error) func() error { | 776 func survive(fn func() error) func() error { |
777 return func() (err error) { | 777 return func() (err error) { |
898 | 898 |
899 func updateStateSummary( | 899 func updateStateSummary( |
900 ctx context.Context, | 900 ctx context.Context, |
901 id int64, | 901 id int64, |
902 state string, | 902 state string, |
903 summary interface{}, | 903 summary any, |
904 ) error { | 904 ) error { |
905 var s sql.NullString | 905 var s sql.NullString |
906 if summary != nil { | 906 if summary != nil { |
907 var b strings.Builder | 907 var b strings.Builder |
908 if err := json.NewEncoder(&b).Encode(summary); err != nil { | 908 if err := json.NewEncoder(&b).Encode(summary); err != nil { |
922 _, err := conn.ExecContext(ctx, deleteJobSQL, id) | 922 _, err := conn.ExecContext(ctx, deleteJobSQL, id) |
923 return err | 923 return err |
924 }) | 924 }) |
925 } | 925 } |
926 | 926 |
927 func errorAndFail(id int64, format string, args ...interface{}) error { | 927 func errorAndFail(id int64, format string, args ...any) error { |
928 ctx := context.Background() | 928 ctx := context.Background() |
929 return tryHardToStoreState(ctx, func(conn *sql.Conn) error { | 929 return tryHardToStoreState(ctx, func(conn *sql.Conn) error { |
930 tx, err := conn.BeginTx(ctx, nil) | 930 tx, err := conn.BeginTx(ctx, nil) |
931 if err != nil { | 931 if err != nil { |
932 return err | 932 return err |
1018 } else { | 1018 } else { |
1019 feedback = logFeedback(idj.id) | 1019 feedback = logFeedback(idj.id) |
1020 } | 1020 } |
1021 | 1021 |
1022 ctx := context.Background() | 1022 ctx := context.Background() |
1023 var summary interface{} | 1023 var summary any |
1024 | 1024 |
1025 errDo := survive(func() error { | 1025 errDo := survive(func() error { |
1026 return auth.RunAs(ctx, idj.user, | 1026 return auth.RunAs(ctx, idj.user, |
1027 func(conn *sql.Conn) error { | 1027 func(conn *sql.Conn) error { |
1028 var err error | 1028 var err error |