Skip to content

Commit

Permalink
Remove context (#6)
Browse files Browse the repository at this point in the history
Signed-off-by: Masudur Rahman <[email protected]>
  • Loading branch information
masudur-rahman authored Nov 30, 2023
1 parent 5023f9e commit f4f1e56
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions nats-logger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ limitations under the License.
package main

import (
"context"
"encoding/json"
"log"
"os"
"strings"
"time"

"github.com/hpcloud/tail"
"github.com/nats-io/nats.go"
Expand Down Expand Up @@ -78,41 +78,32 @@ func main() {
}

func publishClusterProvisioningLogs(source, subject, id string, nc *nats.Conn) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
select {
case <-ctx.Done():
return
default:
if err := publishFile(source, subject, id, nc, cancel); err != nil {
log.Printf("Could not publish file: %s", err)
}

// time.Sleep(500 * time.Millisecond)
if err := publishFile(source, subject, id, nc); err != nil {
log.Printf("Could not publish file: %s", err)
}

time.Sleep(500 * time.Millisecond)
}
}

func publishFile(source, subject, id string, nc *nats.Conn, cancel context.CancelFunc) error {
func publishFile(source, subject, id string, nc *nats.Conn) error {
t, err := tail.TailFile(source, tail.Config{Follow: true})
if err != nil {
return err
}

log.Printf("Publishing lines from %s to %s", source, subject)

status := TaskStatus(TaskStatusRunning)
for line := range t.Lines {
status := generateTaskStatus(line.Text)
if status == TaskStatusRunning {
status = generateTaskStatus(line.Text)
}
msg := newResponse(status, id, "", line.Text)
if err := nc.Publish(subject, msg); err != nil {
if err = nc.Publish(subject, msg); err != nil {
klog.ErrorS(err, "failed to publish log")
}

if status != TaskStatusRunning {
cancel()
return nil
}
}

return nil
Expand Down

0 comments on commit f4f1e56

Please sign in to comment.