Repository: Arriven/db1000n
File: main.go (Go)

Code before:

func httpJob(ctx context.Context, args JobArgs) error {
	type httpJobConfig struct {
		BasicJobConfig
		Path    string
		Method  string
		Body    json.RawMessage
		Headers map[string]string
	}
	var jobConfig httpJobConfig
	err := json.Unmarshal(args, &jobConfig)
	if err != nil {
		return err
	}
	for jobConfig.Next(ctx) {
		req, err := http.NewRequest(parseStringTemplate(jobConfig.Method), parseStringTemplate(jobConfig.Path), bytes.NewReader(parseByteTemplate(jobConfig.Body)))
		if err != nil {
			log.Printf("error creating request: %v", err)
			continue
		}
		for key, value := range jobConfig.Headers {
			req.Header.Add(parseStringTemplate(key), parseStringTemplate(value))
		}

		startedAt := time.Now().Unix()
		log.Printf("[DEBUG] %s %s started at %d", jobConfig.Method, jobConfig.Path, startedAt)

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			log.Printf("error sending request %v: %v", req, err)
			continue
		}
		finishedAt := time.Now().Unix()
		log.Printf("[DEBUG] %s %s duration %d s", jobConfig.Method, jobConfig.Path, finishedAt - startedAt)
		resp.Body.Close() // No need for response
		if resp.StatusCode >= 400 {
			log.Printf("[DEBUG] %s %s failed at %d with code %d", jobConfig.Method, jobConfig.Path, finishedAt, resp.StatusCode)
		} else {
			log.Printf("[DEBUG] %s %s finished at %d", jobConfig.Method, jobConfig.Path, finishedAt)
		}
		time.Sleep(time.Duration(jobConfig.IntervalMs) * time.Millisecond)
	}
	return nil
}

// RawNetJobConfig comment for linter
type RawNetJobConfig struct {
	BasicJobConfig
	Address string
	Body    json.RawMessage
}

Review comment (bug):
It would be ideal to use some more sophisticated logging library that supports logging levels so that we could only include debug logging when needed. Can be done in a separate PR if you want

Code after:
	return output.String()
}

func httpJob(ctx context.Context, args JobArgs) error {
	l := logs.Logs{}

	type httpJobConfig struct {
		BasicJobConfig
		Path    string
		Method  string
		Body    json.RawMessage
		Headers map[string]string
	}
	var jobConfig httpJobConfig
	err := json.Unmarshal(args, &jobConfig)
	if err != nil {
		return err
	}
	for jobConfig.Next(ctx) {
		req, err := http.NewRequest(parseStringTemplate(jobConfig.Method), parseStringTemplate(jobConfig.Path), bytes.NewReader(parseByteTemplate(jobConfig.Body)))
		if err != nil {
			l.Error("error creating request: %v", err)
			continue
		}
		for key, value := range jobConfig.Headers {
			req.Header.Add(parseStringTemplate(key), parseStringTemplate(value))
		}

		startedAt := time.Now().Unix()
		l.Debug("%s %s started at %d", jobConfig.Method, jobConfig.Path, startedAt)

		resp, err := http.DefaultClient.Do(req)
		if err != nil {
			l.Error("error sending request %v: %v", req, err)
			continue
		}

		finishedAt := time.Now().Unix()
		resp.Body.Close() // No need for response
		if resp.StatusCode >= 400 {
			l.Debug("%s %s failed at %d with code %d", jobConfig.Method, jobConfig.Path, finishedAt, resp.StatusCode)
		} else {
			l.Debug("%s %s finished at %d", jobConfig.Method, jobConfig.Path, finishedAt)
		}
		time.Sleep(time.Duration(jobConfig.IntervalMs) * time.Millisecond)
	}
	return nil
}

// RawNetJobConfig comment for linter
type RawNetJobConfig struct {