Spaces:
Paused
Paused
ML Intern deploy
Deploy httpSMS gateway to Hugging Face Spaces (Docker SDK): nginx :7860 -> Go API :8000, Nuxt SPA, Android APK, bootstrap-mode entrypoint
83a59ca Download api/pkg/services/google_cloud_push_queue_service.go from red-kit/httpsms-gateway: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/services/google_cloud_push_queue_service.go
- Command line
-
hf download hf://spaces/red-kit/httpsms-gateway@83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/services/google_cloud_push_queue_service.go
-
curl -L -o google_cloud_push_queue_service.go https://huggingface.co/spaces/red-kit/httpsms-gateway/resolve/83a59cae175e3c9a43b6f5d3f8749ca61b6b5072/api/pkg/services/google_cloud_push_queue_service.go
3.1 kB
| package services | |
| import ( | |
| "context" | |
| "fmt" | |
| "net/http" | |
| "time" | |
| "github.com/avast/retry-go/v5" | |
| cloudtasks "cloud.google.com/go/cloudtasks/apiv2" | |
| "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb" | |
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | |
| "github.com/NdoleStudio/stacktrace" | |
| "google.golang.org/protobuf/types/known/timestamppb" | |
| ) | |
| type googlePushQueue struct { | |
| queueConfig PushQueueConfig | |
| logger telemetry.Logger | |
| tracer telemetry.Tracer | |
| client *cloudtasks.Client | |
| } | |
| // NewGooglePushQueue creates a new googlePushQueue | |
| func NewGooglePushQueue( | |
| logger telemetry.Logger, | |
| tracer telemetry.Tracer, | |
| client *cloudtasks.Client, | |
| queueConfig PushQueueConfig, | |
| ) PushQueue { | |
| return &googlePushQueue{ | |
| client: client, | |
| tracer: tracer, | |
| logger: logger, | |
| queueConfig: queueConfig, | |
| } | |
| } | |
| // Enqueue a task to the queue | |
| func (queue *googlePushQueue) Enqueue(ctx context.Context, task *PushQueueTask, timeout time.Duration) (queueID string, err error) { | |
| err = retry.New(retry.Attempts(3)).Do(func() error { | |
| queueID, err = queue.enqueueImpl(ctx, task, timeout) | |
| return err | |
| }) | |
| return queueID, err | |
| } | |
| // enqueueImpl a task to the queue | |
| func (queue *googlePushQueue) enqueueImpl(ctx context.Context, task *PushQueueTask, timeout time.Duration) (queueID string, err error) { | |
| ctx, span := queue.tracer.Start(ctx) | |
| defer span.End() | |
| ctxLogger := queue.tracer.CtxLogger(queue.logger, span) | |
| headers := map[string]string{"Content-Type": "application/json"} | |
| for key, value := range task.Headers { | |
| headers[key] = value | |
| } | |
| // Build the Task payload. | |
| req := &cloudtaskspb.CreateTaskRequest{ | |
| Parent: queue.queueConfig.Name, | |
| Task: &cloudtaskspb.Task{ | |
| MessageType: &cloudtaskspb.Task_HttpRequest{ | |
| HttpRequest: &cloudtaskspb.HttpRequest{ | |
| Headers: headers, | |
| HttpMethod: queue.httpMethodToProtoHTTPMethod(task.Method), | |
| Url: task.URL, | |
| }, | |
| }, | |
| ScheduleTime: timestamppb.New(time.Now().UTC().Add(timeout)), | |
| }, | |
| } | |
| // Add a payload message if one is present. | |
| req.Task.GetHttpRequest().Body = task.Body | |
| requestCtx, cancel := context.WithTimeout(ctx, time.Second) | |
| defer cancel() | |
| queueTask, err := queue.client.CreateTask(requestCtx, req) | |
| if err != nil { | |
| ctxLogger.Error(stacktrace.Propagatef(err, "cannot schedule task [%s] to URL [%s]", string(task.Body), task.URL)) | |
| return queueID, queue.tracer.WrapErrorSpan(span, stacktrace.Propagatef(err, "cannot schedule task [%s] to URL [%s]", string(task.Body), task.URL)) | |
| } | |
| ctxLogger.Info(fmt.Sprintf( | |
| "item added to [%s] queue with id [%s] and schedule [%s]", | |
| queue.queueConfig.Name, | |
| queueTask.Name, | |
| queueTask.GetScheduleTime().AsTime(), | |
| )) | |
| return queueTask.Name, nil | |
| } | |
| func (queue *googlePushQueue) httpMethodToProtoHTTPMethod(httpMethod string) cloudtaskspb.HttpMethod { | |
| method, ok := map[string]cloudtaskspb.HttpMethod{ | |
| http.MethodGet: cloudtaskspb.HttpMethod_GET, | |
| http.MethodPost: cloudtaskspb.HttpMethod_POST, | |
| http.MethodPut: cloudtaskspb.HttpMethod_PUT, | |
| }[httpMethod] | |
| if !ok { | |
| return cloudtaskspb.HttpMethod_POST | |
| } | |
| return method | |
| } | |