|
2 | 2 | package volumes |
3 | 3 |
|
4 | 4 | import ( |
| 5 | + "context" |
5 | 6 | "encoding/json" |
6 | 7 | "fmt" |
| 8 | + "time" |
7 | 9 |
|
8 | 10 | drycc "github.com/drycc/controller-sdk-go" |
9 | 11 | "github.com/drycc/controller-sdk-go/api" |
@@ -78,6 +80,41 @@ func Expand(c *drycc.Client, appID string, volume api.Volume) (api.Volume, error |
78 | 80 | return newVolume, reqErr |
79 | 81 | } |
80 | 82 |
|
| 83 | +// Serve serves an app's volume. |
| 84 | +func Serve(parent context.Context, c *drycc.Client, appID, name string) (context.Context, map[string]string, error) { |
| 85 | + u := fmt.Sprintf("/v2/apps/%s/volumes/%s/filer/_/bind", appID, name) |
| 86 | + res, err := c.Request("POST", u, nil) |
| 87 | + if err != nil { |
| 88 | + return nil, nil, err |
| 89 | + } |
| 90 | + defer res.Body.Close() |
| 91 | + var filer map[string]string |
| 92 | + if err = json.NewDecoder(res.Body).Decode(&filer); err != nil { |
| 93 | + return nil, nil, err |
| 94 | + } |
| 95 | + ctx, cancel := context.WithCancel(parent) |
| 96 | + go func() { |
| 97 | + defer cancel() |
| 98 | + for { |
| 99 | + select { |
| 100 | + case <-parent.Done(): |
| 101 | + return |
| 102 | + default: |
| 103 | + u := fmt.Sprintf("/v2/apps/%s/volumes/%s/filer/_/ping", appID, name) |
| 104 | + res, err := c.Request("GET", u, nil) |
| 105 | + if err != nil { |
| 106 | + return |
| 107 | + } |
| 108 | + if err := res.Body.Close(); err != nil { |
| 109 | + return |
| 110 | + } |
| 111 | + time.Sleep(time.Second * 30) |
| 112 | + } |
| 113 | + } |
| 114 | + }() |
| 115 | + return ctx, filer, nil |
| 116 | +} |
| 117 | + |
81 | 118 | // Delete delete an app's Volume. |
82 | 119 | func Delete(c *drycc.Client, appID string, name string) error { |
83 | 120 | u := fmt.Sprintf("/v2/apps/%s/volumes/%s/", appID, name) |
|
0 commit comments