Skip to content

Commit

Permalink
Cancel button implemented. Fix predefined input fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
xeome committed Oct 20, 2023
1 parent 1603dc0 commit f294d65
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 31 deletions.
29 changes: 28 additions & 1 deletion controller/sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,34 @@ func handleCancel(ctx *gin.Context) {

internal.CancelTask(task)

log.Infof("%#v", task)
sourceServer := task.SourceServer
sourceAccount := task.SourceAccount
sourcePassword := task.SourcePassword
destinationServer := task.DestinationServer
destinationAccount := task.DestinationAccount
destinationPassword := task.DestinationPassword

sourceDetails := internal.Credentials{
Server: sourceServer,
Account: sourceAccount,
Password: sourcePassword,
}

destinationDetails := internal.Credentials{
Server: destinationServer,
Account: destinationAccount,
Password: destinationPassword,
}

creds := struct {
Source internal.Credentials
Destination internal.Credentials
}{
Source: sourceDetails,
Destination: destinationDetails,
}

ctx.HTML(200, "sync_success.html", creds)
}

func handleRetry(ctx *gin.Context) {
Expand Down
17 changes: 12 additions & 5 deletions internal/cancel.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package internal

// CancelTask cancels a task and removes it from channel
func CancelTask(task *Task) {
if task.Status != "In Progress" {
updateTaskStatus(task, "Cancelled")
log.Debug(len(taskChan))
_ = <-taskChan
log.Debug(len(taskChan))
return
}

select {
case <-taskChan:
log.Info("Task cancelled: ", task.ID)
default:
log.Info("Task not found in channel: ", task.ID)
}

} else {
cancel()
updateTaskStatus(task, "Cancelled")
}
}
64 changes: 58 additions & 6 deletions internal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,57 @@ package internal

import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
"syscall"
"time"
)

func syncIMAP(details *Task) error {
// func syncIMAP( ,details *Task) error {

// updateTaskStatus(details, "In Progress")

// currentTime := time.Now().Format("2006.01.02_15:04:05")

// logname := details.SourceAccount + "_" + details.DestinationAccount + "_" + currentTime + ".log"

// cmd := exec.Command("imapsync",
// "--host1", details.SourceServer,
// "--user1", details.SourceAccount,
// "--password1", details.SourcePassword,
// "--host2", details.DestinationServer,
// "--user2", details.DestinationAccount,
// "--password2", details.DestinationPassword,
// "--logfile", logname)

// updateTaskLogFile(details, logname)
// var stdBuffer bytes.Buffer
// mw := io.MultiWriter(os.Stdout, &stdBuffer)

// cmd.Stdout = mw
// cmd.Stderr = mw

// if err := cmd.Run(); err != nil {
// updateTaskStatus(details, "Error")
// return fmt.Errorf("error running imapsync: %w", err)
// }

// updateTaskStatus(details, "Done")

// return nil
// }

func syncIMAP(ctx context.Context, details *Task) error {
updateTaskStatus(details, "In Progress")

currentTime := time.Now().Format("2006.01.02_15:04:05")

logname := details.SourceAccount + "_" + details.DestinationAccount + "_" + currentTime + ".log"

cmd := exec.Command("imapsync",
cmd := exec.CommandContext(ctx, "imapsync",
"--host1", details.SourceServer,
"--user1", details.SourceAccount,
"--password1", details.SourcePassword,
Expand All @@ -33,12 +68,29 @@ func syncIMAP(details *Task) error {
cmd.Stdout = mw
cmd.Stderr = mw

if err := cmd.Run(); err != nil {
if err := cmd.Start(); err != nil {
updateTaskStatus(details, "Error")
return fmt.Errorf("error running imapsync: %w", err)
return fmt.Errorf("error starting imapsync: %w", err)
}

updateTaskStatus(details, "Done")
done := make(chan error, 1)
go func() {
done <- cmd.Wait()
}()

return nil
select {
case <-ctx.Done():
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
return fmt.Errorf("failed to terminate imapsync process: %w", err)
}
updateTaskStatus(details, "Cancelled")
return ctx.Err()
case err := <-done:
if err != nil {
updateTaskStatus(details, "Error")
return fmt.Errorf("error running imapsync: %w", err)
}
updateTaskStatus(details, "Done")
return nil
}
}
15 changes: 11 additions & 4 deletions internal/sync_loop.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package internal

import (
"context"
"os"
"time"
)

var ctx context.Context
var cancel context.CancelFunc

func processPendingTasks() {
for {
// Get the index of the first pending task
Expand All @@ -16,9 +20,12 @@ func processPendingTasks() {
continue
}

// syncIMAP(task)
simulateTask(task)
time.Sleep(100 * time.Millisecond)
log.Info("Processing task: ", task.ID)
ctx, cancel = context.WithCancel(context.Background())
syncIMAP(ctx, task)

// simulateTask(task)
time.Sleep(1000 * time.Millisecond)
}
}

Expand Down Expand Up @@ -50,6 +57,6 @@ func simulateTask(task *Task) {
logFile.WriteString("This is a test log file\n")

updateTaskStatus(task, "In Progress")
time.Sleep(10000 * time.Millisecond)
time.Sleep(1000 * time.Millisecond)
updateTaskStatus(task, "Done")
}
30 changes: 15 additions & 15 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ <h2 class="text-2xl font-semibold mb-4">Source Details</h2>
<div class="grid gap-4 form-group">
<div class="form-field">
<label for="source_server" class="block font-semibold">Server</label>
<input type="text" x-model="source_server" id="source_server" name="source_server" placeholder="Source Server"
class="input max-w-full" value="{{.SourceDetails.Server}}">
<input type="text" x-model="source_server" id="source_server" name="source_server"
placeholder="Source Server" class="input max-w-full" value="{{.SourceDetails.Server}}">
</div>
<div>
<label for="source_account" class="block font-semibold">Account</label>
<input type="text" x-model="source_account" id="source_account" name="source_account" placeholder="Source Account"
class="input max-w-full" value="{{.SourceDetails.Account}}">
<input type="text" x-model="source_account" id="source_account" name="source_account"
placeholder="Source Account" class="input max-w-full" value="{{.SourceDetails.Account}}">
</div>
<div>
<label for="source_password" class="block font-semibold">Password</label>
Expand Down Expand Up @@ -114,7 +114,7 @@ <h2 class="text-2xl font-semibold mb-4">Destination Details</h2>
<div class="modal w-screen" id="detailsmodal" :class="model_open ? 'visible opacity-100' : '' ">
{{ template "log_window.html"}}
</div>

<!-- Log output -->
<div class="container mx-auto flex px-4" x-show='source_validated && destination_validated'>
<div class="w-full bg-backgroundSecondary p-4 shadow-md border border-border rounded-lg">
Expand All @@ -126,8 +126,8 @@ <h2 class="text-2xl font-semibold">User queue</h2>
</div>
<div class="grid gap-4">
<table class="table-hover table-compact table" id="queue-table">
{{template "table_user.html"}}
</table>
{{template "table_user.html"}}
</table>
</div>
</div>
</div>
Expand Down Expand Up @@ -216,24 +216,24 @@ <h2 class="text-2xl font-semibold">User queue</h2>
document.addEventListener('alpine:init', () => {
Alpine.data('data', () => ({
model_open: '',
source_server: '',
source_account: '',
destination_server: '',
destination_account: '',
source_server: '{{.SourceDetails.Server}}',
source_account: '{{.SourceDetails.Account}}',
destination_server: '{{.DestinationDetails.Server}}',
destination_account: '{{.DestinationDetails.Account}}',

source_validated: false,
destination_validated: false,

showQueue(){
showQueue() {
if (this.source_validated === true && this.destination_validated === true) {
htmx.ajax('POST', "/api/search?exact=true&source_server="+this.source_server+"&source_account="+this.source_account+"&destination_server="+this.destination_server+"&destination_account="+this.destination_account+"", {target:'#table-body',swap:'innerHTML'})
htmx.ajax('POST', "/api/search?exact=true&source_server=" + this.source_server + "&source_account=" + this.source_account + "&destination_server=" + this.destination_server + "&destination_account=" + this.destination_account + "", { target: '#table-body', swap: 'innerHTML' })
}
},

init() {
setInterval(() => {
this.showQueue();
}, 2000);
}, 60000);
},

update_source_status(result) {
Expand All @@ -245,7 +245,7 @@ <h2 class="text-2xl font-semibold">User queue</h2>
this.showQueue();
},
}

))

})
Expand Down

0 comments on commit f294d65

Please sign in to comment.