Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #155 from snickers/move-setupjob
Browse files Browse the repository at this point in the history
pipeline: move SetupJob from downloaders pkg (close #151)
  • Loading branch information
flavioribeiro committed Dec 9, 2016
2 parents 676498b + d300745 commit 77923b3
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 138 deletions.
45 changes: 0 additions & 45 deletions downloaders/downloader.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
package downloaders

import (
"net/url"
"path"
"strings"

"code.cloudfoundry.org/lager"
"github.com/flavioribeiro/gonfig"
"github.com/snickers/snickers/db"
"github.com/snickers/snickers/helpers"
"github.com/snickers/snickers/types"
)

// DownloadFunc is a function type for the multiple
Expand All @@ -27,44 +23,3 @@ func GetDownloadFunc(jobSource string) DownloadFunc {

return HTTPDownload
}

// SetupJob is responsible for set the initial state for a given
// job before starting. It sets local source and destination
// paths and the final destination as well.
func SetupJob(jobID string, dbInstance db.Storage, config gonfig.Gonfig) (types.Job, error) {
job, err := dbInstance.RetrieveJob(jobID)
if err != nil {
return types.Job{}, err
}

localSource, err := helpers.GetLocalSourcePath(config, job.ID)
if err != nil {
return types.Job{}, err
}
job.LocalSource = localSource + path.Base(job.Source)

job.LocalDestination, err = helpers.GetLocalDestination(config, dbInstance, jobID)
if err != nil {
return types.Job{}, err
}

u, err := url.Parse(job.Destination)
if err != nil {
return types.Job{}, err
}
outputFilename, err := helpers.GetOutputFilename(dbInstance, jobID)
if err != nil {
return types.Job{}, err
}
u.Path = path.Join(u.Path, outputFilename)
job.Destination = u.String()

job.Status = types.JobDownloading
job.Details = "0%"
job, err = dbInstance.UpdateJob(job.ID, job)
if err != nil {
return types.Job{}, err
}

return job, nil
}
26 changes: 7 additions & 19 deletions downloaders/downloaders_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ var _ = Describe("Downloaders", func() {
ContainSubstring("No filename could be determined"),
ContainSubstring("The AWS Access Key Id you provided does not exist in our records")))
})

It("Should set the local source and local destination on Job", func() {
dbInstance.StoreJob(exampleJob)
downloader(logger, cfg, dbInstance, exampleJob.ID)
changedJob, _ := dbInstance.RetrieveJob("123")
swapDir, _ := cfg.GetString("SWAP_DIRECTORY", "")

sourceExpected := swapDir + "123/src/source_here.mp4"
Expect(changedJob.LocalSource).To(Equal(sourceExpected))

destinationExpected := swapDir + "123/dst/source_here_240p.mp4"
Expect(changedJob.LocalDestination).To(Equal(destinationExpected))
})
}

Context("HTTP Downloader", func() {
Expand Down Expand Up @@ -113,12 +100,13 @@ var _ = Describe("Downloaders", func() {
BeforeEach(func() {
downloader = S3Download
exampleJob = types.Job{
ID: "123",
Source: "http://AWSKEY:AWSSECRET@BUCKET.s3.amazonaws.com/source_here.mp4",
Destination: "s3://user@pass:/bucket/",
Preset: types.Preset{Name: "240p", Container: "mp4"},
Status: types.JobCreated,
Details: "",
ID: "123",
Source: "http://AWSKEY:AWSSECRET@BUCKET.s3.amazonaws.com/source_here.mp4",
Destination: "s3://user@pass:/bucket/",
Preset: types.Preset{Name: "240p", Container: "mp4"},
Status: types.JobCreated,
Details: "",
LocalDestination: "/tmp/output_here.mp4",
}
})

Expand Down
3 changes: 1 addition & 2 deletions downloaders/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ func FTPDownload(logger lager.Logger, config gonfig.Gonfig, dbInstance db.Storag
log.Info("start", lager.Data{"job": jobID})
defer log.Info("finished")

job, err := SetupJob(jobID, dbInstance, config)
job, err := dbInstance.RetrieveJob(jobID)
if err != nil {
log.Error("setting-up-job", err)
return err
}

Expand Down
3 changes: 1 addition & 2 deletions downloaders/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ func HTTPDownload(logger lager.Logger, config gonfig.Gonfig, dbInstance db.Stora
log.Info("start", lager.Data{"job": jobID})
defer log.Info("finished")

job, err := SetupJob(jobID, dbInstance, config)
job, err := dbInstance.RetrieveJob(jobID)
if err != nil {
log.Error("setting-up-job", err)
return err
}

Expand Down
68 changes: 0 additions & 68 deletions downloaders/http_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions downloaders/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ func S3Download(logger lager.Logger, config gonfig.Gonfig, dbInstance db.Storage
log.Info("start", lager.Data{"job": jobID})
defer log.Info("finished")

job, err := SetupJob(jobID, dbInstance, config)
job, err := dbInstance.RetrieveJob(jobID)
if err != nil {
log.Error("setting-up-job", err)
return err
}

Expand Down
52 changes: 52 additions & 0 deletions pipeline/pipeline.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package pipeline

import (
"net/url"
"os"
"path"

"code.cloudfoundry.org/lager"
"github.com/flavioribeiro/gonfig"
"github.com/snickers/snickers/db"
"github.com/snickers/snickers/downloaders"
"github.com/snickers/snickers/encoders"
"github.com/snickers/snickers/helpers"
"github.com/snickers/snickers/types"
"github.com/snickers/snickers/uploaders"
)
Expand All @@ -22,6 +25,14 @@ func StartJob(logger lager.Logger, config gonfig.Gonfig, dbInstance db.Storage,
})
defer log.Info("finished")

log.Info("setup")
newJob, err := SetupJob(job.ID, dbInstance, config)
job = *newJob
if err != nil {
log.Error("setup-job failed", err)
return
}

log.Info("downloading")
downloadFunc := downloaders.GetDownloadFunc(job.Source)
if err := downloadFunc(log, config, dbInstance, job.ID); err != nil {
Expand Down Expand Up @@ -77,3 +88,44 @@ func CleanSwap(dbInstance db.Storage, jobID string) error {
err = os.RemoveAll(job.LocalDestination)
return err
}

// SetupJob is responsible for set the initial state for a given
// job before starting. It sets local source and destination
// paths and the final destination as well.
func SetupJob(jobID string, dbInstance db.Storage, config gonfig.Gonfig) (*types.Job, error) {
job, err := dbInstance.RetrieveJob(jobID)
if err != nil {
return nil, err
}

localSource, err := helpers.GetLocalSourcePath(config, job.ID)
if err != nil {
return nil, err
}
job.LocalSource = localSource + path.Base(job.Source)

job.LocalDestination, err = helpers.GetLocalDestination(config, dbInstance, jobID)
if err != nil {
return nil, err
}

u, err := url.Parse(job.Destination)
if err != nil {
return nil, err
}
outputFilename, err := helpers.GetOutputFilename(dbInstance, jobID)
if err != nil {
return nil, err
}
u.Path = path.Join(u.Path, outputFilename)
job.Destination = u.String()

job.Status = types.JobDownloading
job.Details = "0%"
job, err = dbInstance.UpdateJob(job.ID, job)
if err != nil {
return nil, err
}

return &job, nil
}
25 changes: 25 additions & 0 deletions pipeline/pipeline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ var _ = Describe("Pipeline", func() {
dbInstance.ClearDatabase()
})

Context("SetupJob function", func() {
It("Should set the local source and local destination on Job", func() {
exampleJob := types.Job{
ID: "123",
Source: "http://flv.io/source_here.mp4",
Destination: "s3://user@pass:/bucket/",
Preset: types.Preset{Name: "240p", Container: "mp4"},
Status: types.JobCreated,
Details: "",
}

dbInstance.StoreJob(exampleJob)
SetupJob(exampleJob.ID, dbInstance, cfg)
changedJob, _ := dbInstance.RetrieveJob("123")

swapDir, _ := cfg.GetString("SWAP_DIRECTORY", "")

sourceExpected := swapDir + "123/src/source_here.mp4"
Expect(changedJob.LocalSource).To(Equal(sourceExpected))

destinationExpected := swapDir + "123/dst/source_here_240p.mp4"
Expect(changedJob.LocalDestination).To(Equal(destinationExpected))
})
})

Context("Pipeline", func() {
It("Should get the HTTPDownload function if source is HTTP", func() {
jobSource := "http://flv.io/KailuaBeach.mp4"
Expand Down

0 comments on commit 77923b3

Please sign in to comment.