Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neoppod
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Levin Zimmermann
neoppod
Commits
ed518ca8
Commit
ed518ca8
authored
Jan 18, 2021
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
go/internal/xcontext/task: New package (draft)
Package task provides primitives to track tasks via contexts.
parent
ce89d01a
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
94 additions
and
0 deletions
+94
-0
go/internal/xcontext/task/task.go
go/internal/xcontext/task/task.go
+94
-0
No files found.
go/internal/xcontext/task/task.go
0 → 100644
View file @
ed518ca8
// Copyright (C) 2017-2021 Nexedi SA and Contributors.
// Kirill Smelkov <kirr@nexedi.com>
//
// This program is free software: you can Use, Study, Modify and Redistribute
// it under the terms of the GNU General Public License version 3, or (at your
// option) any later version, as published by the Free Software Foundation.
//
// You can also Link and Combine this program with other software covered by
// the terms of any of the Free Software licenses or any of the Open Source
// Initiative approved licenses and Convey the resulting work. Corresponding
// source of such a combination shall include the source code for all other
// software used.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//
// See COPYING file for full licensing terms.
// See https://www.nexedi.com/licensing for rationale and options.
// Package task provides primitives to track tasks via contexts.
package
task
import
(
"context"
"fmt"
"lab.nexedi.com/kirr/go123/xerr"
)
// Task represents currently running operation.
type
Task
struct
{
parent
*
Task
name
string
}
func
(
t
*
Task
)
Parent
()
*
Task
{
return
t
.
parent
}
func
(
t
*
Task
)
Name
()
string
{
return
t
.
name
}
type
taskKey
struct
{}
// Running creates new task and returns new context with that task set to current.
func
Running
(
ctx
context
.
Context
,
name
string
)
context
.
Context
{
return
context
.
WithValue
(
ctx
,
taskKey
{},
&
Task
{
parent
:
Current
(
ctx
),
name
:
name
})
}
// Runningf is Running cousin with formatting support.
func
Runningf
(
ctx
context
.
Context
,
format
string
,
argv
...
interface
{})
context
.
Context
{
return
Running
(
ctx
,
fmt
.
Sprintf
(
format
,
argv
...
))
}
// Current returns current task represented by context.
//
// if there is no current task - it returns nil.
func
Current
(
ctx
context
.
Context
)
*
Task
{
task
,
_
:=
ctx
.
Value
(
taskKey
{})
.
(
*
Task
)
return
task
}
// ErrContext adds current task name to error on error return.
//
// To work as intended it should be called under defer like this:
//
// func myfunc(ctx, ...) (..., err error) {
// ctx = task.Running("doing something")
// defer task.ErrContext(&err, ctx)
// ...
//
// Please see lab.nexedi.com/kirr/go123/xerr.Context for semantic details.
func
ErrContext
(
errp
*
error
,
ctx
context
.
Context
)
{
task
:=
Current
(
ctx
)
if
task
==
nil
{
return
}
xerr
.
Context
(
errp
,
task
.
name
)
}
// String returns string representing whole operational stack.
//
// For example if task "c" is running under task "b" which in turn is running
// under task "a" - the operational stack will be "a: b: c".
//
// nil Task is represented as "".
func
(
t
*
Task
)
String
()
string
{
if
t
==
nil
{
return
""
}
prefix
:=
t
.
parent
.
String
()
if
prefix
!=
""
{
prefix
+=
": "
}
return
prefix
+
t
.
name
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment