-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
0c857b1
commit e3def84
Showing
13 changed files
with
364 additions
and
169 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
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
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
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 |
---|---|---|
|
@@ -2,16 +2,20 @@ package main | |
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/sirupsen/logrus" | ||
"github.com/surendratiwari3/paota/config" | ||
"github.com/surendratiwari3/paota/schema" | ||
"github.com/surendratiwari3/paota/workerpool" | ||
|
||
//"github.com/surendratiwari3/paota/example/task" | ||
"github.com/surendratiwari3/paota/logger" | ||
"os" | ||
) | ||
|
||
type printWorker struct { | ||
workerPool workerpool.Pool | ||
} | ||
|
||
func main() { | ||
logrusLog := logrus.StandardLogger() | ||
logrusLog.SetFormatter(&logrus.JSONFormatter{}) | ||
|
@@ -24,7 +28,7 @@ func main() { | |
//Store: "null", | ||
TaskQueueName: "paota_task_queue", | ||
AMQP: &config.AMQPConfig{ | ||
Url: "amqp://localhost:5672/", | ||
Url: "amqp://guest:guest@localhost:5672/", | ||
Check failure Code scanning / SonarCloud AMQP credentials should not be disclosed High
Make sure these Rabbit MQ credentials get revoked, changed, and removed from the code. See more on SonarCloud
|
||
Exchange: "paota_task_exchange", | ||
ExchangeType: "direct", | ||
BindingKey: "paota_task_binding_key", | ||
|
@@ -47,10 +51,13 @@ func main() { | |
logger.ApplicationLogger.Info("workerPool is nil") | ||
os.Exit(0) | ||
} | ||
|
||
printWorker := printWorker{workerPool: newWorkerPool} | ||
|
||
logger.ApplicationLogger.Info("newWorkerPool created successfully") | ||
// Register tasks | ||
regTasks := map[string]interface{}{ | ||
"Print": Print, | ||
"Print": printWorker.Print, | ||
} | ||
err = newWorkerPool.RegisterTasks(regTasks) | ||
if err != nil { | ||
|
@@ -65,7 +72,46 @@ func main() { | |
} | ||
} | ||
|
||
func Print(arg *schema.Signature) error { | ||
func (wp printWorker) Publish() { | ||
// UserRecord represents the structure of user records. | ||
type UserRecord struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
// Add other fields as needed | ||
} | ||
// Replace this with the received user record | ||
user := UserRecord{ | ||
ID: "1", | ||
Name: "John Doe", | ||
Email: "john.doe@example.com", | ||
} | ||
|
||
// Convert the struct to a JSON string | ||
userJSON, err := json.Marshal(user) | ||
if err != nil { | ||
// | ||
} | ||
|
||
printJob := &schema.Signature{ | ||
Name: "Print", | ||
Args: []schema.Arg{ | ||
{ | ||
Type: "string", | ||
Value: string(userJSON), | ||
}, | ||
}, | ||
RetryCount: 10, | ||
RoutingKey: "consumer_publisher", | ||
IgnoreWhenTaskNotRegistered: true, | ||
} | ||
|
||
wp.workerPool.SendTaskWithContext(context.Background(), printJob) | ||
|
||
} | ||
|
||
func (wp printWorker) Print(arg *schema.Signature) error { | ||
logger.ApplicationLogger.Info("success") | ||
wp.Publish() | ||
return nil | ||
} |
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,124 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"github.com/sirupsen/logrus" | ||
"github.com/surendratiwari3/paota/config" | ||
"github.com/surendratiwari3/paota/schema" | ||
"github.com/surendratiwari3/paota/workerpool" | ||
"sync" | ||
|
||
//"github.com/surendratiwari3/paota/example/task" | ||
"github.com/surendratiwari3/paota/logger" | ||
"os" | ||
) | ||
|
||
type printWorker struct { | ||
workerPool workerpool.Pool | ||
} | ||
|
||
func main() { | ||
logrusLog := logrus.StandardLogger() | ||
logrusLog.SetFormatter(&logrus.JSONFormatter{}) | ||
logrusLog.SetReportCaller(true) | ||
|
||
logger.ApplicationLogger = logrusLog | ||
|
||
cnf := config.Config{ | ||
Broker: "amqp", | ||
//Store: "null", | ||
TaskQueueName: "paota_task_queue", | ||
AMQP: &config.AMQPConfig{ | ||
Url: "amqp://guest:guest@localhost:5672/", | ||
Check failure Code scanning / SonarCloud AMQP credentials should not be disclosed High
Make sure these Rabbit MQ credentials get revoked, changed, and removed from the code. See more on SonarCloud
|
||
Exchange: "paota_task_exchange", | ||
ExchangeType: "direct", | ||
BindingKey: "consumer_publisher", | ||
PrefetchCount: 100, | ||
ConnectionPoolSize: 10, | ||
DelayedQueue: "delay_consumer_publisher", | ||
}, | ||
} | ||
|
||
newWorkerPool, err := workerpool.NewWorkerPoolWithConfig(context.Background(), 10, "testWorker", cnf) | ||
if err != nil { | ||
logger.ApplicationLogger.Error("workerPool is not created", err) | ||
os.Exit(0) | ||
} else if newWorkerPool == nil { | ||
logger.ApplicationLogger.Info("workerPool is nil") | ||
os.Exit(0) | ||
} | ||
|
||
printWorker := printWorker{workerPool: newWorkerPool} | ||
|
||
logger.ApplicationLogger.Info("newWorkerPool created successfully") | ||
// Register tasks | ||
regTasks := map[string]interface{}{ | ||
"Print": printWorker.Print, | ||
} | ||
err = newWorkerPool.RegisterTasks(regTasks) | ||
if err != nil { | ||
logger.ApplicationLogger.Info("error while registering task") | ||
return | ||
} | ||
logger.ApplicationLogger.Info("Worker is also started") | ||
|
||
err = newWorkerPool.Start() | ||
if err != nil { | ||
logger.ApplicationLogger.Error("error while starting worker") | ||
} | ||
} | ||
|
||
func (wp printWorker) Publish() { | ||
// UserRecord represents the structure of user records. | ||
type UserRecord struct { | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Email string `json:"email"` | ||
// Add other fields as needed | ||
} | ||
// Replace this with the received user record | ||
user := UserRecord{ | ||
ID: "1", | ||
Name: "John Doe", | ||
Email: "john.doe@example.com", | ||
} | ||
|
||
// Convert the struct to a JSON string | ||
userJSON, err := json.Marshal(user) | ||
if err != nil { | ||
// | ||
} | ||
|
||
printJob := &schema.Signature{ | ||
Name: "Print", | ||
Args: []schema.Arg{ | ||
{ | ||
Type: "string", | ||
Value: string(userJSON), | ||
}, | ||
}, | ||
RetryCount: 10, | ||
RoutingKey: "consumer_publisher_", | ||
IgnoreWhenTaskNotRegistered: true, | ||
} | ||
|
||
waitGrp := sync.WaitGroup{} | ||
waitGrp.Add(1) | ||
go func() { | ||
for i := 0; i < 100; i++ { | ||
wp.workerPool.SendTaskWithContext(context.Background(), printJob) | ||
} | ||
waitGrp.Done() | ||
}() | ||
} | ||
|
||
waitGrp.Wait() | ||
|
||
} | ||
|
||
func (wp printWorker) Print(arg *schema.Signature) error { | ||
logger.ApplicationLogger.Info("success") | ||
wp.Publish() | ||
return nil | ||
} |
6 changes: 3 additions & 3 deletions
6
internal/store/backend_interface.go → internal/backend/backend_interface.go
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
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
Oops, something went wrong.