ymlr-redis
TypeScript icon, indicating that this package has built-in type declarations

1.2.2-alpha.5 • Public • Published

ymlr-redis

ymlr-redis for ymlr plugin

Shares


Tag details

Tags Description
ymlr-redis Declare a redis connector
ymlr-redis'job Add a new job with input data to do async
ymlr-redis'onJob Handle jobs which is add by ymlr-redis'job
ymlr-redis'pub Publish a message to channels in redis
ymlr-redis'remove Remove callbacks in channels after subscribing
ymlr-redis'sub Subscribe channels in redis
ymlr-redis'unsub Unsubscribe channels in redis

ymlr-redis

Declare a redis connector

Example:

  - name: "[redis] localhost"
    ymlr-redis:
      uri: redis://user:pass                  # redis uri
      runs:                                   # When a message is received then it will runs them
        - echo: redis is connected

Publish a message to channels

  - name: "[redis] localhost"
    ymlr-redis:
      uri: redis://user:pass                  # redis uri
      runs:                                   # When a message is received then it will runs them
        - name: Publish a message
          ymlr-redis'pub:
            channels:
              - test
            data:
              msg: Hello world

ymlr-redis'job

Add a new job with input data to do async

Example:

  - name: Crop image size
    ymlr-redis'job:
      uri: redis://user:pass
      opts:                               # ioredis options
      name: Crop Image                    # Queue name
      queueOpts:                          # Job queue options (https://docs.bullmq.io)
      jobOpts:                            # Job options (https://docs.bullmq.io)
        removeOnComplete: true
      data:                               # Job data
        url: http://...
        type: jpeg
        size:
          width: 10
          height: 10

Declare global then reused by code

  - name: Crop image size
    id: processImageJobsProxy
    detach: false                         # Dont release connection, keep it's used in background
    ymlr-redis'job:
      uri: redis://user:pass
      opts:                               # ioredis options
      name: Crop Image                    # Queue name
      queueOpts:                          # Job queue options (https://docs.bullmq.io)
      jobOpts:                            # Job options (https://docs.bullmq.io)
        removeOnComplete: true

  - js: |
      await $vars.processImageJobsProxy.$.add({
        url: 'http://...',
        type: 'jpeg',
        size: {
          width: 10,
          height: 10
        }
      }, {
        removeOnFail: true
      })

ymlr-redis'onJob

Handle jobs which is add by ymlr-redis'job

Example:

  - name: Handle to crop image size
    ymlr-redis'onJob:
      uri: redis://user:pass
      opts:                                 # ioredis options
        maxRetriesPerRequest:
      name: Crop Image                      # Queue name
      workerOpts:                           # Job worker options (https://docs.bullmq.io)
        concurrency: 1
      runs:
        - echo: A new job has justed added
        - echo: ${ $parentState.job }       # Job information
        - echo: ${ $parentState.job.data }  # Job data

ymlr-redis'pub

Publish a message to channels in redis

Example:

Publish a message to redis

  - name: "[redis] localhost"
    ymlr-redis'pub:
      uri: redis://user:pass
      channel: channel1
      channels:
        - channel2
        - channel3
      data:
        name: thanh

Reuse redis connection to publish multiple times

  - name: "[redis] localhost"
    ymlr-redis:
      uri: redis://user:pass
      runs:
        - ymlr-redis'pub:
            channels:
              - channel1
            data:
              name: thanh
        - ...
        # Other elements

Or reuse by global variable Reuse redis connection to publish multiple times

  - name: "[redis] localhost"
    ymlr-redis:
      uri: redis://user:pass
    vars:
      redis1: ${this}

  - ymlr-redis'pub:
      redis: ${ $vars.redis1 }
      channels:
        - channel1
      data:
        name: thanh

ymlr-redis'remove

Remove callbacks in channels after subscribing

Example:

  - id: redis
    name: Global Redis
    ymlr-redis:
      uri: redis://user:pass

  - name: "subscribe a channel1"
    ymlr-redis'sub:
      name: test_channel1
      redis: ${ $vars.redis }
      channel: channel1
      runs:
        - echo: callback 01 ${ $parentState.channelName }

  - name: keep subscribing channels but it removes the handler "test_channel1"
    ymlr-redis'unsub: test_channel1
    # ymlr-redis'unsub: [test_channel1, test_channel2]

ymlr-redis'sub

Subscribe channels in redis

Example:

  - name: "[redis] localhost"
    ymlr-redis'sub:
      uri: redis://user:pass
      type: buffer                            # Message type is in [text, buffer]. Default is "text"
      channel: channel1
      channels:                               # channels which is subscribed
        - channel1
        - channel2
      runs:                                   # When a message is received then it will runs them
        - ${ $parentState }                   # - Received data in a channel
        - ${ $parentState.channelName }       # - channel name
        - ${ $parentState.channelData }       # - Received message which is cast to object
        - ${ $parentState.channelMsg }        # - Received message which is text

        - ...
        # Other elements

        - stop:

Used in global redis

  - name: Global Redis
    ymlr-redis:
      uri: redis://user:pass
      runs:
        - name: "[redis] localhost"
          ymlr-redis'sub:
            type: buffer                        # Message type is in [text, buffer]. Default is "text"
            channel: channel1
            channels:                           # channels which is subscribed
              - channel1
              - channel2
            runs:                               # When a message is received then it will runs them
              - ${ $parentState }               # - Received data in a channel
              - ${ $parentState.channelName }   # - Channel name
              - ${ $parentState.channelData }   # - Received message which is cast to object
              - ${ $parentState.channelMsg }    # - Received message which is text or buffer

              - ...
              # Other elements

Or reuse by global variable

  - id: redis
    name: Global Redis
    ymlr-redis:
      uri: redis://user:pass

  - name: "[redis] localhost"
    ymlr-redis'sub:
      name: my-test-channel                 # channel name which to reused when register again
      redis: ${ $vars.redis }
      channel: channel1
      channels:                             # channels which is subscribed
        - channel1
        - channel2
      runs:                                 # When a message is received then it will runs them
        - ${ $parentState }                 # - Received data in a channel
        - ${ $parentState.channelName }     # - channel name
        - ${ $parentState.channelData }     # - Received message which is cast to object
        - ${ $parentState.channelMsg }      # - Received message which is text

        - ...
        # Other elements

        - stop:                             # - Stop subscribing

  - name: "subscribe channels again after unsubcribed"
    ymlr-redis'sub:
      redis: ${ $vars.redis }
      channel: channel1
      channels:                             # channels which is subscribed again
        - channel2

ymlr-redis'unsub

Unsubscribe channels in redis

Example:

  - id: redis
    name: Global Redis
    ymlr-redis:
      uri: redis://user:pass

  - name: "[redis] localhost"
    ymlr-redis'unsub:
      redis: ${ $vars.redis }
      channel: channel1
      channels:                             # channels which are unsubscribed
        - channel1
        - channel2

Have fun :)

Readme

Keywords

Package Sidebar

Install

npm i ymlr-redis

Weekly Downloads

44

Version

1.2.2-alpha.5

License

ISC

Unpacked Size

64 kB

Total Files

40

Last publish

Collaborators

  • circle2jt