From f76521914219cc97fcee23b1403a2002de977577 Mon Sep 17 00:00:00 2001 From: arisnguyenit97 Date: Thu, 8 Feb 2024 22:15:38 +0700 Subject: [PATCH] :recycle: refactor: enabled keepalive socket connection and updated codebase CI.CD #19 #14 --- .github/workflows/ci_notify.yml | 111 ++++++++++++++++++++++++++++++++ pkg/ami/ami_event.go | 24 +++---- 2 files changed, 123 insertions(+), 12 deletions(-) create mode 100644 .github/workflows/ci_notify.yml diff --git a/.github/workflows/ci_notify.yml b/.github/workflows/ci_notify.yml new file mode 100644 index 0000000..bba77fc --- /dev/null +++ b/.github/workflows/ci_notify.yml @@ -0,0 +1,111 @@ +name: Notify + +on: + push: + branches: ["master"] + tags: + - "v*" + pull_request: + branches: ["master"] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + notify: + runs-on: ubuntu-latest + needs: build + steps: + - name: Check Secrets + id: check + run: | + if [[ -z "${{ secrets.TELEGRAM_CHAT_ID }}" || -z "${{ secrets.TELEGRAM_BOT_TOKEN }}" ]]; then + echo "::set-output name=skip::true" + else + echo "::set-output name=skip::false" + fi + - name: Send Telegram Notification + if: steps.check.outputs.skip == 'false' + uses: appleboy/telegram-action@master + with: + to: ${{ secrets.TELEGRAM_CHAT_ID }} + token: ${{ secrets.TELEGRAM_BOT_TOKEN }} + format: markdown + message: | + 🚀 CI Commit + 🔯 `${{ github.actor }}` created commit: + - message: `${{ github.event.commits[0].message }}` + - hash: `${{ github.sha }}` + - repository: `${{ github.repository }}` + - head: `${{ github.event.head_commit.message }}` + 🍀 See changes: `https://github.com/${{ github.repository }}/commit/${{github.sha}}` + + deploy: + runs-on: ubuntu-latest + if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') + needs: build + steps: + - name: Checkout repository + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Check if tag exists + id: check_tag + run: | + if [ -n "$GITHUB_REF" ]; then + TAG=${GITHUB_REF#refs/tags/} + # echo "::set-output name=tag::$TAG" + echo "TAG=${TAG}" >> $GITHUB_ENV + else + # echo "::set-output name=tag::" + echo "TAG=" >> $GITHUB_ENV + fi + shell: bash + + - name: Check Secrets + id: check + run: | + if [[ -z "${{ secrets.TELEGRAM_CHAT_ID }}" || -z "${{ secrets.TELEGRAM_BOT_TOKEN }}" ]]; then + echo "::set-output name=skip::true" + else + echo "::set-output name=skip::false" + fi + + - name: Generate Changelog + id: changelog + run: | + # Generate your changelog here and set it as an output variable + CHANGELOG=$(git log --pretty=format:"%h - %s" -n 10) + echo "::set-output name=changelog::$CHANGELOG" + + - name: Create GitHub Release + id: create_release + uses: softprops/action-gh-release@v1 + with: + tag_name: ${{ env.TAG }} + body: | + :gem: released new version ${{ env.TAG }} + draft: false + prerelease: false + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Send Telegram Notification + if: steps.check.outputs.skip == 'false' + uses: appleboy/telegram-action@master + with: + to: ${{ secrets.TELEGRAM_CHAT_ID }} + token: ${{ secrets.TELEGRAM_BOT_TOKEN }} + format: markdown + message: | + 🚀 New tag created: **${{ env.TAG }}** + 🔯 `${{ github.actor }}` created tag: + - repository: `${{ github.repository }}` + - head: `${{ github.event.head_commit.message }}` + 🍀 See changes: `https://github.com/${{ github.repository }}/releases/tag/${{ env.TAG }}` + 📜 Changelog: + `${{ steps.changelog.outputs.changelog }}` diff --git a/pkg/ami/ami_event.go b/pkg/ami/ami_event.go index 1725b67..eb26745 100644 --- a/pkg/ami/ami_event.go +++ b/pkg/ami/ami_event.go @@ -185,7 +185,7 @@ func (e *AMIEvent) OpenFullEvents(c *AMI) { log.Printf("ami event: '%s' received: %s", message.Field(strings.ToLower(config.AmiEventKey)), message.Json()) case err := <-c.Error(): c.Close() - log.Printf("ami listener has error occurred: %s", err.Error()) + e.Reconnect(c, err) } } } @@ -201,13 +201,13 @@ func (e *AMIEvent) OpenFullEventsTranslator(c *AMI, d *AMIDictionary) { log.Printf("ami event: '%s' received: %s", message.Field(strings.ToLower(config.AmiEventKey)), message.JsonTranslator(d)) case err := <-c.Error(): c.Close() - log.Printf("ami listener has error occurred: %s", err.Error()) + e.Reconnect(c, err) } } } // Listen All Events Callback with translator dictionary -func (e *AMIEvent) OpenFullEventsCallbackTranslator(c *AMI, d *AMIDictionary, callback func(*AMIMessage, string, error)) { +func (e *AMIEvent) OpenFullEventsCallbackTranslator(c *AMI, d *AMIDictionary, callback func(msg *AMIMessage, json string, e error)) { all := c.AllEvents() defer c.Close() for { @@ -217,7 +217,7 @@ func (e *AMIEvent) OpenFullEventsCallbackTranslator(c *AMI, d *AMIDictionary, ca callback(message, message.JsonTranslator(d), nil) case err := <-c.Error(): c.Close() - log.Printf("ami listener has error occurred: %s", err.Error()) + e.Reconnect(c, err) callback(nil, err.Error(), err) } } @@ -234,7 +234,7 @@ func (e *AMIEvent) OpenEvent(c *AMI, name string) { log.Printf("ami event: '%s' received: %s", name, message.Json()) case err := <-c.Error(): c.Close() - log.Printf("ami listener event: '%s' has error occurred: %s", name, err.Error()) + e.Reconnect(c, err) } } } @@ -250,13 +250,13 @@ func (e *AMIEvent) OpenEventTranslator(c *AMI, d *AMIDictionary, name string) { log.Printf("ami event: '%s' received: %s", name, message.JsonTranslator(d)) case err := <-c.Error(): c.Close() - log.Printf("ami listener event: '%s' has error occurred: %s", name, err.Error()) + e.Reconnect(c, err) } } } // Listen Event Callback key name -func (e *AMIEvent) OpenEventCallbackTranslator(c *AMI, d *AMIDictionary, name string, callback func(*AMIMessage, string, error)) { +func (e *AMIEvent) OpenEventCallbackTranslator(c *AMI, d *AMIDictionary, name string, callback func(msg *AMIMessage, json string, e error)) { event := c.OnEvent(name) defer c.Close() for { @@ -266,7 +266,7 @@ func (e *AMIEvent) OpenEventCallbackTranslator(c *AMI, d *AMIDictionary, name st callback(message, message.JsonTranslator(d), nil) case err := <-c.Error(): c.Close() - log.Printf("ami listener event: '%s' has error occurred: %s", name, err.Error()) + e.Reconnect(c, err) callback(nil, err.Error(), err) } } @@ -283,7 +283,7 @@ func (e *AMIEvent) OpenEvents(c *AMI, keys ...string) { log.Printf("ami event(s): '%s' received: %s", keys, message.Json()) case err := <-c.Error(): c.Close() - log.Printf("ami listener event(s): '%s' has error occurred: %s", keys, err.Error()) + e.Reconnect(c, err) } } } @@ -299,13 +299,13 @@ func (e *AMIEvent) OpenEventsTranslator(c *AMI, d *AMIDictionary, keys ...string log.Printf("ami event(s): '%s' received: %s", keys, message.JsonTranslator(d)) case err := <-c.Error(): c.Close() - log.Printf("ami listener event(s): '%s' has error occurred: %s", keys, err.Error()) + e.Reconnect(c, err) } } } // Listen Events Callback by collect of keys string -func (e *AMIEvent) OpenEventsCallbackTranslator(c *AMI, d *AMIDictionary, callback func(*AMIMessage, string, error), keys ...string) { +func (e *AMIEvent) OpenEventsCallbackTranslator(c *AMI, d *AMIDictionary, callback func(msg *AMIMessage, json string, e error), keys ...string) { event := c.OnEvents(keys...) defer c.Close() for { @@ -315,7 +315,7 @@ func (e *AMIEvent) OpenEventsCallbackTranslator(c *AMI, d *AMIDictionary, callba callback(message, message.JsonTranslator(d), nil) case err := <-c.Error(): c.Close() - log.Printf("ami listener event(s): '%s' has error occurred: %s", keys, err.Error()) + e.Reconnect(c, err) callback(nil, err.Error(), err) } }