Skip to content

Commit d7957ac

Browse files
feat: add support for scheduled functions to Go client (#368)
* feat: add additional log fields * feat: add functions manifest logging * feat: add `scheduled_functions` log field * feat: pass `FunctionSchedules` to deploy * refactor: remove `manifest` log field * refactor: add variable
1 parent c445487 commit d7957ac

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

go/porcelain/deploy.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type DeployOptions struct {
9191

9292
files *deployFiles
9393
functions *deployFiles
94-
functionSchedules []models.FunctionSchedule
94+
functionSchedules []*models.FunctionSchedule
9595
}
9696

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

213213
options.files = files
214214

215-
functions, schedules, err := bundle(options.FunctionsDir, options.Observer)
215+
functions, schedules, err := bundle(ctx, options.FunctionsDir, options.Observer)
216216
if err != nil {
217217
if options.Observer != nil {
218218
options.Observer.OnFailedWalk()
@@ -238,10 +238,15 @@ func (n *Netlify) DoDeploy(ctx context.Context, options *DeployOptions, deploy *
238238
}
239239
}
240240

241+
if len(schedules) > 0 {
242+
deployFiles.FunctionSchedules = schedules
243+
}
244+
241245
l := context.GetLogger(ctx)
242246
l.WithFields(logrus.Fields{
243-
"site_id": options.SiteID,
244-
"deploy_files": len(options.files.Sums),
247+
"site_id": options.SiteID,
248+
"deploy_files": len(options.files.Sums),
249+
"scheduled_functions": len(schedules),
245250
}).Debug("Starting to deploy files")
246251
authInfo := context.GetAuthInfo(ctx)
247252

@@ -580,7 +585,7 @@ func walk(dir string, observer DeployObserver, useLargeMedia, ignoreInstallDirs
580585
return files, err
581586
}
582587

583-
func bundle(functionDir string, observer DeployObserver) (*deployFiles, []models.FunctionSchedule, error) {
588+
func bundle(ctx context.Context, functionDir string, observer DeployObserver) (*deployFiles, []*models.FunctionSchedule, error) {
584589
if functionDir == "" {
585590
return nil, nil, nil
586591
}
@@ -592,7 +597,7 @@ func bundle(functionDir string, observer DeployObserver) (*deployFiles, []models
592597
if err == nil {
593598
defer manifestFile.Close()
594599

595-
return bundleFromManifest(manifestFile, observer)
600+
return bundleFromManifest(ctx, manifestFile, observer)
596601
}
597602

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

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

644649
if err != nil {
645650
return nil, nil, err
646651
}
647652

653+
logger := context.GetLogger(ctx)
654+
logger.Debug("Found functions manifest file")
655+
648656
var manifest functionsManifest
649657

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

656-
schedules := make([]models.FunctionSchedule, 0, len(manifest.Functions))
664+
schedules := make([]*models.FunctionSchedule, 0, len(manifest.Functions))
657665
functions := newDeployFiles()
658666

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

672680
if function.Schedule != "" {
673-
schedules = append(schedules, models.FunctionSchedule{
681+
schedules = append(schedules, &models.FunctionSchedule{
674682
Cron: function.Schedule,
675683
Name: function.Name,
676684
})

go/porcelain/deploy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func TestUploadFiles_Cancelation(t *testing.T) {
203203
}
204204

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

208208
assert.Nil(t, err)
209209
assert.Equal(t, 3, len(functions.Files))
@@ -247,7 +247,7 @@ func TestBundleWithManifest(t *testing.T) {
247247
defer os.Remove(manifestPath)
248248
assert.Nil(t, err)
249249

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

252252
assert.Nil(t, err)
253253

0 commit comments

Comments
 (0)