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

Feature/context for jobs #32

Merged
merged 33 commits into from
Oct 18, 2017
Merged

Conversation

interlock
Copy link
Contributor

@interlock interlock commented Oct 3, 2017

Allows for jobs that take and call with a context as described in #25

Todo:

  • Context caller for Deps as a separate func helper
  • Test a simple Timeout context
  • split dup code for acceptable func types out
  • wrap calling job func's to deal with error/context uniformly

closes #25

natefinch and others added 9 commits October 1, 2017 22:24
This commit adds helper functions that should be commonly needed
by many magefiles. It includes command execution handlers as well
as error handlers.

This commit also removes the only two non-stdlib dependencies in
the codebase.  Since mage will likely be vendored into a repo,
it would be nice to avoid cluttering the vendor directory with
dependencies that are only for the build script.
Moved magefile template to its own file
@natefinch
Copy link
Member

gah, sorry for the conflicts.

@interlock
Copy link
Contributor Author

Ya, I saw it coming from that other PR. Still working out handling timeouts locally.

Copy link
Member

@natefinch natefinch left a comment

Choose a reason for hiding this comment

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

Ran out of time to do a proper review, will do more tomorow


func Timeout(ctx context.Context) {
fmt.Printf("timeout start\n")
time.Sleep(1000000000000)
Copy link
Member

Choose a reason for hiding this comment

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

time.sleep takes a time.Duration, you could make this easier to read by just doing time.Sleep(time.Hour) or something :)

Copy link
Contributor Author

@interlock interlock Oct 10, 2017

Choose a reason for hiding this comment

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

It does and "oops!" did not intend to commit that... I'll remove it. I use that for my manual test cases.

mage/main.go Outdated
@@ -35,8 +35,8 @@ const initFile = "magefile.go"

var (
force, verbose, list, help, mageInit, keep, showVersion bool

timestamp, commitHash, gitTag string
timeout int
Copy link
Member

Choose a reason for hiding this comment

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

you can put spaces between the lines os the types aren't so crazily right justified :)

Removed template defered recover in preference to the runTarget func
that does the same.
Fixed many, but not all, gofmt errors in the template
@interlock
Copy link
Contributor Author

This goes as far as I can take it right now.

  • Parse updated to spit out some scaffold around running targets wrapped with context
  • template has a new function, runTarget that handles the defer recover and actual ctx channel handling for errors. Brings the extraction of a StatusCode error to one spot as well.
  • There is a single test for expiring a context on timeout. We could have more complicated tests, but I figure we can make those when someone does something complicated.
  • I lightly updated the site docs to indicate there is now a -t <seconds> flag to set a timeout
  • mg/runtime has a Context function which returns the same Context everytime based on if we have a -t or not.

@interlock interlock changed the title [WIP] Feature/context for jobs Feature/context for jobs Oct 14, 2017
Copy link
Member

@natefinch natefinch left a comment

Choose a reason for hiding this comment

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

Looks great, a few minor nitpicks

// Manage your deps, or running package managers.
func InstallDeps(ctx context.Context) error {
fmt.Println("Installing Deps...")
cmd := exec.Command("go", "get", "github.com/stretchr/piglatin")
Copy link
Member

Choose a reason for hiding this comment

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

there's a version of exec.Command that takes a context, that would be a perfect example for using this new feature: https://golang.org/pkg/os/exec/#CommandContext

mage/main.go Outdated
@@ -46,6 +46,7 @@ func init() {
flag.BoolVar(&help, "h", false, "show this help")
flag.BoolVar(&mageInit, "init", false, "create a starting template if no mage files exist")
flag.BoolVar(&keep, "keep", false, "keep intermediate mage files around after running")
flag.IntVar(&timeout, "t", 0, "timeout in seconds, Default (0) no timeout")
Copy link
Member

Choose a reason for hiding this comment

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

if you make this a flag.DurationVar, it'll convert a string right to a duration, and then you can do -t 1m5s for example

@@ -0,0 +1,3 @@
// Place your settings in this file to overwrite default and user settings.
{
Copy link
Member

Choose a reason for hiding this comment

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

this should probably be .gitignored

mage/main.go Outdated
@@ -40,6 +40,8 @@ const mainfile = "mage_output_file.go"
const initFile = "magefile.go"

var (
force, verbose, list, help, mageInit, keep, showVersion bool
Copy link
Member

Choose a reason for hiding this comment

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

this can get removed.... with switch to a standalone parse function, we don't have global flag variables anymore.

@@ -31,7 +31,11 @@ func testmain(m *testing.M) int {
log.Fatal(err)
}
if err := os.Mkdir(dir, 0700); err != nil {
log.Fatal(err)
if os.IsExist(err) {
os.RemoveAll(fmt.Sprintf("%s/*", dir))
Copy link
Member

Choose a reason for hiding this comment

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

RemoveAll removes a directory. globbing (*) doesn't generally work in go functions anyway. if you want to just delete everything inside the directory, you'll either have to loop through, or better, just delete the whole thing and recreate it.

mg/deps.go Outdated
fn = f
default:
panic("attempted to add a dep that did not match required type")
Copy link
Member

Choose a reason for hiding this comment

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

can you slap a
// should be impossible, since we already checked this
above this?

mg/runtime.go Outdated
var ctx context.Context
var ctxCancel func()

func GetContext() (context.Context, func()) {
Copy link
Member

Choose a reason for hiding this comment

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

Can you call this just Context()? GetFoo is sorta non-idiomatic.

mg/runtime.go Outdated
if os.Getenv("MAGEFILE_TIMEOUT") != "" {
timeout, err := strconv.Atoi(os.Getenv("MAGEFILE_TIMEOUT"))
if err != nil {
fmt.Println("timeout must be a number >= 0")
Copy link
Member

Choose a reason for hiding this comment

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

speaking of which, check for negative numbers here

parse/parse.go Outdated
Comment string
}

func (f Function) TemplateString() string {
Copy link
Member

Choose a reason for hiding this comment

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

doc comment here please

}

// DefaultName
if strings.Compare(info.DefaultName, "ReturnsError") != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

if info.DefaultName != "ReturnsError" {

Copy link
Member

@natefinch natefinch left a comment

Choose a reason for hiding this comment

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

two tiny tweaks and we're home free!

mage/main.go Outdated
@@ -368,7 +367,7 @@ func RunCompiled(inv Invocation, exePath string) int {
c.Env = append(c.Env, "MAGEFILE_HELP=1")
}
if inv.Timeout > 0 {
c.Env = append(c.Env, fmt.Sprintf("MAGEFILE_TIMEOUT=%d", inv.Timeout))
c.Env = append(c.Env, fmt.Sprintf("MAGEFILE_TIMEOUT=%dns", inv.Timeout))
Copy link
Member

Choose a reason for hiding this comment

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

you can probably just do inv.Timeout.String()

@@ -32,7 +33,7 @@ func testmain(m *testing.M) int {
}
if err := os.Mkdir(dir, 0700); err != nil {
if os.IsExist(err) {
os.RemoveAll(fmt.Sprintf("%s/*", dir))
os.RemoveAll(fmt.Sprintf("%s", dir))
Copy link
Member

Choose a reason for hiding this comment

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

can remove the sprintf now :)

@natefinch natefinch merged commit df36722 into magefile:master Oct 18, 2017
@interlock interlock deleted the feature/context branch October 18, 2017 03:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature Request: automatic insertion of a job-scoped context
2 participants