本文整理汇总了Golang中github.com/travis-ci/worker/context.LoggerFromContext函数的典型用法代码示例。如果您正苦于以下问题:Golang LoggerFromContext函数的具体用法?Golang LoggerFromContext怎么用?Golang LoggerFromContext使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了LoggerFromContext函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Write
func (w *amqpLogWriter) Write(p []byte) (int, error) {
if w.closed() {
return 0, fmt.Errorf("attempted write to closed log")
}
context.LoggerFromContext(w.ctx).WithFields(logrus.Fields{
"length": len(p),
"bytes": string(p),
}).Debug("writing bytes")
w.timer.Reset(w.timeout)
w.bytesWritten += len(p)
if w.bytesWritten > w.maxLength {
_, err := w.WriteAndClose([]byte(fmt.Sprintf("\n\nThe log length has exceeded the limit of %d MB (this usually means that the test suite is raising the same exception over and over).\n\nThe job has been terminated\n", w.maxLength/1000/1000)))
if err != nil {
context.LoggerFromContext(w.ctx).WithField("err", err).Error("couldn't write 'log length exceeded' error message to log")
}
return 0, ErrWrotePastMaxLogLength
}
w.bufferMutex.Lock()
defer w.bufferMutex.Unlock()
return w.buffer.Write(p)
}
示例2: Run
func (s *stepGenerateScript) Run(state multistep.StateBag) multistep.StepAction {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
b := backoff.NewExponentialBackOff()
b.MaxInterval = 10 * time.Second
b.MaxElapsedTime = time.Minute
var script []byte
err := backoff.Retry(func() (err error) {
script, err = s.generator.Generate(ctx, buildJob.RawPayload())
return
}, b)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't generate build script, erroring job")
err := buildJob.Error(ctx, "An error occurred while generating the build script.")
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't requeue job")
}
return multistep.ActionHalt
}
context.LoggerFromContext(ctx).Info("generated script")
state.Put("script", script)
return multistep.ActionContinue
}
示例3: runProcessor
func (p *ProcessorPool) runProcessor(queue JobQueue) error {
processorUUID := uuid.NewRandom()
ctx := context.FromProcessor(p.Context, processorUUID.String())
jobsChan, err := queue.Jobs(ctx)
if err != nil {
context.LoggerFromContext(p.Context).WithField("err", err).Error("couldn't create jobs channel")
return err
}
proc, err := NewProcessor(ctx, p.Hostname, jobsChan, p.Provider, p.Generator, p.Canceller, p.HardTimeout, p.LogTimeout)
if err != nil {
context.LoggerFromContext(p.Context).WithField("err", err).Error("couldn't create processor")
return err
}
proc.SkipShutdownOnLogTimeout = p.SkipShutdownOnLogTimeout
p.processorsLock.Lock()
p.processors = append(p.processors, proc)
p.processorsLock.Unlock()
proc.Run()
return nil
}
示例4: processCommand
func (d *AMQPCanceller) processCommand(delivery amqp.Delivery) error {
command := &cancelCommand{}
err := json.Unmarshal(delivery.Body, command)
if err != nil {
context.LoggerFromContext(d.ctx).WithField("err", err).Error("unable to parse JSON")
return err
}
if command.Type != "cancel_job" {
context.LoggerFromContext(d.ctx).WithField("command", command.Type).Error("unknown worker command")
return nil
}
d.cancelMutex.Lock()
defer d.cancelMutex.Unlock()
cancelChan, ok := d.cancelMap[command.JobID]
if !ok {
context.LoggerFromContext(d.ctx).WithField("command", command.Type).WithField("job", command.JobID).Info("no job with this ID found on this worker")
return nil
}
if tryClose(cancelChan) {
context.LoggerFromContext(d.ctx).WithField("command", command.Type).WithField("job", command.JobID).Info("cancelling job")
} else {
context.LoggerFromContext(d.ctx).WithField("command", command.Type).WithField("job", command.JobID).Warn("job already cancelled")
}
return nil
}
示例5: Run
func (s *stepStartInstance) Run(state multistep.StateBag) multistep.StepAction {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
context.LoggerFromContext(ctx).Info("starting instance")
ctx, cancel := gocontext.WithTimeout(ctx, s.startTimeout)
defer cancel()
startTime := time.Now()
instance, err := s.provider.Start(ctx, buildJob.StartAttributes())
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't start instance")
err := buildJob.Requeue()
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't requeue job")
}
return multistep.ActionHalt
}
context.LoggerFromContext(ctx).WithField("boot_time", time.Now().Sub(startTime)).Info("started instance")
state.Put("instance", instance)
return multistep.ActionContinue
}
示例6: Run
func (s *stepUploadScript) Run(state multistep.StateBag) multistep.StepAction {
ctx := state.Get("ctx").(gocontext.Context)
buildJob := state.Get("buildJob").(Job)
instance := state.Get("instance").(backend.Instance)
script := state.Get("script").([]byte)
ctx, cancel := gocontext.WithTimeout(ctx, s.uploadTimeout)
defer cancel()
err := instance.UploadScript(ctx, script)
if err != nil {
errMetric := "worker.job.upload.error"
if err == backend.ErrStaleVM {
errMetric += ".stalevm"
}
metrics.Mark(errMetric)
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't upload script, attemping requeue")
err := buildJob.Requeue()
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't requeue job")
}
return multistep.ActionHalt
}
context.LoggerFromContext(ctx).Info("uploaded script")
return multistep.ActionContinue
}
示例7: pollInDirTick
func (f *FileJobQueue) pollInDirTick(ctx gocontext.Context) {
logger := context.LoggerFromContext(ctx)
entries, err := ioutil.ReadDir(f.createdDir)
if err != nil {
logger.WithField("err", err).Error("input directory read error")
return
}
logger.WithFields(logrus.Fields{
"entries": entries,
"file_job_queue": fmt.Sprintf("%p", f),
}).Debug("entries")
for _, entry := range entries {
if entry.IsDir() || !strings.HasSuffix(entry.Name(), ".json") {
continue
}
buildJob := &fileJob{
createdFile: filepath.Join(f.createdDir, entry.Name()),
payload: &JobPayload{},
startAttributes: &backend.StartAttributes{},
}
startAttrs := &jobPayloadStartAttrs{Config: &backend.StartAttributes{}}
fb, err := ioutil.ReadFile(buildJob.createdFile)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("input file read error")
continue
}
err = json.Unmarshal(fb, buildJob.payload)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("payload JSON parse error")
continue
}
err = json.Unmarshal(fb, &startAttrs)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("start attributes JSON parse error")
continue
}
buildJob.rawPayload, err = simplejson.NewJson(fb)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("raw payload JSON parse error")
continue
}
buildJob.startAttributes = startAttrs.Config
buildJob.receivedFile = filepath.Join(f.receivedDir, entry.Name())
buildJob.startedFile = filepath.Join(f.startedDir, entry.Name())
buildJob.finishedFile = filepath.Join(f.finishedDir, entry.Name())
buildJob.logFile = filepath.Join(f.logDir, strings.Replace(entry.Name(), ".json", ".log", -1))
buildJob.bytes = fb
f.buildJobChan <- buildJob
}
}
示例8: GracefulShutdown
// GracefulShutdown tells the processor to finish the job it is currently
// processing, but not pick up any new jobs. This method will return
// immediately, the processor is done when Run() returns.
func (p *Processor) GracefulShutdown() {
defer func() {
err := recover()
if err != nil {
context.LoggerFromContext(p.ctx).WithField("err", err).Error("recovered from panic")
}
}()
context.LoggerFromContext(p.ctx).Info("processor initiating graceful shutdown")
tryClose(p.graceful)
}
示例9: writeLogAndFinishWithState
func (s *stepRunScript) writeLogAndFinishWithState(ctx gocontext.Context, logWriter LogWriter, buildJob Job, state FinishState, logMessage string) {
_, err := logWriter.WriteAndClose([]byte(logMessage))
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't write final log message")
}
err = buildJob.Finish(state)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).WithField("state", state).Error("couldn't update job state")
}
}
示例10: Cleanup
func (s *stepUpdateState) Cleanup(state multistep.StateBag) {
buildJob := state.Get("buildJob").(Job)
ctx := state.Get("ctx").(gocontext.Context)
mresult, ok := state.GetOk("scriptResult")
if ok {
result := mresult.(*backend.RunResult)
var err error
switch result.ExitCode {
case 0:
err = buildJob.Finish(FinishStatePassed)
case 1:
err = buildJob.Finish(FinishStateFailed)
default:
err = buildJob.Finish(FinishStateErrored)
}
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't mark job as finished")
}
}
}
示例11: Stop
func (i *gceInstance) Stop(ctx gocontext.Context) error {
logger := context.LoggerFromContext(ctx)
state := &multistep.BasicStateBag{}
c := &gceInstanceStopContext{
ctx: ctx,
errChan: make(chan error),
}
runner := &multistep.BasicRunner{
Steps: []multistep.Step{
&gceInstanceStopMultistepWrapper{c: c, f: i.stepDeleteInstance},
&gceInstanceStopMultistepWrapper{c: c, f: i.stepWaitForInstanceDeleted},
},
}
logger.WithField("instance", i.instance.Name).Info("deleting instance")
go runner.Run(state)
logger.Debug("selecting over error and done channels")
select {
case err := <-c.errChan:
return err
case <-ctx.Done():
if ctx.Err() == gocontext.DeadlineExceeded {
metrics.Mark("worker.vm.provider.gce.delete.timeout")
}
return ctx.Err()
}
}
示例12: apiRateLimit
func (p *gceProvider) apiRateLimit(ctx gocontext.Context) error {
metrics.Gauge("travis.worker.vm.provider.gce.rate-limit.queue", int64(p.rateLimitQueueDepth))
startWait := time.Now()
defer metrics.TimeSince("travis.worker.vm.provider.gce.rate-limit", startWait)
atomic.AddUint64(&p.rateLimitQueueDepth, 1)
// This decrements the counter, see the docs for atomic.AddUint64
defer atomic.AddUint64(&p.rateLimitQueueDepth, ^uint64(0))
errCount := 0
for {
ok, err := p.rateLimiter.RateLimit("gce-api", p.rateLimitMaxCalls, p.rateLimitDuration)
if err != nil {
errCount++
if errCount >= 5 {
context.CaptureError(ctx, err)
context.LoggerFromContext(ctx).WithField("err", err).Info("rate limiter errored 5 times")
return err
}
} else {
errCount = 0
}
if ok {
return nil
}
// Sleep for up to 1 second
time.Sleep(time.Millisecond * time.Duration(mathrand.Intn(1000)))
}
}
示例13: process
func (p *Processor) process(ctx gocontext.Context, buildJob Job) {
state := new(multistep.BasicStateBag)
state.Put("hostname", p.fullHostname())
state.Put("buildJob", buildJob)
state.Put("ctx", ctx)
logTimeout := p.logTimeout
if buildJob.Payload().Timeouts.LogSilence != 0 {
logTimeout = time.Duration(buildJob.Payload().Timeouts.LogSilence) * time.Second
}
steps := []multistep.Step{
&stepSubscribeCancellation{
canceller: p.canceller,
},
&stepGenerateScript{
generator: p.generator,
},
&stepSendReceived{},
&stepStartInstance{
provider: p.provider,
startTimeout: p.startupTimeout,
},
&stepUploadScript{
uploadTimeout: p.scriptUploadTimeout,
},
&stepUpdateState{},
&stepOpenLogWriter{
logTimeout: logTimeout,
maxLogLength: 4500000,
},
&stepRunScript{
logTimeout: logTimeout,
hardTimeout: p.hardTimeout,
skipShutdownOnLogTimeout: p.SkipShutdownOnLogTimeout,
},
}
runner := &multistep.BasicRunner{Steps: steps}
context.LoggerFromContext(ctx).Info("starting job")
runner.Run(state)
context.LoggerFromContext(ctx).Info("finished job")
p.ProcessedCount++
}
示例14: Run
func (s *stepSubscribeCancellation) Run(state multistep.StateBag) multistep.StepAction {
ctx := state.Get("ctx").(gocontext.Context)
buildJob := state.Get("buildJob").(Job)
ch := make(chan struct{})
state.Put("cancelChan", (<-chan struct{})(ch))
err := s.canceller.Subscribe(buildJob.Payload().Job.ID, ch)
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't subscribe to canceller")
err := buildJob.Requeue()
if err != nil {
context.LoggerFromContext(ctx).WithField("err", err).Error("couldn't requeue job")
}
return multistep.ActionHalt
}
return multistep.ActionContinue
}
示例15: flush
func (w *amqpLogWriter) flush() {
if w.buffer.Len() <= 0 {
return
}
buf := make([]byte, LogChunkSize)
for w.buffer.Len() > 0 {
w.bufferMutex.Lock()
n, err := w.buffer.Read(buf)
w.bufferMutex.Unlock()
if err != nil {
// According to documentation, err should only be non-nil if
// there's no data in the buffer. We've checked for this, so
// this means that err should never be nil. Something is very
// wrong if this happens, so let's abort!
panic("non-empty buffer shouldn't return an error on Read")
}
part := amqpLogPart{
JobID: w.jobID,
Content: string(buf[0:n]),
Number: w.logPartNumber,
}
w.logPartNumber++
err = w.publishLogPart(part)
if err != nil {
switch err.(type) {
case *amqp.Error:
if w.reopenChannel() != nil {
context.LoggerFromContext(w.ctx).WithField("err", err).Error("couldn't publish log part and couldn't reopen channel")
// Close or something
return
}
err = w.publishLogPart(part)
context.LoggerFromContext(w.ctx).WithField("err", err).Error("couldn't publish log part, even after reopening channel")
default:
context.LoggerFromContext(w.ctx).WithField("err", err).Error("couldn't publish log part")
}
}
}
}