Example #1
0
func main() {
	log = log15.New()
	log.Info("starting server...")

	envconfig.Process("", &settings)

	schema, err := graphql.NewSchema(makeSchema())
	if err != nil {
		log.Info("error creating schema", "err", err)
		return
	}

	h := handler.New(&handler.Config{
		Schema: &schema,
		Pretty: false,
	})

	mux := http.NewServeMux()
	mux.Handle("/graphql", h)
	mux.Handle("/", http.FileServer(http.Dir("dist")))

	log.Info("Listening at " + settings.Port + "...")
	graceful.Run(":"+settings.Port, 1*time.Second, mux)
	log.Info("Exiting...")
}
Example #2
0
func StartWebServer() error {
	conf, err := config.GetConfig()
	if err != nil {
		return err
	}

	var hystrixTimeout time.Duration
	conf.Hystrix.Timeout = strings.TrimSpace(conf.Hystrix.Timeout)
	if conf.Hystrix.Timeout != "" {
		hystrixTimeout, err = time.ParseDuration(conf.Hystrix.Timeout)
		if err != nil || hystrixTimeout < time.Millisecond {
			hystrixTimeout = time.Second
			log15.Error("Use default time", "module", "hystrix", "timeout", hystrixTimeout)
		}
	}

	hystrix.ConfigureCommand("waitFor", hystrix.CommandConfig{
		Timeout:                int(int64(hystrixTimeout) / int64(time.Millisecond)), // converted into Millisecond.
		MaxConcurrentRequests:  conf.Hystrix.MaxConcurrentRequests,
		ErrorPercentThreshold:  conf.Hystrix.ErrorPercentThreshold,
		RequestVolumeThreshold: conf.Hystrix.RequestVolumeThreshold,
		SleepWindow:            conf.Hystrix.SleepWindow,
	})

	e := echo.New()
	e.Post("/api/v1/tweet", createTweetV1)
	e.Get("/api/v1/tweets/:id", getAllTweetForV1)
	e.Get("/api/v1/wait/:timeout", waitFor)
	e.Get("/api/v1/wait_protected/:timeout", waitForProtected)
	e.Static("/", "www/static/")
	logsrv := log15.New("pid", os.Getpid(), "addr", conf.Web.Address)
	return listenAndServer(logsrv, conf.Web.Address, handlers.LoggingHandler(os.Stdout, handlers.CompressHandler(e.Router())))
}
Example #3
0
func newLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(os.Stdout, log15.LogfmtFormat())
	//h = log15.CallerStackHandler("%+n", h)
	l.SetHandler(log15.LazyHandler(h))
	return l
}
Example #4
0
func (srv *T) Init() error {
	err := os.MkdirAll(srv.Config.General.DataPath, os.ModeDir|0700)
	if err != nil {
		return err
	}
	database, err := db.NewDB(filepath.Join(srv.Config.General.DataPath, "db"))
	if err != nil {
		return err
	}
	srv.DB = database
	srv.Log = log15.New()
	srv.Router = pubsub.New(100)
	return nil
}
Example #5
0
func ExampleHandler() {
	log := log15.New()
	log.SetHandler(&slack15.Handler{
		URL: "", // pass url here, or through $SLACK_WEBHOOK_URL
		// You can skip this and stick with webhook defaults
		Envelope: slack15.Envelope{
			Username:  "******",
			IconEmoji: ":showman:",
		},
	})
	log.Info("Whaam!", "who", "Roy Lichtenstein", "when", 1963)

	// Output:
}
Example #6
0
func newLogger(c *Context) (log15.Logger, error) {
	lvl := c.String(FlagLogLevel)
	l := log15.New()
	log.Println(fmt.Sprintf("Using log level %s", lvl))
	v, err := log15.LvlFromString(lvl)
	if err != nil {
		return l, err
	}
	h := log15.LvlFilterHandler(v, log15.StreamHandler(os.Stdout, log15.LogfmtFormat()))
	if lvl == "debug" {
		h = log15.CallerFileHandler(h)
	}
	l.SetHandler(log15.LazyHandler(h))
	return l, err
}
Example #7
0
func (i *Irrational) Init(license xchg.LicenseID) {
	i.log.Info("Licensed", "license", license)
	i.log = log15.New("license", license)
	i.id = license
	//i.tc = make(chan Tick)
	i.cl = make(chan bool)
	i.tq = lane.NewQueue()
	i.lp = make(map[xchg.Symbol]float32)
	i.ls = make(map[xchg.Symbol]uint64)
	i.tra = make(map[xchg.AuctionID]xchg.TradeStatus)
	i.trb = make(map[xchg.BidID]xchg.TradeStatus)
	i.act = i.mkt.Bank().Open()
	i.log.Info("Bank account opened", "token", i.act)
	go i.Loop()
}
Example #8
0
func main() {
	flag.Parse()
	args := flag.Args()
	if len(args) != 2 || *validatorName == "" {
		flag.Usage()
		os.Exit(2)
	}
	log := log15.New()

	expected, err := os.Open(args[0])
	if err != nil {
		log.Error("Unable to open expected file", "err", err)
		os.Exit(1)
	}
	defer expected.Close()

	contestant, err := os.Open(args[1])
	if err != nil {
		log.Error("Unable to open contestant file", "err", err)
		os.Exit(1)
	}
	defer contestant.Close()

	validator := &common.ValidatorSettings{
		Name:      *validatorName,
		Tolerance: tolerance,
	}
	score, mismatch, err := runner.CalculateScore(
		validator,
		expected,
		contestant,
	)

	if err != nil {
		log.Error("Error validating", "err", err)
		os.Exit(1)
	}

	if mismatch != nil {
		log.Info(
			"Token mismatch",
			"expected", mismatch.Expected,
			"got", mismatch.Contestant,
		)
	}

	fmt.Printf("%.2f\n", score)
}
Example #9
0
func init() {
	Log = log.New()
	var handler log.Handler
	env := config.Viper.GetString("env")
	lvl, err := log.LvlFromString(config.Viper.GetString("LogLevel"))
	if err != nil {
		panic("Could not read configuration for LogLevel, check the 'config-" + env +
			".json' file: " + err.Error())
	}
	if env == "dev" || env == "qa" {
		handler = logext.FatalHandler(log.LvlFilterHandler(lvl, log.CallerFileHandler(log.StdoutHandler)))
	} else {
		handler = logext.FatalHandler(log.LvlFilterHandler(lvl, log.CallerFileHandler(log.StreamHandler(os.Stdout, log.JsonFormat()))))
	}
	Log.SetHandler(handler)
}
Example #10
0
func TestSpeculativeHandler(t *testing.T) {
	t.Parallel()

	// test with an even multiple of the buffer size, less than full buffer size
	// and not a multiple of the buffer size
	for _, count := range []int{10000, 50, 432} {
		recs := make(chan *log.Record)
		done := make(chan int)
		spec := SpeculativeHandler(100, log.ChannelHandler(recs))

		go func() {
			defer close(done)
			expectedCount := int(math.Min(float64(count), float64(100)))
			expectedIdx := count - expectedCount
			for r := range recs {
				if r.Ctx[1] != expectedIdx {
					t.Errorf("Bad ctx 'i', got %d expected %d", r.Ctx[1], expectedIdx)
					return
				}
				expectedIdx++
				expectedCount--

				if expectedCount == 0 {
					// got everything we expected
					break
				}
			}

			select {
			case <-recs:
				t.Errorf("got an extra record we shouldn't have!")
			default:
			}
		}()

		lg := log.New()
		lg.SetHandler(spec)
		for i := 0; i < count; i++ {
			lg.Debug("test speculative", "i", i)
		}

		go spec.Flush()

		// wait for the go routine to finish
		<-done
	}
}
// inital PoolConfig of pgx
func (pgdb *PostgresDB) InitConfig(dbhost, dbuser, dbpassword, dbname string) error {

	pgdb.poolConfig = pgx.ConnPoolConfig{
		ConnConfig: pgx.ConnConfig{
			Host:     dbhost,
			User:     dbuser,
			Password: dbpassword,
			Database: dbname,
			Logger:   log.New("module", "pgx"),
		},
		MaxConnections: 5,
		AfterConnect:   pgdb.afterConnect,
	}

	// = connPoolConfig
	return nil
}
Example #12
0
func RunFormula(s scheduler.Scheduler, e executor.Executor, formula def.Formula, journal io.Writer) def.JobResult {
	jobLoggerFactory := func(_ def.JobID) io.Writer {
		// All job progress reporting, still copy to our shared journal stream.
		// This func might now be outdated; but we haven't decided what any of this
		//  should look like if take a lurch toward supporting cluster farming.
		//  (It might make sense to have a structural comms layer?  Or, maybe plain
		//  byte streams are best for sanity conservation.  Either way: not today.)
		return journal
	}

	s.Configure(e, 1, jobLoggerFactory) // queue concept a bit misplaced here
	s.Start()

	// Set up a logger.
	log := log15.New()
	log.SetHandler(log15.StreamHandler(journal, log15.TerminalFormat()))

	id, jobChan := s.Schedule(formula)
	log = log.New(log15.Ctx{"JobID": id})

	log.Info("Job queued")
	job := <-jobChan
	// TODO need better lifecycle events here.  "starting" here means we might still be in provisioning stage.
	log.Info("Job starting")

	// Stream job output to terminal in real time
	_, err := io.Copy(journal, job.OutputReader())
	if err != nil {
		log.Error("Error reading job stream", "error", err)
		panic(err)
	}

	result := job.Wait()
	if result.Error != nil {
		log.Error("Job execution errored", "error", result.Error.Message())
	} else {
		log.Info("Job finished", log15.Ctx{
			"exit":    result.ExitCode,
			"outputs": result.Outputs,
		})
	}
	return result
}
Example #13
0
func (e *Executor) Start(f def.Formula, id def.JobID, stdin io.Reader, journal io.Writer) def.Job {

	// Prepare the forumla for execution on this host
	def.ValidateAll(&f)

	job := basicjob.New(id)
	jobReady := make(chan struct{})

	go func() {
		// Run the formula in a temporary directory
		flak.WithDir(func(dir string) {

			// spool our output to a muxed stream
			var strm streamer.Mux
			strm = streamer.CborFileMux(filepath.Join(dir, "log"))
			outS := strm.Appender(1)
			errS := strm.Appender(2)
			job.Streams = strm
			defer func() {
				// Regardless of how the job ends (or even if it fails the remaining setup), output streams must be terminated.
				outS.Close()
				errS.Close()
			}()

			// Job is ready to stream process output
			close(jobReady)

			// Set up a logger.  Tag all messages with this jobid.
			logger := log15.New(log15.Ctx{"JobID": id})
			logger.SetHandler(log15.StreamHandler(journal, log15.TerminalFormat()))

			job.Result = e.Run(f, job, dir, stdin, outS, errS, logger)
		}, e.workspacePath, "job", string(job.Id()))

		// Directory is clean; job complete
		close(job.WaitChan)
	}()

	<-jobReady
	return job
}
Example #14
0
func TestHotSwapHandler(t *testing.T) {
	t.Parallel()

	h1, r1 := testHandler()

	l := log.New()
	h := HotSwapHandler(h1)
	l.SetHandler(h)

	l.Info("to h1")
	if r1.Msg != "to h1" {
		t.Fatalf("didn't get expected message to h1")
	}

	h2, r2 := testHandler()
	h.Swap(h2)
	l.Info("to h2")
	if r2.Msg != "to h2" {
		t.Fatalf("didn't get expected message to h2")
	}
}
Example #15
0
func TestErrorHandler(t *testing.T) {
	t.Parallel()

	h, r := testHandler()
	lg := log.New()
	lg.SetHandler(EscalateErrHandler(
		log.LvlFilterHandler(log.LvlError, h)))

	lg.Debug("some function result", "err", nil)
	if r.Msg != "" {
		t.Fatalf("Expected debug level message to be filtered")
	}

	lg.Debug("some function result", "err", errors.New("failed operation"))
	if r.Msg != "some function result" {
		t.Fatalf("Expected debug level message to be escalated and pass lvlfilter")
	}

	if r.Lvl != log.LvlError {
		t.Fatalf("Expected debug level message to be escalated to LvlError")
	}
}
Example #16
0
func init() {
	log = log15.New()
}
Example #17
0
package v1

import (
	"github.com/Efruit/marqit/bank"
	"github.com/Efruit/marqit/exchange"
	"github.com/inconshreveable/log15"
	"math/rand"
	"sync"
)

var BnLog = log15.New("module", "bank", "version", "v1")

type Bank1 struct {
	sync.RWMutex
	accounts map[bank.AccountID]bank.Account
	a        uint
	apin     bank.PIN
}

var _ bank.Bank = &Bank1{}

func NewBank1() (bank.Bank, bank.PIN) {
	apin := (*Bank1).generatePIN(nil)
	return &Bank1{
		accounts: make(map[bank.AccountID]bank.Account),
		apin:     apin,
	}, apin
}

func (_ *Bank1) generatePIN() bank.PIN {
	length := rand.Intn(10) + 10
Example #18
0
func newLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(os.Stdout, log15.LogfmtFormat())
	l.SetHandler(log15.LazyHandler(h))
	return l
}
Example #19
0
package netplayer

import (
	"github.com/Efruit/marqit/bank"
	xchg "github.com/Efruit/marqit/exchange"
	"github.com/Efruit/marqit/managers"
	"github.com/Efruit/marqit/trader"
	"github.com/inconshreveable/log15"
)

var NpLog = log15.New("module", "netplayer")

var NewNetPlayer manager.TraderMaker = func(m manager.MiniMarket) trader.Trader {
	return &NetPlayer{Market: m}
}

type NetPlayer struct {
	Market  manager.MiniMarket
	License xchg.LicenseID
	Account bank.Token
	trx     map[xchg.OrderID]xchg.TradeStatus
}

var _ trader.Trader = &NetPlayer{}

func (n *NetPlayer) Init(l xchg.LicenseID) {
	bank := n.Market.Bank()
	if bank == nil {
		panic("netplayer: bank == nil")
	}
	n.License = l
Example #20
0
package irrational

import (
	"github.com/Efruit/marqit/bank"
	xchg "github.com/Efruit/marqit/exchange"
	"github.com/Efruit/marqit/managers"
	"github.com/Efruit/marqit/ticker"
	"github.com/Efruit/marqit/trader"
	"github.com/inconshreveable/log15"
	"github.com/oleiade/lane"
	"math/rand"
	"time"
)

var NewIrrational manager.TraderMaker = func(m manager.MiniMarket) trader.Trader {
	return &Irrational{mkt: m, log: log15.New("module", "irrational")}
}

var _ trader.AuctionBidder = &Irrational{}
var _ trader.AuctionAsker = &Irrational{}

type Irrational struct {
	log  log15.Logger
	mode xchg.Mode
	id   xchg.LicenseID
	act  bank.Token
	mkt  manager.MiniMarket
	cl   chan bool
	cl2  bool
	tq   *lane.Queue
	lp   map[xchg.Symbol]float32
Example #21
0
func nullLogger() log15.Logger {
	l := log15.New()
	h := log15.StreamHandler(ioutil.Discard, log15.LogfmtFormat())
	l.SetHandler(h)
	return l
}
Example #22
0
func main() {

	srvlog := log.New("module", "postgres/pgx")
	srvlog.Info("Program starting", "args", os.Args)

	var err error
	// init postgres DB connection
	dbhost := "localhost"
	dbuser := "******"
	dbpassword := "******"
	dbname := "tsingcloud"

	srvlog.Warn("database info", log.Ctx{"dbhost": dbhost, "dbuser": dbuser})

	//var taskPgdb *task.PostgresDB
	//taskPgdb =&task.PostgresDB{}

	taskPgdb := new(task.PostgresDB)
	taskPgdb.InitDb(dbhost, dbuser, dbpassword, dbname)

	defer taskPgdb.Pool.Close()

	if len(os.Args) == 1 {
		printHelp()
		srvlog.Warn("print help")
		os.Exit(0)
	}

	switch os.Args[1] {

	/**
	case "test":
		err = taskPgdb.Transfer()
		if err != nil {
			fmt.Fprintf(os.Stderr, "query error: %v\n", err)
			srvlog.Warn("query error", "error", err)
			os.Exit(1)
		}
	*/

	case "list":
		err = taskPgdb.ListTasks()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to list tasks: %v\n", err)
			srvlog.Warn("Unable to list tasks", "error", err)
			os.Exit(1)
		}

	case "add":
		err = taskPgdb.AddTask(os.Args[2])
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to add task: %v\n", err)
			os.Exit(1)
		}

	case "update":
		n, err := strconv.ParseInt(os.Args[2], 10, 32)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err)
			os.Exit(1)
		}
		err = taskPgdb.UpdateTask(int32(n), os.Args[3], os.Args[4])
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to update task: %v\n", err)
			os.Exit(1)
		}

	case "remove":
		n, err := strconv.ParseInt(os.Args[2], 10, 32)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable convert task_num into int32: %v\n", err)
			os.Exit(1)
		}
		err = taskPgdb.RemoveTask(int32(n))
		if err != nil {
			fmt.Fprintf(os.Stderr, "Unable to remove task: %v\n", err)
			os.Exit(1)
		}

	default:
		fmt.Fprintln(os.Stderr, "Invalid command")
		srvlog.Warn("print help")
		printHelp()
		os.Exit(1)
	}
}
Example #23
0
// NewContext creates a new Context from the specified Config. This also
// creates a Logger.
func NewContext(config *Config) (*Context, error) {
	var context = Context{
		Config: *config,
	}

	// Logging
	context.Log = log15.New()
	if context.Config.Logging.File == "/dev/null" {
		context.handler = log15.DiscardHandler()
	} else if context.Config.Logging.File == "stderr" {
		context.handler = log15.StderrHandler
	} else {
		handler, err := log15.FileHandler(
			context.Config.Logging.File,
			log15.LogfmtFormat(),
		)
		if err != nil {
			return nil, err
		}
		context.handler = handler
	}
	level, err := log15.LvlFromString(context.Config.Logging.Level)
	if err != nil {
		return nil, err
	}
	context.handler = log15.LvlFilterHandler(level, context.handler)
	context.Log.SetHandler(context.handler)

	// Tracing
	if context.Config.Tracing.Enabled {
		s, err := os.Stat(context.Config.Tracing.File)
		if os.IsNotExist(err) || s.Size() == 0 {
			context.tracingFd, err = os.Create(context.Config.Tracing.File)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				false,
			)
			if err != nil {
				return nil, err
			}
		} else {
			context.tracingFd, err = os.OpenFile(
				context.Config.Tracing.File,
				os.O_WRONLY|os.O_APPEND,
				0664,
			)
			if err != nil {
				return nil, err
			}
			context.EventCollector, err = NewWriterEventCollector(
				context.tracingFd,
				true,
			)
			if err != nil {
				return nil, err
			}
		}
	} else {
		context.EventCollector = &NullEventCollector{}
	}
	hostname, err := os.Hostname()
	if err != nil {
		hostname = "main"
	}
	context.EventFactory = NewEventFactory(hostname, "main")
	context.EventFactory.Register(context.EventCollector)

	return &context, nil
}