Commit 697729af authored by Lukas Böhm's avatar Lukas Böhm 🎱
Browse files

add background jobs

parent 03a515fa
......@@ -215,6 +215,20 @@ func CloseShare(w http.ResponseWriter, r *http.Request) *HTTPError {
return &HTTPError{err, "Can't edit data", 500}
}
// background job
{
job, err := enqueuer.Enqueue("DeleteShare", nil)
////deleteIn := time.Now().Sub(*share.Expires)
//deleteIn := 1
//job, err := enqueuer.EnqueueIn("DeleteShare", int64(deleteIn), map[string]interface{}{
// "ShareID": share.ID,
//})
if err != nil {
return &HTTPError{ err, "Error creating background job", 500 }
}
PrettyPrint(job)
}
return SendJSON(w, share)
}
......@@ -255,7 +269,7 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
if err != nil {
return &HTTPError{err, "Request does not contain a valid body (parsing file)", 400}
}
defer file.Close() // TODO fehlerbehandlung
defer file.Close()
var att Attachment
{
......@@ -277,8 +291,8 @@ func UploadAttachment(w http.ResponseWriter, r *http.Request) *HTTPError {
}
err = ioutil.WriteFile(filepath.Join(config.mediaDir, "temp", sid.String(), att.ID.String()), fileBytes, os.ModePerm)
if err != nil {
return &HTTPError{ err, "cant save file", 500 }
db.Rollback()
return &HTTPError{ err, "cant save file", 500 }
}
db.Commit()
}
......
package main
import (
"fmt"
"github.com/gocraft/work"
"github.com/gomodule/redigo/redis"
"github.com/google/uuid"
)
// Make a redis pool
var redisPool = &redis.Pool{
MaxActive: 5,
MaxIdle: 5,
Wait: true,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", ":6379")
},
}
// Make an enqueuer with a particular namespace
var enqueuer = work.NewEnqueuer("ChiefSend", redisPool)
type Context struct{
ShareID uuid.UUID
}
func ConfigurePool() {
// Make a new pool. Arguments:
// Context{} is a struct that will be the context for the request.
// 10 is the max concurrency
// "ChiefSend" is the Redis namespace
pool := work.NewWorkerPool(Context{}, 10, "ChiefSend", redisPool)
// Add middleware that will be executed for each job
pool.Middleware((*Context).FindShare)
pool.Middleware((*Context).Log)
pool.Job("DeleteShare", (*Context).DeleteShare)
pool.Start()
//signalChan := make(chan os.Signal, 1)
//signal.Notify(signalChan, os.Interrupt, os.Kill)
//<-signalChan
//pool.Stop()
}
func (c *Context) FindShare(job *work.Job, next work.NextMiddlewareFunc) error {
fmt.Println("FindShare (background job")
if _, ok := job.Args["ShareID"]; ok {
c.ShareID = uuid.MustParse(job.ArgString("ShareID"))
if err := job.ArgError(); err != nil {
return err
}
}
return next()
}
func (c *Context) Log(job *work.Job, next work.NextMiddlewareFunc) error {
fmt.Printf("Starting job: %s (%s)", job.Name, c.ShareID)
return next()
}
func (c *Context) DeleteShare(job *work.Job) error {
return nil // TODO
}
......@@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"github.com/google/uuid"
"log"
"os"
)
......@@ -18,6 +19,20 @@ var config = struct {
}
func main() {
SendMail(Share{
ID: uuid.MustParse("5713d228-a042-446d-a5e4-183b19fa832a"),
Name: "MailTest",
DownloadLimit: 100,
IsPublic: false,
IsTemporary: false,
Password: "test123",
Attachments: []Attachment{
{
Filename: "kekw.txt",
},
},
})
// test database connection
db, err := GetDatabase()
if err != nil {
......@@ -29,6 +44,7 @@ func main() {
// start
fmt.Println("Let's go!!!")
ConfigureRoutes()
ConfigurePool()
}
func PrettyPrint(i interface{}) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment