Skip to content

Commit

Permalink
imapsync connected to queue
Browse files Browse the repository at this point in the history
  • Loading branch information
LobbyLobster committed Oct 11, 2023
1 parent ddd7188 commit 3d76de2
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 29 deletions.
1 change: 1 addition & 0 deletions controller/sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func handleSync(ctx *gin.Context) {
// Add to queue
log.Infof("Adding %s to queue", sourceDetails.Account)
internal.AddTask(sourceDetails, destinationDetails)
ctx.HTML(200, "success.html", "added to queue")

// log.Infof("Syncing %s to %s", sourceDetails.Account, destinationDetails.Account)

Expand Down
19 changes: 1 addition & 18 deletions internal/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,8 @@ func InitQueue() {
SetupLogger()
queue = list.New()
taskChan = make(chan Task)
for i := 0; i < 10; i++ {
addOneTask()
}
go processPendingTasks()
}

func addOneTask() {
task := &Task{
ID: queue.Len() + 1,
SourceAccount: "jomo",
SourceServer: "imap.gmail.com",
DestinationAccount: "emin",
DestinationServer: "imap.yandex.com",
Status: "Pending",
}
queue.PushFront(task)
go func() {
taskChan <- *task
}()
go processPendingTasks()
}

func AddTask(sourceDetails, destinationDetails Credentials) {
Expand Down
2 changes: 2 additions & 0 deletions internal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

func syncIMAP(details *Task) error {

details.Status = "In Progress"

cmd := exec.Command("imapsync", "--host1", details.SourceServer, "--user1", details.SourceAccount, "--password1", details.SourcePassword, "--host2", details.DestinationServer, "--user2", details.DestinationAccount, "--password2", details.DestinationPassword)
var stdBuffer bytes.Buffer
mw := io.MultiWriter(os.Stdout, &stdBuffer)
Expand Down
11 changes: 1 addition & 10 deletions internal/sync_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@ func processPendingTasks() {

// If there are no pending tasks, wait for a new task to be added
if task == nil {
task := <-taskChan
simulateSyncIMAP(&task)
continue
}

simulateSyncIMAP(task)
syncIMAP(task)
time.Sleep(2000 * time.Millisecond)
}
}
Expand All @@ -28,10 +26,3 @@ func getFirstPendingTask() *Task {
}
return nil
}

func simulateSyncIMAP(details *Task) error {
details.Status = "In Progress"
time.Sleep(5000 * time.Millisecond)
details.Status = "Done"
return nil
}
2 changes: 1 addition & 1 deletion templates/admin.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ <h2 class="text-4xl m-2">Queue</h2>
table.removeAttribute("hx-trigger");
htmx.process(table);
} else {
table.setAttribute("hx-trigger", "every 4s");
table.setAttribute("hx-trigger", "every 2s");
htmx.process(table);
}
}
Expand Down

0 comments on commit 3d76de2

Please sign in to comment.