Skip to content

init #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

init #12

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 75 additions & 9 deletions cmd/ctrlc/root/run/exec/exec.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package exec

import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
Expand All @@ -11,7 +16,13 @@ import (
)

func NewRunExecCmd() *cobra.Command {
return &cobra.Command{
var (
name string
workspaceID string
interval time.Duration
)

cmd := &cobra.Command{
Use: "exec",
Short: "Execute commands directly when a job is received",
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -21,24 +32,79 @@ func NewRunExecCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("failed to create API client: %w", err)
}

// Create the ExecRunner with API client
runner := NewExecRunner(client)

// Create job agent config
agentConfig := api.UpsertJobAgentJSONRequestBody{
Name: name,
Type: "exec",
}

// Set workspace ID if provided
if workspaceID != "" {
agentConfig.WorkspaceId = workspaceID
}

// Create job agent
ja, err := jobagent.NewJobAgent(
client,
api.UpsertJobAgentJSONRequestBody{
Name: "exec",
Type: "exec",
},
&ExecRunner{},
agentConfig,
runner,
)
if err != nil {
return fmt.Errorf("failed to create job agent: %w", err)
}

log.Info("Exec runner started", "name", name, "workspaceID", workspaceID)

// Setup signal handling for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, os.Interrupt, syscall.SIGTERM)

go func() {
<-sigCh
log.Info("Shutting down gracefully...")
runner.ExitAll(true)
cancel()
}()

// Main polling loop
ticker := time.NewTicker(interval)
defer ticker.Stop()

// Run initial job check
if err := ja.RunQueuedJobs(); err != nil {
log.Error("failed to run queued jobs", "error", err)
log.Error("Failed to run queued jobs", "error", err)
}
if err := ja.UpdateRunningJobs(); err != nil {
log.Error("failed to check for jobs", "error", err)
log.Error("Failed to check for running jobs", "error", err)
}

// Polling loop
for {
select {
case <-ctx.Done():
return nil
case <-ticker.C:
if err := ja.RunQueuedJobs(); err != nil {
log.Error("Failed to run queued jobs", "error", err)
}
if err := ja.UpdateRunningJobs(); err != nil {
log.Error("Failed to check for running jobs", "error", err)
}
}
}
return nil
},
}

cmd.Flags().StringVar(&name, "name", "exec-runner", "Name of the job agent")
cmd.Flags().StringVar(&workspaceID, "workspace-id", "", "Workspace ID to pull jobs from")
cmd.Flags().DurationVar(&interval, "interval", 10*time.Second, "Polling interval for checking jobs")

return cmd
}
Loading