-
-
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.
Add dependency caching in Docker. Add Dockerfile linter hadoling. Add pre-commit. Use cron instead of spamming on delivery server. Add a test that checks if the connection is restored in case of interruption.
- Loading branch information
Showing
12 changed files
with
266 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,7 @@ | ||
.dockerignore | ||
.gitignore | ||
.idea/ | ||
.github/ | ||
docker-compose.yml | ||
Dockerfile | ||
README.md |
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,2 @@ | ||
ignored: | ||
- DL3007 |
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,30 @@ | ||
fail_fast: false | ||
|
||
repos: | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v4.3.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: mixed-line-ending | ||
args: [ '--fix=lf' ] | ||
- id: check-yaml | ||
- id: check-json | ||
- id: check-added-large-files | ||
- id: pretty-format-json | ||
args: [ '--autofix', '--no-sort-keys' ] | ||
|
||
- repo: https://github.com/golangci/golangci-lint | ||
rev: v1.46.2 | ||
hooks: | ||
- id: golangci-lint | ||
|
||
- repo: https://github.com/hadolint/hadolint | ||
rev: v2.10.0 | ||
hooks: | ||
- id: hadolint | ||
|
||
- repo: https://github.com/segmentio/golines | ||
rev: v0.10.0 | ||
hooks: | ||
- id: golines |
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
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
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
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,83 @@ | ||
package copier | ||
|
||
import ( | ||
"fmt" | ||
"github.com/robfig/cron/v3" | ||
"github.com/rs/zerolog/log" | ||
"time" | ||
) | ||
|
||
const ( | ||
readableTimeLayout = "02.01.2006 15:04:05" | ||
) | ||
|
||
type Runner struct { | ||
copier *StreamCopier | ||
} | ||
|
||
func NewRunner(copier *StreamCopier) *Runner { | ||
return &Runner{ | ||
copier: copier, | ||
} | ||
} | ||
|
||
func (r *Runner) ScheduleRecording( | ||
cronString string, | ||
duration time.Duration, | ||
url string, | ||
outputFunc GetOutputFunc, | ||
delay time.Duration, | ||
) error { | ||
c := cron.New() | ||
entryID, err := c.AddFunc(cronString, func() { r.record(url, outputFunc, duration, delay) }) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.Start() | ||
|
||
entry := getEntryByID(c, entryID) | ||
if entry == nil { | ||
return fmt.Errorf("entry with id %v not found", entryID) | ||
} | ||
|
||
r.copier.logger.Info(). | ||
Str("url", url). | ||
Str("delay", delay.String()). | ||
Str("next_call", entry.Next.Format(readableTimeLayout)). | ||
Str("duration", duration.String()). | ||
Msg("Starting listening") | ||
|
||
return nil | ||
} | ||
|
||
func (r *Runner) record( | ||
url string, | ||
outputFunc GetOutputFunc, | ||
duration time.Duration, | ||
delay time.Duration, | ||
) { | ||
start := time.Now() | ||
finish := start.Add(duration) | ||
for { | ||
log.Debug().Msg("RUN") | ||
if time.Now().After(finish) { | ||
return | ||
} | ||
if err := r.copier.CopyStream(url, outputFunc); err != nil && err != ErrStreamClosed { | ||
r.copier.logger.Error().Err(err).Msg("Error copying stream") | ||
return | ||
} | ||
time.Sleep(delay) | ||
} | ||
} | ||
|
||
func getEntryByID(c *cron.Cron, id cron.EntryID) *cron.Entry { | ||
for _, entry := range c.Entries() { | ||
if entry.ID == id { | ||
return &entry | ||
} | ||
} | ||
|
||
return nil | ||
} |
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
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
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.