-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b89e5a0
Showing
8 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |