class Beetle::Server

Overview

The Server class is the co-ordinator of the Beetle subsystem. The basic process is:

The server listens on the following endpoints:

Defined in:

beetle/server.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

Create a new Server instance.


[View source]

Instance Method Detail

def archiver=(archiver : Archiver) #

Set the optional archiver to be run on completion of a job execution.


[View source]
def listen #

Listen for instructions. The endpoints are described above. The payloads are:

  • /job - A JSON representation of a Job. For example:
{
    "name": "charges_local",
    "host": "http://localhost:3001",
    "run_for_sec": 10,
    "tasks": [
        {
            "task_class": "BCaaS::ChargeTask",
            "weight": 1,
            "sleep_min_msec": 500,
            "sleep_max_msec": 1000
        }
    ]
}

If the request is valid, the job will be stored and available to be run with the /exec request. The response body will simply be Job saved.

  • /exec - A JSON representation of an Exec object. For example:
  {
      "name": "charges_local",
      "params": {
          "sc_mc": "mc"
      }
  }

A valid request will result in the named job being executed synchronously. On completion the response body will contain a JSON representation of a JobExec object. For example:

{
  "id": "77f2d06c-be37-4bdc-98ad-02fed5acd892",
  "name": "charges_local_2",
  "host": "http://localhost:3001",
  "t_start": "2019-02-19 21:36:47.430+00:00",
  "elapsed_usec": 750782,
  "params": {
      "sc_mc": "mc"
  },
  "error_400_on_threshold_exceeded": false,
  "tasks": {
      "BCaaS::ChargeTask": {
          "host": "http://localhost:3001",
          "weight": 1,
          "sleep_min_msec": 500,
          "sleep_max_msec": 1000,
          "threshold_avg_usec": 0,
          "threshold_max_usec": 0,
          "n_task": 2,
          "n_success": 2,
          "n_fail": 0,
          "min_usec": 448,
          "max_usec": 1980,
          "avg_usec": 1214,
          "tot_usec": 2428,
          "executions": [
              {
                  "id": "80b65420-2902-4e44-8676-6e2f620003af",
                  "swarm_id": 1,
                  "t_start": "2019-02-19 21:36:47.430+00:00",
                  "elapsed_usec": 1980,
                  "request_context": "buyer_id: 5066112c-b321-40a0-ac61-a7868b1921ee",
                  "is_success": true
              },
              {
                  "id": "5f173b79-9e11-423b-9fe9-e204c669af3a",
                  "swarm_id": 1,
                  "t_start": "2019-02-19 21:36:48.180+00:00",
                  "elapsed_usec": 448,
                  "request_context": "buyer_id: 45672a98-281a-4ee3-857a-89ea27fa2dcb",
                  "is_success": true
              }
          ],
          "threshold_avg_exceeded": false,
          "threshold_max_exceeded": false
      }
  },
  "run_for_sec": 0,
  "run_n": 2,
  "n_swarm": 1,
  "threshold_exceeded": false
}
  • /tasks This endpoint doesn't require a payload. It returns a JSON representation of the list of registered (via Server#register_task) tasks. For example:
{
  "tasks": [
      "BCaaS::ChargeTask",
      "BCaaS::PreAuthTask"
  ]
}
  • /jobs This endpoint doesn't require a payload. It returns a JSON representation of all the jobs available for execution (via the /exec endpoint). For example:
{
  "charges_local_2": {
      "name": "charges_local_2",
      "host": "http://localhost:3001",
      "tasks": [
          {
              "task_class": "BCaaS::ChargeTask",
              "weight": 1,
              "sleep_min_msec": 500,
              "sleep_max_msec": 1000,
              "threshold_avg_usec": 0,
              "threshold_max_usec": 0,
              "host": "http://localhost:3001"
          }
      ],
      "run_for_sec": 0,
      "run_n": 2,
      "n_swarm": 1
  },
  "charges_staging": {
      "name": "charges_staging",
      "tasks": [
          {
              "task_class": "BCaaS::ChargeTask",
              "weight": 1,
              "sleep_min_msec": 500,
              "sleep_max_msec": 1000,
              "threshold_avg_usec": 0,
              "threshold_max_usec": 0,
              "host": "https://app.bcaas-staging.msts.com/api/v20180807"
          }
      ],
      "run_for_sec": 10,
      "run_n": 0,
      "n_swarm": 1
  },
  "charges_5_staging": {
      "name": "charges_5_staging",
      "tasks": [
          {
              "task_class": "BCaaS::ChargeTask",
              "weight": 1,
              "sleep_min_msec": 0,
              "sleep_max_msec": 0,
              "threshold_avg_usec": 0,
              "threshold_max_usec": 0,
              "host": "https://app.bcaas-staging.msts.com/api/v20180807"
          }
      ],
      "run_for_sec": 10,
      "run_n": 0,
      "n_swarm": 5
  }
}
  • /quit - Terminate the server. No payload is required.

[View source]
def register_task(task_class : Task.class) #

Register a subclass of Task that is available to be part of a Job. If you submit a /job request referencing an unregistered Task class you will receive a 400 error with a message of Unknown Task class(es): ....


[View source]