diff --git a/internal/app/fs.go b/internal/app/fs.go index 5196d13..84d4209 100644 --- a/internal/app/fs.go +++ b/internal/app/fs.go @@ -152,7 +152,7 @@ func (f *fsPlugin) Find(thread *starlark.Thread, builtin *starlark.Builtin, args var minSize, limit starlark.Int var ignoreError starlark.Bool - if err := starlark.UnpackArgs("find", args, kwargs, "path", &path, "name?", nameGlob, "limit?", &limit, "min_size?", &minSize, "ignore_errors", &ignoreError); err != nil { + if err := starlark.UnpackArgs("find", args, kwargs, "path", &path, "name?", &nameGlob, "limit?", &limit, "min_size?", &minSize, "ignore_errors", &ignoreError); err != nil { return nil, err } diff --git a/internal/app/fs_store.go b/internal/app/fs_store.go index bca7d2f..865f68b 100644 --- a/internal/app/fs_store.go +++ b/internal/app/fs_store.go @@ -53,25 +53,38 @@ func InitFileStore(ctx context.Context, connectString string) error { return nil } -func backgroundCleanup(ctx context.Context, cleanupTicker *time.Ticker) { - for range cleanupTicker.C { - expired, err := listExpiredFile(ctx) - if err != nil { - fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err) - break - } +func fileCleanup(ctx context.Context) error { + expired, err := listExpiredFile(ctx) + if err != nil { + return fmt.Errorf("error cleaning up expired files %w", err) + } - for _, file := range expired { - if strings.HasPrefix(file.FilePath, "file://") { - err := os.Remove(strings.TrimPrefix(file.FilePath, "file://")) - if err != nil { - fmt.Fprintf(os.Stderr, "error deleting file %s: %s", file.FilePath, err) - } + for _, file := range expired { + if strings.HasPrefix(file.FilePath, "file://") { + err := os.Remove(strings.TrimPrefix(file.FilePath, "file://")) + if err != nil { + return fmt.Errorf("error deleting file %s: %w", file.FilePath, err) } } - err = deleteExpiredFiles(ctx) + } + err = deleteExpiredFiles(ctx) + if err != nil { + return fmt.Errorf("error deleting expired files %w", err) + } + return nil +} + +func backgroundCleanup(ctx context.Context, cleanupTicker *time.Ticker) { + err := fileCleanup(ctx) + if err != nil { + fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err) + return + } + + for range cleanupTicker.C { + err := fileCleanup(ctx) if err != nil { - fmt.Fprintf(os.Stderr, "error deleting expired files %s", err) + fmt.Fprintf(os.Stderr, "error cleaning up expired files %s", err) break } } diff --git a/tests/commander/test_misc.yaml b/tests/commander/test_misc.yaml index 3537b78..d8a63cc 100644 --- a/tests/commander/test_misc.yaml +++ b/tests/commander/test_misc.yaml @@ -141,3 +141,8 @@ tests: stdout: /numlines/_clace_app/file/usr_file_ actions0500: command: rm action_inp.txt + actions1000: # Create app + command: ../clace app create --auth=none --approve ./file_app /file_app + actions1100: + command: curl localhost:25222/file_app + stdout: Success diff --git a/tests/file_app/app.star b/tests/file_app/app.star new file mode 100644 index 0000000..acf2845 --- /dev/null +++ b/tests/file_app/app.star @@ -0,0 +1,45 @@ +load("fs.in", "fs") +load("exec.in", "exec") +load("http.in", "http") + +def handler(req): + exec.run("sh", ["-c", "rm -rf /tmp/fileapptmp"]) + exec.run("sh", ["-c", "mkdir /tmp/fileapptmp"]) + ret1 = exec.run("sh", ["-c", "echo \"abc\" > /tmp/fileapptmp/testfileapp.txt"]) + ret11 = exec.run("sh", ["-c", "echo \"abc\" > /tmp/fileapptmp/testfileapp2.txt"]) + ret2 = fs.load_file("/tmp/fileapptmp/testfileapp.txt") # single_access=True is default + ret3 = fs.load_file("/tmp/fileapptmp/testfileapp2.txt", single_access=False) + + # First attempt works + ret4 = http.get("http://localhost:25222" + ret2.value["url"]) + if ret4.value.status_code != 200: + return "Error: %s" % ret4.value.status_code + ret5 = http.get("http://localhost:25222" + ret3.value["url"]) + if ret5.value.status_code != 200: + return "Error: %d" % ret5.value.status_code + + # Second attempt fails for file1 (it got deleted on first GET call), works for file2 as it is multi_access + ret6 = http.get("http://localhost:25222" + ret2.value["url"]) + if ret6.value.status_code == 200: + return "Error: %d" % ret6.value.status_code + ret7 = http.get("http://localhost:25222" + ret3.value["url"]) + if ret7.value.status_code != 200: + return "Error: %d" % ret7.value.status_code + + ret8 = fs.find("/tmp/fileapptmp", "testfileapp.txt") + if len(ret8.value) != 0: + return "Error: expected no files on disk %s" % ret8.value + ret9 = fs.find("/tmp/fileapptmp", "testfileapp2.txt") + if len(ret9.value) == 0: + return "Error: expected files on disk" + + return "Success" + +app = ace.app("file test", + routes = [ace.api("/", type=ace.TEXT)], + permissions = [ + ace.permission("fs.in", "load_file"), + ace.permission("fs.in", "find"), + ace.permission("exec.in", "run"), + ace.permission("http.in", "get")] + )