-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor main function and add crontab functionality
- Loading branch information
minpeter
committed
Mar 22, 2024
1 parent
a1b5f90
commit cba0e81
Showing
5 changed files
with
127 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package crontab | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"time" | ||
|
||
"github.com/robfig/cron" | ||
"github.com/tempfiles-Team/tempfiles-backend/database" | ||
) | ||
|
||
func Crontab() { | ||
terminator := cron.New() | ||
|
||
terminator.AddFunc("1 */5 * * *", func() { | ||
log.Println("⏲️ Check for expired files", time.Now().Format("2006-01-02 15:04:05")) | ||
var files []database.FileTracking | ||
if err := database.Engine.Where("expire_time < ? and is_deleted = ?", time.Now(), false).Find(&files); err != nil { | ||
log.Println("cron db query error", err.Error()) | ||
} | ||
for _, file := range files { | ||
log.Printf("🗑️ Set this folder for deletion: %s \n", file.FolderId) | ||
file.IsDeleted = true | ||
if _, err := database.Engine.ID(file.Id).Cols("Is_deleted").Update(&file); err != nil { | ||
log.Printf("cron db update error, file: %s, error: %s\n", file.FolderId, err.Error()) | ||
} | ||
} | ||
}) | ||
|
||
terminator.AddFunc("1 */20 * * *", func() { | ||
log.Println("⏲️ Check which files need to be deleted", time.Now().Format("2006-01-02 15:04:05")) | ||
var files []database.FileTracking | ||
if err := database.Engine.Where("is_deleted = ?", true).Find(&files); err != nil { | ||
log.Println("file list error: ", err.Error()) | ||
} | ||
for _, file := range files { | ||
log.Printf("🗑️ Delete this folder: %s\n", file.FolderId) | ||
if err := os.RemoveAll("./tmp/" + file.FolderId); err != nil { | ||
log.Println("delete file error: ", err.Error()) | ||
} | ||
if _, err := database.Engine.Delete(&file); err != nil { | ||
log.Println("delete file error: ", err.Error()) | ||
} | ||
} | ||
}) | ||
|
||
terminator.Start() | ||
defer terminator.Stop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"components":{"schemas":{"Files":{"properties":{"data":{},"error":{"type":"string"},"message":{"type":"string"}},"type":"object"},"string":{"type":"string"},"unknown-interface":{}}},"info":{"description":"OpenAPI","title":"OpenAPI","version":"0.0.1"},"openapi":"3.0.3","paths":{"/":{"get":{"description":"controller: main.main.func3","operationId":"GET /:func3","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/string"}}},"description":"OK"},"default":{"description":""}},"summary":"func3","tags":["string"]}},"/files/":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getAllFiles","operationId":"GET /files/:getAllFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getAllFiles","tags":["Files"]},"post":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.postFiles","operationId":"POST /files/:postFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"postFiles","tags":["Files"]}},"/files/{id}":{"delete":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.deleteFiles","operationId":"DELETE /files/{id}:deleteFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"deleteFiles","tags":["Files"]},"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getFiles","operationId":"GET /files/{id}:getFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getFiles","tags":["Files"]}},"/files/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.downloadFile","operationId":"GET /files/{id}/{name}:downloadFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"downloadFile","tags":["files"]}}}} | ||
{"components":{"schemas":{"Files":{"properties":{"data":{},"error":{"type":"string"},"message":{"type":"string"}},"type":"object"},"string":{"type":"string"},"unknown-interface":{}}},"info":{"description":"OpenAPI","title":"OpenAPI","version":"0.0.1"},"openapi":"3.0.3","paths":{"/":{"get":{"description":"controller: main.main.func1","operationId":"GET /:func1","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/string"}}},"description":"OK"},"default":{"description":""}},"summary":"func1","tags":["string"]}},"/files/":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getAllFiles","operationId":"GET /files/:getAllFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getAllFiles","tags":["Files"]},"post":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.postFiles","operationId":"POST /files/:postFiles","responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"postFiles","tags":["Files"]}},"/files/{id}":{"delete":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.deleteFiles","operationId":"DELETE /files/{id}:deleteFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"deleteFiles","tags":["Files"]},"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.getFiles","operationId":"GET /files/{id}:getFiles","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Files"}}},"description":"OK"},"default":{"description":""}},"summary":"getFiles","tags":["Files"]}},"/files/{id}/{name}":{"get":{"description":"controller: github.com/tempfiles-Team/tempfiles-backend/controllers.FilesRessources.downloadFile","operationId":"GET /files/{id}/{name}:downloadFile","parameters":[{"in":"path","name":"id","required":true,"schema":{"type":"string"}},{"in":"path","name":"name","required":true,"schema":{"type":"string"}}],"responses":{"200":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/unknown-interface"}}},"description":"OK"},"default":{"description":""}},"summary":"downloadFile","tags":["files"]}}}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.