Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
andresparrab committed Feb 7, 2021
0 parents commit b89e5a0
Show file tree
Hide file tree
Showing 8 changed files with 453 additions and 0 deletions.
80 changes: 80 additions & 0 deletions activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package app

import (
"fmt"
"log"
"net/http"
"time"

"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)

var DBClient *sqlx.DB

var PostNumber Post

func ConnectToMySQL() {
db, err := sqlx.Connect("mysql", "doctor:miyamoto@tcp(localhost:3306)/test_db?parseTime=true")
if err != nil {
log.Fatalln(err)
}
fmt.Println("connected", db)
DBClient = db

}

func GetInt() error {

tx := DBClient.MustBegin()
tx.Get(&PostNumber, `SELECT id, test_numbers, created_at FROM numbers WHERE id=1`)
tx.Commit()
fmt.Println("ID: ", PostNumber.ID)
fmt.Println("Number: ", PostNumber.TestNumbers)
return nil
}

func UpdateInt() error {
PostNumber.TestNumbers++
td := time.Now()
DBClient.Exec("UPDATE numbers SET test_numbers=? WHERE id=1;", PostNumber.TestNumbers)
fmt.Println("Updated number:", PostNumber.TestNumbers, "Created: ", PostNumber.CreatedAt, "Updated: ", td)
return nil

}

// The sunction below are for testing the functions without a Temporal workflow
// with the main.go in the sql folder

func GetInt2(c *gin.Context) {

td := time.Now()
tx := DBClient.MustBegin()
tx.Get(&PostNumber, `SELECT id, test_numbers, created_at FROM numbers WHERE id=1`)
tx.Commit()
fmt.Println("ID: ", PostNumber.ID)
fmt.Println("Number: ", PostNumber.TestNumbers)

c.JSON(http.StatusOK, gin.H{
//"error": false,
"id": PostNumber.ID,
"Number: ": PostNumber.TestNumbers,
"Updated number": PostNumber.TestNumbers,
"Created ": PostNumber.CreatedAt,
"Updated ": td,
})
//return nil
}

func UpdateInt2(c *gin.Context) {
PostNumber.TestNumbers++
td := time.Now()
DBClient.Exec("UPDATE numbers SET test_numbers=? WHERE id=1;", PostNumber.TestNumbers)
fmt.Println("Updated number:", PostNumber.TestNumbers, "Created: ", PostNumber.CreatedAt, "Updated: ", td)

c.JSON(http.StatusCreated, gin.H{
"error": false,
})

}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module andresparrab/atest/app

go 1.15

require (
github.com/gin-gonic/gin v1.6.3
github.com/go-sql-driver/mysql v1.5.0
github.com/jmoiron/sqlx v1.3.1
go.temporal.io/sdk v1.4.1
)
209 changes: 209 additions & 0 deletions go.sum

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package app

import "time"

const NumberTaskQueue = "UPDATE_NUMBER_TASK_QUEUE"

type Post struct {
ID int64 `db:"id"`
TestNumbers int64 `json:"test_numbers" db:"test_numbers" `
CreatedAt time.Time `db:"created_at"`
}
24 changes: 24 additions & 0 deletions sql/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"andresparrab/atest/app"
"fmt"

"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
)

func main() {

app.ConnectToMySQL()
app.GetInt()
r := gin.Default()
r.PUT("/", app.UpdateInt2)
r.GET("/", app.GetInt2)
fmt.Println(r)
app.UpdateInt()

if err := r.Run(":5000"); err != nil {
panic(err.Error())
}
}
43 changes: 43 additions & 0 deletions start/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"andresparrab/atest/app"
"context"
"log"

"go.temporal.io/sdk/client"
)

func main() {
// Create the client object
c, err := client.NewClient(client.Options{})
if err != nil {
log.Fatalln("unable to create Temporal client", err)
}
defer c.Close()

options := client.StartWorkflowOptions{
ID: "UpdtaingNumber",
TaskQueue: app.NumberTaskQueue,
}

inputToTemporal := "Getting and updating the int from a mysql DB for testing purposes"

we, err := c.ExecuteWorkflow(context.Background(), options, app.UpdateNumer, inputToTemporal)
if err != nil {
log.Fatalln("error starting UpdateNmber workflow", err)
}
printResults(we.GetID(), we.GetRunID())
//fmt.Println("This is the we ", we)
}

func printResults(workflowID, runID string) {
log.Printf(
"\nGetting and Updating of INT from DB \n",
)
log.Printf(
"\nWorkflowID: %s RunID: %s\n",
workflowID,
runID,
)
}
40 changes: 40 additions & 0 deletions worker/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"fmt"
"log"

"andresparrab/atest/app"

_ "github.com/go-sql-driver/mysql"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
)

func main() {
app.ConnectToMySQL()
// Create the temporal client object just once per process
c, err := client.NewClient(client.Options{})
if err != nil {
log.Fatalln("unable to create Temporal client", err)
}
defer c.Close()
// This worker hosts both Worker and Activity functions
fmt.Println("Started the worker")
numberWoker := worker.New(c, app.NumberTaskQueue, worker.Options{})

fmt.Println("Register the workflow app.UpdateNumer ")
numberWoker.RegisterWorkflow(app.UpdateNumer)

fmt.Println("Register the Activity app.GetInt ")
numberWoker.RegisterActivity(app.GetInt)

fmt.Println("Register the app.UpdateInt ")
numberWoker.RegisterActivity(app.UpdateInt)

// Start listening to the Task Queue
err = numberWoker.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("unable to start Worker", err)
}
}
36 changes: 36 additions & 0 deletions workflow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app

import (
"fmt"
"time"

"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)

func UpdateNumer(ctx workflow.Context, mess string) error {
fmt.Println("Entering UpdateNumer Workflow")
// RetryPolicy specifies how to automatically handle retries if an Activity fails.
retrypolicy := &temporal.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2.0,
MaximumInterval: time.Minute,
MaximumAttempts: 500,
}
options := workflow.ActivityOptions{
StartToCloseTimeout: time.Minute,
RetryPolicy: retrypolicy,
}
ctx = workflow.WithActivityOptions(ctx, options)
fmt.Println("Executing the GetInt")
err := workflow.ExecuteActivity(ctx, GetInt).Get(ctx, nil)
if err != nil {
return err
}
fmt.Println("Executing the UpdateInt")
err = workflow.ExecuteActivity(ctx, UpdateInt).Get(ctx, nil)
if err != nil {
return err
}
return nil
}

0 comments on commit b89e5a0

Please sign in to comment.