Added "AUTO_RECEIVE_SCHEDULE" parameter to docker-compose.yml file

* signal-cli recommends to call "receive" on a regular basis. In case
  "receive" is not already called periodically by the user application,
  add the option to invoke it at a specific schedule from within the
  docker container.

  The "AUTO_RECEIVE_SCHEDULE" parameter takes a cron schedule expression
  and calls "receive" at the given time.

see #129
This commit is contained in:
Bernhard B
2021-05-11 18:35:24 +02:00
parent 5153de7319
commit 887eff056b
5 changed files with 75 additions and 11 deletions

View File

@@ -13,6 +13,7 @@ require (
github.com/gofrs/uuid v3.3.0+incompatible
github.com/h2non/filetype v1.1.0
github.com/mailru/easyjson v0.7.1 // indirect
github.com/robfig/cron/v3 v3.0.1
github.com/sirupsen/logrus v1.6.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14

View File

@@ -94,6 +94,9 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=

View File

@@ -1,21 +1,24 @@
package main
import (
"encoding/json"
"flag"
"strings"
"net/http"
"os"
"path/filepath"
"io/ioutil"
"github.com/bbernhard/signal-cli-rest-api/api"
_ "github.com/bbernhard/signal-cli-rest-api/docs"
"github.com/bbernhard/signal-cli-rest-api/utils"
"github.com/gin-gonic/gin"
"github.com/robfig/cron/v3"
log "github.com/sirupsen/logrus"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
"github.com/bbernhard/signal-cli-rest-api/api"
"github.com/bbernhard/signal-cli-rest-api/utils"
_ "github.com/bbernhard/signal-cli-rest-api/docs"
"os"
)
// @title Signal Cli REST API
// @version 1.0
// @description This is the Signal Cli REST API documentation.
@@ -32,10 +35,10 @@ import (
// @tag.name Messages
// @tag.description Send and Receive Signal Messages.
// @tag.name Attachments
// @tag.name Attachments
// @tag.description List and Delete Attachments.
// @tag.name Profiles
// @tag.name Profiles
// @tag.description Update Profile.
// @tag.name Identities
@@ -151,6 +154,57 @@ func main() {
swaggerUrl := ginSwagger.URL("http://127.0.0.1:" + string(swaggerPort) + "/swagger/doc.json")
router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler, swaggerUrl))
autoReceiveSchedule := utils.GetEnv("AUTO_RECEIVE_SCHEDULE", "")
if autoReceiveSchedule != "" {
p := cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow)
schedule, err := p.Parse(autoReceiveSchedule)
if err != nil {
log.Fatal("Invalid AUTO_RECEIVE_SCHEDULE: ", err.Error())
}
c := cron.New()
c.Schedule(schedule, cron.FuncJob(func() {
err := filepath.Walk(*signalCliConfig, func(path string, info os.FileInfo, err error) error {
filename := filepath.Base(path)
if strings.HasPrefix(filename, "+") && info.Mode().IsRegular() {
log.Debug("AUTO_RECEIVE_SCHEDULE: Calling receive for number ", filename)
resp, err := http.Get("http://127.0.0.1:8080/v1/receive/"+filename)
if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", filename, ": ", err.Error())
}
if resp.StatusCode != 200 {
jsonResp, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't read json response: ", err.Error())
return nil
}
type ReceiveResponse struct {
Error string `json:"error"`
}
var receiveResponse ReceiveResponse
err = json.Unmarshal(jsonResp, &receiveResponse)
if err != nil {
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't parse json response: ", err.Error())
return nil
}
log.Error("AUTO_RECEIVE_SCHEDULE: Couldn't call receive for number ", filename, ": ", receiveResponse)
}
}
return nil
})
if err != nil {
log.Fatal("Couldn't get registered numbers")
}
}))
c.Start()
}
router.Run()
}