Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error hook payload outputs to stdout/stderr. #59

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 20 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Response example:
"queue_size": 0,
"retry_queue_size": 0,
"workers_queue_size": 0,
"cmdq_queue_size": 0,
"error_queue_size": 0,
"retry_count": 0,
"req_count": 0,
"sent_count": 0,
Expand All @@ -143,7 +143,7 @@ start\_at | The time of started
queue\_size | queue size of requests
retry\_queue\_size | queue size for resending notification
workers\_queue\_size | summary of worker's queue size
command\_queue\_size | error hook command queue size
error\_queue\_size | error hook command queue size
retry\_count | summary of retry count
request\_count | request count to gunfish
err\_count | count of recieving error response
Expand Down Expand Up @@ -183,21 +183,24 @@ api_key = "API key for FCM"

param | status | description
---------------- | ------ | --------------------------------------------------------------------------------------
port |optional| Listen port number.
worker_num |optional| Number of Gunfish owns http clients.
queue_size |optional| Limit number of posted JSON from the developer application.
max_request_size |optional| Limit size of Posted JSON array.
max_connections |optional| Max connections
key_file |required| The key file path.
cert_file |optional| The cert file path.
kid |optional| kid for APNs provider authentication token.
team_id |optional| team id for APNs provider authentication token.
error_hook |optional| Error hook command. This command runs when Gunfish catches an error response.
api_key |optional| FCM api key. If you want to delivery notifications to android, it is required.

## Error Hook

Error hook command can get an each error response with JSON format by STDIN.
port |optional| Listen port number.
worker\_num |optional| Number of Gunfish owns http clients.
queue_size |optional| Limit number of posted JSON from the developer application.
max\_request\_size |optional| Limit size of Posted JSON array.
max\_connections |optional| Max connections
key\_file |required| The key file path.
cert\_file |optional| The cert file path.
kid |optional| kid for APNs provider authentication token.
team\_id |optional| team id for APNs provider authentication token.
error\_hook |optional| Error hook command. This command runs when Gunfish catches an error response.
error\_hook\_command\_persistent |optional|Persist error hook command.
error\_hook\_to |optional| Error hook outputs into stdout/stderr.
api\_key |optional| FCM api key. If you want to delivery notifications to android, it is required.

## Error Hook payload

Error hook command/stdout/stderr will get an each error response with JSON format.
The command accepts a payload by STDIN.

for example JSON structure: (>= v0.2.x)
```json5
Expand Down Expand Up @@ -265,41 +268,9 @@ $ go get github.com/lestrrat/go-server-starter/cmd/start_server
$ start_server --port 38003 --pid-file gunfish.pid -- ./gunfish -c conf/gunfish.toml
```

## Customize

### How to Implement Response Handlers

If you have to handle something on error or on success, you should implement error or success handlers.
For example handlers you should implement is given below:

```go
type CustomYourErrorHandler struct {
hookCmd string
}

func (ch CustomYourErrorHandler) OnResponse(result Result){
// ...
}

func (ch CustomYourErrorHandler) HookCmd( ) string {
return ch.hookCmd
}
```

Then you can use these handlers to set before to start gunfish server `( gunfish.StartServer( Config, Environment ) )`.

```go
InitErrorResponseHandler(CustomYourErrorHandler{hookCmd: "echo 'on error!'"})
```

You can implement a success custom handler in the same way but a hook command is not executed in the success handler in order not to make cpu resource too tight.

### Test

Requires [dep](https://github.com/golang/dep/) for vendoring.

```
$ make get-deps
$ make test
```

Expand Down
16 changes: 9 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ type Config struct {

// SectionProvider is Gunfish provider configuration
type SectionProvider struct {
WorkerNum int `toml:"worker_num"`
QueueSize int `toml:"queue_size"`
RequestQueueSize int `toml:"max_request_size"`
Port int `toml:"port"`
DebugPort int
MaxConnections int `toml:"max_connections"`
ErrorHook string `toml:"error_hook"`
WorkerNum int `toml:"worker_num"`
QueueSize int `toml:"queue_size"`
RequestQueueSize int `toml:"max_request_size"`
Port int `toml:"port"`
DebugPort int
MaxConnections int `toml:"max_connections"`
ErrorHook string `toml:"error_hook"`
ErrorHookTo string `toml:"error_hook_to"`
ErrorHookCommandPersistent bool `toml:"error_hook_command_persistent"`
}

// SectionApns is the configure which is loaded from gunfish.toml
Expand Down
11 changes: 2 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ type Provider struct {
// Therefore, you can specifies hook command which is set at toml file.
type ResponseHandler interface {
OnResponse(Result)
HookCmd() string
}

// DefaultResponseHandler is the default ResponseHandler if not specified.
Expand All @@ -46,12 +45,6 @@ type DefaultResponseHandler struct {
func (rh DefaultResponseHandler) OnResponse(result Result) {
}

// HookCmd returns hook command to execute after getting response from APNS
// only when to get error response.
func (rh DefaultResponseHandler) HookCmd() string {
return rh.Hook
}

// StartServer starts an apns provider server on http.
func StartServer(conf config.Config, env Environment) {
// Initialize DefaultResponseHandler if response handlers are not defined.
Expand All @@ -60,7 +53,7 @@ func StartServer(conf config.Config, env Environment) {
}

if errorResponseHandler == nil {
InitErrorResponseHandler(DefaultResponseHandler{Hook: conf.Provider.ErrorHook})
InitErrorResponseHandler(DefaultResponseHandler{})
}

// Init Provider
Expand Down Expand Up @@ -337,7 +330,7 @@ func (prov *Provider) StatsHandler() http.HandlerFunc {
atomic.StoreInt64(&(srvStats.QueueSize), int64(len(prov.Sup.queue)))
atomic.StoreInt64(&(srvStats.RetryQueueSize), int64(len(prov.Sup.retryq)))
atomic.StoreInt64(&(srvStats.WorkersQueueSize), int64(wqs))
atomic.StoreInt64(&(srvStats.CommandQueueSize), int64(len(prov.Sup.cmdq)))
atomic.StoreInt64(&(srvStats.ErrorQueueSize), int64(len(prov.Sup.errq)))
res.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(res)
err := encoder.Encode(srvStats.GetStats())
Expand Down
2 changes: 1 addition & 1 deletion stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Stats struct {
QueueSize int64 `json:"queue_size"`
RetryQueueSize int64 `json:"retry_queue_size"`
WorkersQueueSize int64 `json:"workers_queue_size"`
CommandQueueSize int64 `json:"cmdq_queue_size"`
ErrorQueueSize int64 `json:"error_queue_size"`
RetryCount int64 `json:"retry_count"`
RequestCount int64 `json:"req_count"`
SentCount int64 `json:"sent_count"`
Expand Down
Loading