Skip to content

Commit ec75594

Browse files
authored
refactor: remove callback from WALArchiver factory (#18)
Signed-off-by: Armando Ruocco <[email protected]>
1 parent 80c40ba commit ec75594

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

pkg/archiver/archiver.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func New(
6666
env []string,
6767
spoolDirectory string,
6868
pgDataDirectory string,
69-
removeEmptyFileArchive func() error,
69+
emptyWalArchivePath string,
7070
) (archiver *WALArchiver, err error) {
7171
contextLog := log.FromContext(ctx)
7272
var walArchiveSpool *spool.WALSpool
@@ -81,9 +81,9 @@ func New(
8181
env: env,
8282
pgDataDirectory: pgDataDirectory,
8383
barmanArchiver: &walarchive.BarmanArchiver{
84-
Env: env,
85-
Touch: walArchiveSpool.Touch,
86-
RemoveEmptyFileArchive: removeEmptyFileArchive,
84+
Env: env,
85+
Touch: walArchiveSpool.Touch,
86+
EmptyWalArchivePath: emptyWalArchivePath,
8787
},
8888
}
8989
return archiver, nil

pkg/archiver/command_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package archiver
1818

1919
import (
20+
"os"
2021
"strings"
2122

2223
barmanApi "github.com/cloudnative-pg/barman-cloud/pkg/api"
@@ -27,6 +28,8 @@ import (
2728

2829
var _ = Describe("barmanCloudWalArchiveOptions", func() {
2930
var config *barmanApi.BarmanObjectStoreConfiguration
31+
var tempDir string
32+
var tempEmptyWalArchivePath string
3033

3134
BeforeEach(func() {
3235
config = &barmanApi.BarmanObjectStoreConfiguration{
@@ -36,10 +39,20 @@ var _ = Describe("barmanCloudWalArchiveOptions", func() {
3639
Encryption: "aes256",
3740
},
3841
}
42+
var err error
43+
tempDir, err = os.MkdirTemp(os.TempDir(), "command_test")
44+
Expect(err).ToNot(HaveOccurred())
45+
file, err := os.CreateTemp(tempDir, "empty-wal-archive-path")
46+
Expect(err).ToNot(HaveOccurred())
47+
tempEmptyWalArchivePath = file.Name()
48+
})
49+
AfterEach(func() {
50+
err := os.RemoveAll(tempDir)
51+
Expect(err).ToNot(HaveOccurred())
3952
})
4053

4154
It("should generate correct arguments", func(ctx SpecContext) {
42-
archiver, err := New(ctx, nil, "spool", "pgdata", nil)
55+
archiver, err := New(ctx, nil, "spool", "pgdata", tempEmptyWalArchivePath)
4356
Expect(err).ToNot(HaveOccurred())
4457

4558
extraOptions := []string{"--min-chunk-size=5MB", "--read-timeout=60", "-vv"}
@@ -66,7 +79,7 @@ var _ = Describe("barmanCloudWalArchiveOptions", func() {
6679
config.Wal.ArchiveAdditionalCommandArgs = extraOptions
6780

6881
archiver, err := New(
69-
ctx, nil, "spool", "pgdata", nil)
82+
ctx, nil, "spool", "pgdata", tempEmptyWalArchivePath)
7083
Expect(err).ToNot(HaveOccurred())
7184

7285
options, err := archiver.BarmanCloudWalArchiveOptions(ctx, config, "test-cluster")

pkg/walarchive/cmd.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"time"
2626

2727
"github.com/cloudnative-pg/machinery/pkg/execlog"
28+
"github.com/cloudnative-pg/machinery/pkg/fileutils"
2829
"github.com/cloudnative-pg/machinery/pkg/log"
2930

3031
barmanCapabilities "github.com/cloudnative-pg/barman-cloud/pkg/capabilities"
@@ -33,9 +34,9 @@ import (
3334
// BarmanArchiver implements a WAL archiver based
3435
// on Barman cloud
3536
type BarmanArchiver struct {
36-
Env []string
37-
Touch func(walFile string) error
38-
RemoveEmptyFileArchive func() error
37+
Env []string
38+
Touch func(walFile string) error
39+
EmptyWalArchivePath string
3940
}
4041

4142
// WALArchiverResult contains the result of the archival of one WAL
@@ -89,7 +90,10 @@ func (archiver *BarmanArchiver) Archive(
8990

9091
// Removes the `.check-empty-wal-archive` file inside PGDATA after the
9192
// first successful archival of a WAL file.
92-
return archiver.RemoveEmptyFileArchive()
93+
if err := fileutils.RemoveFile(archiver.EmptyWalArchivePath); err != nil {
94+
return fmt.Errorf("error while deleting the check WAL file flag: %w", err)
95+
}
96+
return nil
9397
}
9498

9599
// ArchiveList archives a list of WAL files in parallel

0 commit comments

Comments
 (0)