Skip to content

feat: add support for scheduled functions to Go client #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions go/porcelain/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type DeployOptions struct {

files *deployFiles
functions *deployFiles
functionSchedules []models.FunctionSchedule
functionSchedules []*models.FunctionSchedule
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change to pointers here is just so that it matches the FunctionSchedules type from DeployFiles without doing any conversion.

}

type uploadError struct {
Expand Down Expand Up @@ -212,7 +212,7 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *

options.files = files

functions, schedules, err := bundle(options.FunctionsDir, options.Observer)
functions, schedules, err := bundle(ctx, options.FunctionsDir, options.Observer)
if err != nil {
if options.Observer != nil {
options.Observer.OnFailedWalk()
Expand All @@ -238,10 +238,15 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
}
}

if len(schedules) > 0 {
deployFiles.FunctionSchedules = schedules
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where we pass the schedules to the deploy options.

}

l := context.GetLogger(ctx)
l.WithFields(logrus.Fields{
"site_id": options.SiteID,
"deploy_files": len(options.files.Sums),
"site_id": options.SiteID,
"deploy_files": len(options.files.Sums),
"scheduled_functions": len(schedules),
}).Debug("Starting to deploy files")
authInfo := context.GetAuthInfo(ctx)

Expand Down Expand Up @@ -580,7 +585,7 @@ func walk(dir string, observer DeployObserver, useLargeMedia, ignoreInstallDirs
return files, err
}

func bundle(functionDir string, observer DeployObserver) (*deployFiles, []models.FunctionSchedule, error) {
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bundle and bundleFromManifest are now receiving the context so that they can acquire a logger and print a debug message.

if functionDir == "" {
return nil, nil, nil
}
Expand All @@ -592,7 +597,7 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, []models
if err == nil {
defer manifestFile.Close()

return bundleFromManifest(manifestFile, observer)
return bundleFromManifest(ctx, manifestFile, observer)
}

functions := newDeployFiles()
Expand Down Expand Up @@ -638,13 +643,16 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, []models
return functions, nil, nil
}

func bundleFromManifest(manifestFile *os.File, observer DeployObserver) (*deployFiles, []models.FunctionSchedule, error) {
func bundleFromManifest(ctx context.Context, manifestFile *os.File, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
manifestBytes, err := ioutil.ReadAll(manifestFile)

if err != nil {
return nil, nil, err
}

logger := context.GetLogger(ctx)
logger.Debug("Found functions manifest file")

var manifest functionsManifest

err = json.Unmarshal(manifestBytes, &manifest)
Expand All @@ -653,7 +661,7 @@ func bundleFromManifest(manifestFile *os.File, observer DeployObserver) (*deploy
return nil, nil, fmt.Errorf("malformed functions manifest file: %w", err)
}

schedules := make([]models.FunctionSchedule, 0, len(manifest.Functions))
schedules := make([]*models.FunctionSchedule, 0, len(manifest.Functions))
functions := newDeployFiles()

for _, function := range manifest.Functions {
Expand All @@ -670,7 +678,7 @@ func bundleFromManifest(manifestFile *os.File, observer DeployObserver) (*deploy
}

if function.Schedule != "" {
schedules = append(schedules, models.FunctionSchedule{
schedules = append(schedules, &models.FunctionSchedule{
Cron: function.Schedule,
Name: function.Name,
})
Expand Down
4 changes: 2 additions & 2 deletions go/porcelain/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestUploadFiles_Cancelation(t *testing.T) {
}

func TestBundle(t *testing.T) {
functions, schedules, err := bundle("../internal/data", mockObserver{})
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})

assert.Nil(t, err)
assert.Equal(t, 3, len(functions.Files))
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestBundleWithManifest(t *testing.T) {
defer os.Remove(manifestPath)
assert.Nil(t, err)

functions, schedules, err := bundle("../internal/data", mockObserver{})
functions, schedules, err := bundle(gocontext.Background(), "../internal/data", mockObserver{})

assert.Nil(t, err)

Expand Down