-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move crontab job to temporal workflow (#202)
Co-authored-by: 泽华 <[email protected]>
- Loading branch information
1 parent
2d54ab0
commit 9e84a56
Showing
11 changed files
with
229 additions
and
71 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,19 @@ | ||
package activity | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"opencsg.com/csghub-server/common/config" | ||
"opencsg.com/csghub-server/component" | ||
) | ||
|
||
func CalcRecomScore(ctx context.Context, config *config.Config) error { | ||
c, err := component.NewRecomComponent(config) | ||
if err != nil { | ||
slog.Error("failed to create recom component", "err", err) | ||
return err | ||
} | ||
c.CalculateRecomScore(context.Background()) | ||
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,28 @@ | ||
package activity | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
|
||
"opencsg.com/csghub-server/builder/multisync" | ||
"opencsg.com/csghub-server/builder/store/database" | ||
"opencsg.com/csghub-server/common/config" | ||
"opencsg.com/csghub-server/component" | ||
) | ||
|
||
func SyncAsClient(ctx context.Context, config *config.Config) error { | ||
c, err := component.NewMultiSyncComponent(config) | ||
if err != nil { | ||
slog.Error("failed to create multi sync component", "err", err) | ||
return err | ||
} | ||
syncClientSettingStore := database.NewSyncClientSettingStore() | ||
setting, err := syncClientSettingStore.First(ctx) | ||
if err != nil { | ||
slog.Error("failed to find sync client setting", "error", err) | ||
return err | ||
} | ||
apiDomain := config.MultiSync.SaasAPIDomain | ||
sc := multisync.FromOpenCSG(apiDomain, setting.Token) | ||
return c.SyncAsClient(ctx, sc) | ||
} |
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,32 @@ | ||
package workflow | ||
|
||
import ( | ||
"time" | ||
|
||
"go.temporal.io/sdk/temporal" | ||
"go.temporal.io/sdk/workflow" | ||
"opencsg.com/csghub-server/api/workflow/activity" | ||
"opencsg.com/csghub-server/common/config" | ||
) | ||
|
||
func CalcRecomScoreWorkflow(ctx workflow.Context, config *config.Config) error { | ||
logger := workflow.GetLogger(ctx) | ||
logger.Info("calc recom score workflow started") | ||
|
||
retryPolicy := &temporal.RetryPolicy{ | ||
MaximumAttempts: 3, | ||
} | ||
|
||
options := workflow.ActivityOptions{ | ||
StartToCloseTimeout: time.Hour * 1, | ||
RetryPolicy: retryPolicy, | ||
} | ||
|
||
ctx = workflow.WithActivityOptions(ctx, options) | ||
err := workflow.ExecuteActivity(ctx, activity.CalcRecomScore, config).Get(ctx, nil) | ||
if err != nil { | ||
logger.Error("failed to calc recom score", "error", err) | ||
return err | ||
} | ||
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,32 @@ | ||
package workflow | ||
|
||
import ( | ||
"time" | ||
|
||
"go.temporal.io/sdk/temporal" | ||
"go.temporal.io/sdk/workflow" | ||
"opencsg.com/csghub-server/api/workflow/activity" | ||
"opencsg.com/csghub-server/common/config" | ||
) | ||
|
||
func SyncAsClientWorkflow(ctx workflow.Context, config *config.Config) error { | ||
logger := workflow.GetLogger(ctx) | ||
logger.Info("sync as client workflow started") | ||
|
||
retryPolicy := &temporal.RetryPolicy{ | ||
MaximumAttempts: 3, | ||
} | ||
|
||
options := workflow.ActivityOptions{ | ||
StartToCloseTimeout: time.Hour * 1, | ||
RetryPolicy: retryPolicy, | ||
} | ||
|
||
ctx = workflow.WithActivityOptions(ctx, options) | ||
err := workflow.ExecuteActivity(ctx, activity.SyncAsClient, config).Get(ctx, nil) | ||
if err != nil { | ||
logger.Error("failed to sync as client", "error", err) | ||
return err | ||
} | ||
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,88 @@ | ||
package workflow | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
enumspb "go.temporal.io/api/enums/v1" | ||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/worker" | ||
"opencsg.com/csghub-server/api/workflow/activity" | ||
"opencsg.com/csghub-server/common/config" | ||
) | ||
|
||
const ( | ||
AlreadyScheduledMessage = "schedule with this ID is already registered" | ||
CronJobQueueName = "workflow_cron_queue" | ||
) | ||
|
||
func RegisterCronJobs(config *config.Config) error { | ||
var err error | ||
if wfClient == nil { | ||
wfClient, err = client.Dial(client.Options{ | ||
HostPort: config.WorkFLow.Endpoint, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("unable to create workflow client, error:%w", err) | ||
} | ||
} | ||
|
||
if !config.Saas { | ||
_, err = wfClient.ScheduleClient().Create(context.Background(), client.ScheduleOptions{ | ||
ID: "sync-as-client-schedule", | ||
Spec: client.ScheduleSpec{ | ||
CronExpressions: []string{config.CronJob.SyncAsClientCronExpression}, | ||
}, | ||
Overlap: enumspb.SCHEDULE_OVERLAP_POLICY_SKIP, | ||
Action: &client.ScheduleWorkflowAction{ | ||
ID: "sync-as-client-workflow", | ||
TaskQueue: CronJobQueueName, | ||
Workflow: SyncAsClientWorkflow, | ||
Args: []interface{}{config}, | ||
}, | ||
}) | ||
if err != nil && err.Error() != AlreadyScheduledMessage { | ||
return fmt.Errorf("unable to create schedule, error:%w", err) | ||
} | ||
} | ||
|
||
_, err = wfClient.ScheduleClient().Create(context.Background(), client.ScheduleOptions{ | ||
ID: "calc-recom-score-schedule", | ||
Spec: client.ScheduleSpec{ | ||
CronExpressions: []string{config.CronJob.CalcRecomScoreCronExpression}, | ||
}, | ||
Overlap: enumspb.SCHEDULE_OVERLAP_POLICY_SKIP, | ||
Action: &client.ScheduleWorkflowAction{ | ||
ID: "calc-recom-score-workflow", | ||
TaskQueue: CronJobQueueName, | ||
Workflow: CalcRecomScoreWorkflow, | ||
Args: []interface{}{config}, | ||
}, | ||
}) | ||
if err != nil && err.Error() != AlreadyScheduledMessage { | ||
return fmt.Errorf("unable to create schedule, error:%w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func StartCronWorker(config *config.Config) error { | ||
var err error | ||
if wfClient == nil { | ||
wfClient, err = client.Dial(client.Options{ | ||
HostPort: config.WorkFLow.Endpoint, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("unable to create workflow client, error:%w", err) | ||
} | ||
} | ||
wfWorker = worker.New(wfClient, CronJobQueueName, worker.Options{}) | ||
if !config.Saas { | ||
wfWorker.RegisterWorkflow(SyncAsClientWorkflow) | ||
wfWorker.RegisterActivity(activity.SyncAsClient) | ||
} | ||
wfWorker.RegisterWorkflow(CalcRecomScoreWorkflow) | ||
wfWorker.RegisterActivity(activity.CalcRecomScore) | ||
|
||
return wfWorker.Start() | ||
} |
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
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