forked from cs3org/reva
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add serverless services * Load serverless services * Start serverless services on launch * Codacy changes * Changelog * Example serverless config * Add example serverless service * Add service name to logger Co-authored-by: Gianmaria Del Monte <g.macmount@gmail.com> * Simplify function call Co-authored-by: Gianmaria Del Monte <g.macmount@gmail.com> * Exit with errors if initserverless fails Co-authored-by: Gianmaria Del Monte <g.macmount@gmail.com> * Add signal handling to serverless services * Use context to pass timeout on service stop --------- Co-authored-by: Gianmaria Del Monte <g.macmount@gmail.com>
- Loading branch information
Showing
9 changed files
with
394 additions
and
7 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,7 @@ | ||
Enhancement: Serverless Services | ||
|
||
New type of service (along with http and grpc) | ||
which does not have a listening server. Useful for | ||
the notifications service and others in the future. | ||
|
||
https://github.com/cs3org/reva/pull/3824 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
[log] | ||
output = "/var/log/revad/revad-notifications.log" | ||
mode = "json" | ||
|
||
[shared] | ||
gatewaysvc = "localhost:19000" | ||
jwt_secret = "Pive-Fumkiu4" | ||
skip_user_groups_in_token = true | ||
|
||
[serverless.services.notifications] | ||
nats_address = "nats-server-01.example.com" | ||
nats_token = "secret-token-example" | ||
nats_template_subject = "reva-notifications-template" | ||
nats_notification_subject = "reva-notifications-notification" | ||
nats_trigger_subject = "reva-notifications-trigger" | ||
storage_driver = "sql" | ||
grouping_interval = 60 | ||
grouping_maxsize = 100 | ||
|
||
[serverless.services.notifications.storage_drivers.sql] | ||
db_username = "username" | ||
db_password = "password" | ||
db_host = "database.example.com" | ||
db_port = 3306 | ||
db_name = "notifications" | ||
|
||
[serverless.services.notifications.handlers.email] | ||
smtp_server = "mx.example.com:25" | ||
disable_auth = true | ||
default_sender = "noreply@cernbox.cern.ch" | ||
|
||
[tracing] |
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,98 @@ | ||
// Copyright 2018-2023 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package helloworld | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/cs3org/reva/pkg/rserverless" | ||
"github.com/mitchellh/mapstructure" | ||
"github.com/rs/zerolog" | ||
) | ||
|
||
type config struct { | ||
Outfile string `mapstructure:"outfile"` | ||
} | ||
|
||
func (c *config) init() { | ||
if c.Outfile == "" { | ||
c.Outfile = "/tmp/revad-helloworld-hello" | ||
} | ||
} | ||
|
||
type svc struct { | ||
conf *config | ||
file *os.File | ||
log *zerolog.Logger | ||
} | ||
|
||
func init() { | ||
rserverless.Register("helloworld", New) | ||
} | ||
|
||
// New returns a new helloworld service. | ||
func New(m map[string]interface{}, log *zerolog.Logger) (rserverless.Service, error) { | ||
conf := &config{} | ||
conf.init() | ||
|
||
if err := mapstructure.Decode(m, conf); err != nil { | ||
return nil, err | ||
} | ||
|
||
file, err := os.OpenFile(conf.Outfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
log.Err(err) | ||
return nil, err | ||
} | ||
|
||
s := &svc{ | ||
conf: conf, | ||
log: log, | ||
file: file, | ||
} | ||
|
||
return s, nil | ||
} | ||
|
||
// Start starts the helloworld service. | ||
func (s *svc) Start() { | ||
s.log.Debug().Msgf("helloworld server started, saying hello at %s", s.conf.Outfile) | ||
go s.sayHello(s.conf.Outfile) | ||
} | ||
|
||
// Close stops the helloworld service. | ||
func (s *svc) Close(ctx context.Context) error { | ||
return s.file.Close() | ||
} | ||
|
||
func (s *svc) sayHello(filename string) { | ||
for { | ||
s.log.Info().Msg("saying hello") | ||
h := fmt.Sprintf("%s - hello world!\n", time.Now().String()) | ||
|
||
_, err := s.file.Write([]byte(h)) | ||
if err != nil { | ||
s.log.Err(err) | ||
} | ||
time.Sleep(5 * time.Second) | ||
} | ||
} |
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,25 @@ | ||
// Copyright 2018-2023 CERN | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package loader | ||
|
||
import ( | ||
// Load core serverless services. | ||
_ "github.com/cs3org/reva/internal/serverless/services/helloworld" | ||
// Add your own service here. | ||
) |
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.