Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(517)

Unified Diff: mmutex/cmd/mmutex/main.go

Issue 2980603003: Flesh out the mmutex binary wrapper and test for existence of lockfile (Closed)
Patch Set: Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mmutex/cmd/mmutex/main.go
diff --git a/mmutex/cmd/mmutex/main.go b/mmutex/cmd/mmutex/main.go
index d61b202902035f014f3edb4f856c6365c1465583..a661462489e381c053c79c79716f75b9f8a2d315 100644
--- a/mmutex/cmd/mmutex/main.go
+++ b/mmutex/cmd/mmutex/main.go
@@ -4,6 +4,68 @@
package main
+import (
+ "fmt"
+ "os"
+
+ "github.com/maruel/subcommands"
+
+ "github.com/luci/luci-go/mmutex/lib"
+)
+
+// TODO(charliea): Compute this path from $MMUTEX_LOCK_DIR rather than using a constant.
+const lockFilePath = "/tmp/lock"
+
+var application = &subcommands.DefaultApplication{
+ Name: "mmutex",
+ Title: `'Maintenance Mutex' - Global mutex to isolate maintenance tasks.
+
+mmutex is a command line tool that helps prevent maintenance tasks from running
+during user tasks. The tool does this by way of a global lock file that users
+must acquire before running their tasks.
+
+Clients can use this tool to request that their task be run with one of two
+types of access to the system:
+
+ * Exclusive access guarantees that no other callers have any access
+ exclusive or shared) to the resource while the specified command is run.
+ * Shared access guarantees that only other callers with shared access
+ will have access to the resource while the specified command is run.
+
+In short, exclusive access guarantees a task is run alone, while shared access
+tasks may be run alongside other shared access tasks.
+
+The source for mmutex lives at:
+ https://github.com/luci/luci-go/tree/master/mmutex`,
+ Commands: []*subcommands.Command{
+ cmdExclusiveLock,
+ subcommands.CmdHelp,
+ },
+}
+
+var cmdExclusiveLock = &subcommands.Command{
iannucci 2017/07/12 01:34:59 I would recommend putting each subcommand in its o
charliea (OOO until 10-5) 2017/07/12 02:17:17 Done.
+ UsageLine: "exclusive -- <command>",
+ ShortDesc: "acquires an exclusive lock before running the command",
+ CommandRun: func() subcommands.CommandRun {
+ return &exclusiveLockRun{}
+ },
+}
+
+type exclusiveLockRun struct {
+ subcommands.CommandRunBase
+}
+
+func (c *exclusiveLockRun) Run(a subcommands.Application, args []string, env subcommands.Env) int {
+ // TODO(charliea): Make sure that `args` has length greater than zero.
+
+ if err := lib.AcquireExclusiveLock(lockFilePath); err != nil {
+ fmt.Fprintf(os.Stderr, "error acquiring exclusive lock: %s\n", err)
+ return 1
+ }
+
+ return 0
+}
+
func main() {
- // TODO(charliea): Write the command line interface.
+ os.Exit(subcommands.Run(application, nil))
}
« no previous file with comments | « mmutex/cmd/mmutex/.gitignore ('k') | mmutex/lib/flock.go » ('j') | mmutex/lib/flock.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698