Skip to content
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

Introduce development mode #543

Merged
merged 3 commits into from
Jun 24, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added support for ecs style validation for dataset fields. [#520](https://github.com/elastic/package-registry/pull/520)
* Use BasePackage for search output data. [#529](https://github.com/elastic/package-registry/pull/529)
* Add support for owner field in package manifest. [#536](https://github.com/elastic/package-registry/pull/536)
* Introduce development mode. [#543](https://github.com/elastic/package-registry/pull/543)

### Deprecated

Expand Down
19 changes: 19 additions & 0 deletions devmode/devmode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.

package devmode

import "log"

var enabled bool

func Enable() {
log.Println("Enable development mode")

enabled = true
}

func Enabled() bool {
return enabled
}
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

ucfgYAML "github.com/elastic/go-ucfg/yaml"

"github.com/elastic/package-registry/devmode"
"github.com/elastic/package-registry/util"
)

Expand All @@ -42,6 +43,7 @@ var (
CacheTimeSearch: 10 * time.Minute,
CacheTimeCategories: 10 * time.Minute,
CacheTimeCatchAll: 10 * time.Minute,
DevMode: false,
}
)

Expand All @@ -57,6 +59,8 @@ type Config struct {
CacheTimeSearch time.Duration `config:"cache_time.search"`
CacheTimeCategories time.Duration `config:"cache_time.categories"`
CacheTimeCatchAll time.Duration `config:"cache_time.catch_all"`

DevMode bool `config:"dev_mode"`
}

func main() {
Expand All @@ -68,6 +72,11 @@ func main() {
packagesBasePaths := getPackagesBasePaths(config)
ensurePackagesAvailable(packagesBasePaths)

// If config.DevMode is set, the service will not cache package content.
if config.DevMode {
devmode.Enable()
}

// If -dry-run=true is set, service stops here after validation
if dryRun {
return
Expand Down
8 changes: 5 additions & 3 deletions util/packages.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,19 @@ import (
"path/filepath"
"strings"

"github.com/elastic/package-registry/devmode"

"github.com/pkg/errors"
)

var packageList []Package

// GetPackages returns a slice with all existing packages.
// The list is stored in memory and on the second request directly
// served from memory. This assumes chnages to packages only happen on restart.
// The list is stored in memory and on the second request directly served from memory.
// This assumes changes to packages only happen on restart (unless development mode is enabled).
// Caching the packages request many file reads every time this method is called.
func GetPackages(packagesBasePaths []string) ([]Package, error) {
if packageList != nil {
if !devmode.Enabled() && packageList != nil {
return packageList, nil
}

Expand Down