2013-12-13 23:16:56 +04:00
// Copyright 2013, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/ *
vtworker is the main program to run a worker job .
It has two modes : single command or interactive .
- in single command , it will start the job passed in from the command line ,
and exit .
- in interactive mode , use a web browser to start an action .
* /
package main
import (
"flag"
2013-12-14 04:08:10 +04:00
"fmt"
2013-12-13 23:16:56 +04:00
"os"
"time"
2013-12-14 04:08:10 +04:00
log "github.com/golang/glog"
2015-06-24 12:40:09 +03:00
"github.com/youtube/vitess/go/exit"
2014-08-14 22:46:09 +04:00
"github.com/youtube/vitess/go/vt/logutil"
2013-12-13 23:16:56 +04:00
"github.com/youtube/vitess/go/vt/servenv"
"github.com/youtube/vitess/go/vt/topo"
"github.com/youtube/vitess/go/vt/worker"
)
var (
2015-06-23 14:59:39 +03:00
cell = flag . String ( "cell" , "" , "cell to pick servers from" )
2015-06-22 21:37:46 +03:00
commandDisplayInterval = flag . Duration ( "command_display_interval" , time . Second , "Interval between each status update when vtworker is executing a single command from the command line" )
2013-12-13 23:16:56 +04:00
)
2014-06-17 02:27:45 +04:00
func init ( ) {
servenv . RegisterDefaultFlags ( )
2015-06-30 14:55:07 +03:00
logger := logutil . NewConsoleLogger ( )
flag . CommandLine . SetOutput ( logutil . NewLoggerWriter ( logger ) )
flag . Usage = func ( ) {
logger . Printf ( "Usage: %s [global parameters] command [command parameters]\n" , os . Args [ 0 ] )
logger . Printf ( "\nThe global optional parameters are:\n" )
flag . PrintDefaults ( )
logger . Printf ( "\nThe commands are listed below, sorted by group. Use '%s <command> -h' for more help.\n\n" , os . Args [ 0 ] )
worker . PrintAllCommands ( logger )
}
2014-06-17 02:27:45 +04:00
}
2013-12-14 04:08:10 +04:00
var (
2015-06-23 15:24:01 +03:00
wi * worker . Instance
2013-12-14 04:08:10 +04:00
)
2013-12-13 23:16:56 +04:00
func main ( ) {
2015-06-24 12:40:09 +03:00
defer exit . Recover ( )
2015-06-22 21:37:46 +03:00
setUsage ( )
2013-12-13 23:16:56 +04:00
flag . Parse ( )
args := flag . Args ( )
servenv . Init ( )
defer servenv . Close ( )
ts := topo . GetServer ( )
defer topo . CloseServers ( )
2015-06-23 15:24:01 +03:00
wi = worker . NewInstance ( ts , * cell , 30 * time . Second , * commandDisplayInterval )
2015-06-23 14:59:39 +03:00
wi . InstallSignalHandlers ( )
wi . InitStatusHandling ( )
2015-06-22 21:37:46 +03:00
2013-12-13 23:16:56 +04:00
if len ( args ) == 0 {
2015-01-13 02:46:09 +03:00
// In interactive mode, initialize the web UI to choose a command.
2015-06-22 21:37:46 +03:00
wi . InitInteractiveMode ( )
2013-12-13 23:16:56 +04:00
} else {
2015-06-24 12:40:09 +03:00
// In single command mode, just run it.
2015-06-30 15:13:00 +03:00
worker , done , err := wi . RunCommand ( args , nil /*custom wrangler*/ , true /*runFromCli*/ )
2015-06-24 12:40:09 +03:00
if err != nil {
log . Error ( err )
exit . Return ( 1 )
}
// Run the subsequent, blocking wait asynchronously.
2015-06-23 14:59:39 +03:00
go func ( ) {
2015-06-24 12:40:09 +03:00
if err := wi . WaitForCommand ( worker , done ) ; err != nil {
2015-06-23 14:59:39 +03:00
log . Error ( err )
2015-06-24 12:40:09 +03:00
// We cannot use exit.Return() here because we are in a different go routine now.
2015-06-23 14:59:39 +03:00
os . Exit ( 1 )
}
os . Exit ( 0 )
} ( )
2013-12-13 23:16:56 +04:00
}
2014-06-17 02:27:45 +04:00
servenv . RunDefault ( )
2013-12-13 23:16:56 +04:00
}
2015-06-22 21:37:46 +03:00
func setUsage ( ) {
flag . Usage = func ( ) {
fmt . Fprintf ( os . Stderr , "Usage: %s [global parameters] command [command parameters]\n" , os . Args [ 0 ] )
fmt . Fprintf ( os . Stderr , "\nThe global optional parameters are:\n" )
flag . PrintDefaults ( )
fmt . Fprintf ( os . Stderr , "\nThe commands are listed below, sorted by group. Use '%s <command> -h' for more help.\n\n" , os . Args [ 0 ] )
for _ , group := range worker . Commands {
if group . Name == "Debugging" {
continue
}
fmt . Fprintf ( os . Stderr , "%v: %v\n" , group . Name , group . Description )
for _ , cmd := range group . Commands {
fmt . Fprintf ( os . Stderr , " %v %v\n" , cmd . Name , cmd . Params )
}
fmt . Fprintf ( os . Stderr , "\n" )
}
}
}