Skip to content

Kobo Hub Client Worker by kobo admin howto

Tomas Mlcoch edited this page Dec 21, 2016 · 1 revision

Kobo Hub-Client-Worker by kobo-admin howto

  • The Hub - Central server to coordinate workers. It provides XML-RPC methods for use by clients and workers.
  • The client - Command line tool to control a hub (Add task, ...).
  • The Worker - Command line program to process tasks from a hub.

0) Create a directory

In this example directory '''hcw-project''' is used (hcv stands for hub-client-worker).

$ mkdir ~/hcw-project
$ cd ~/hcw-project

1) Start a hub project

In this example '''my_hub''' name is used.BR

$ kobo-admin start-hub my_hub

Update my_hub/settings.py

Set sqlite3 as an database engine.BR

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': '/home/<your-username>/hcw-project/my_hub/db',
        'USER': '',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    }
}
...
FILES_PATH = '~/hcw-project/my_hub/files'

Note: Path to sqlite3 database could be relative too. But in case use with the Apache server there is necessary use an absolute path.

Sync database

$ cd my_hub
$ python manage.py syncdb
... Create a superuser (e.g. admin:admin just for simplicity) ...

Run hub

$ python manage.py runserver

Hub configuration

Open http://127.0.0.1:8000/ in a web browser.BR Select login (upper right corner) and log in with the superuser credentials.BR '''Add an user to the hub''BR Select admin (upper right corner) -> Users -> Add user.BR Add an user "foouser" with password "foo12345" and click "Save"BR '''Add a worker to the hub''BR Return to the admin home page (http://127.0.0.1:8000/admin/) and select workers -> Add worker.BR Leave "Worker key" blank and fill "Name" with '''FQDN - Fully Qualified Domain Name''' (output of a command "hostname --fqdn")BR Add Arch "noarch" and Channel "default" (at the bottom of page) and click "Save".BR Click on the created worker and copy a value from a "Worker key" field. We'll need it later.BR

2) Start a worker project

Open a new terminal and run:

$ cd ~/hcw-project
$ kobo-admin start-worker myfirstworker

Configure the worker

$ cd myfirstworker/
$ export MYFIRSTWORKER_CONFIG_FILE=~/hcw-project/myfirstworker/myfirstworker.conf

Edit myfirstworker.conf

AUTH_METHOD = "worker_key"
...
WORKER_KEY = "qrrMevJHhaf61f8k7iDlvC0OP93MvMtQO5Och2BPS8FBQokqP4B6CqiQJhh4ILsy"
#             ^^^^ Use the worker key from administration ^^^^^^^^^^^^^^^^^^^^
...
PID_FILE = "/home/<your-username>/hcw-project/myfirstworker/myfirstworker.pid"
...
LOG_FILE = "/home/<your-username>/hcw-project/myfirstworker/myfirstworker.log"

Create a worker task

$ cd tasks/
$ kobo-admin start-worker-task add_two_numbers

Modify the task (file: task_add_two_numbers.py)

    arches = ["noarch"]
    channels = ["default"]
...
    def run(self):
        num_1 = self.args["a"]
        num_2 = self.args["b"]
        self.result = str(int(num_1)+int(num_2))
...

Run the worker

$ cd ~/hcw-project/myfirstworker/
$ python myfirstworker --foreground

3) Start a client project

Open a new terminal and run:

$ cd ~/hcw-project
$ kobo-admin start-client myclient

Configure the client

$ cd myclient/
$ export MYCLIENT_CONFIG_FILE=~/hcw-project/myclient/myclient.conf

Edit myclient.conf

AUTH_METHOD = "password"
...
USERNAME = "foouser"
...
PASSWORD = "foo12345"

Create a client command

$ cd commands/
$ kobo-admin start-client-command add_two_numbers

Modify the run method in a cmd_add_two_numbers.py

    def run(self, *args, **kwargs):
        # optparser output is passed via *args (args) and **kwargs (opts)
        username = kwargs.pop("username", None)
        password = kwargs.pop("password", None)

        if len(args) != 2:
            self.parser.error("Use two numbers as arguments")

        self.set_hub(username, password)

        kwargs = {
            "owner_name": username,
            "label": "Add two numbers.",
            "method": "AddTwoNumbers",
            "args": {
                "a": args[0],
                "b": args[1],
            },
            "weight": 0,
            "arch_name": "noarch",
            "channel_name": "default",
         }
         print self.hub.client.create_task(kwargs)
...
# Remove a line with "raise NotImplementedError"

Run command

$ python myclient --username admin --password admin add-two-numbers 5 7

Other usefull commands

$ python myclient help  # Show a help message with available commands.
$ python myclient list-workers  # List of workers available on the hub.

4) Result

Open http://127.0.0.1:8000/ in a web browser.BR Select "Tasks". Your submited task shoud be there.BR Go to the task detail by click the task id, here you can find a task result (the result is number 12).BR