Skip to content

Commit

Permalink
Merge pull request #2 from StealthyLoner/dev
Browse files Browse the repository at this point in the history
Clean codebase
  • Loading branch information
jjonek committed Aug 25, 2015
2 parents ecd3e51 + a51fc45 commit 4e4d0de
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 51 deletions.
38 changes: 20 additions & 18 deletions slamon_agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import logging
import uuid
"""Agent module."""

import time
import uuid
import logging
import argparse
import importlib

from slamon_agent import timeutil
from slamon_agent.executor import Executor
from slamon_agent.communication import TemporaryError
from slamon_agent.communication import Communicator
from slamon_agent.handlers import TaskHandler
from slamon_agent.communication import Communicator, TemporaryError


class Agent(object):
"""
Agent class presents an instance of agent application.
"""
"""Agent class presents an instance of agent application."""

def __init__(self, afm_url, default_wait=60, max_tasks=2, name='Python Agent 1.0', agent_uuid=None):
"""
Initialize Agent object.
:param afm_url: The URL for the AFM service.
:param default_wait: Amount of time to wait after each run iteration.
:param max_tasks: Maximum tasks this agent can execute during one run iteration.
:param name: The name of this Agent instance.
:param agent_uuid: UUID for this Agent instance.
"""
self.afm = Communicator(afm_url)
self.max_tasks = max_tasks
self.default_wait = default_wait
Expand All @@ -27,16 +34,15 @@ def __init__(self, afm_url, default_wait=60, max_tasks=2, name='Python Agent 1.0

def exit(self):
"""
Signal agent to exit. After issuing exit, agent will not make further task requests,
Signal agent to exit.
After issuing exit, agent will not make further task requests,
but will wait until all currently processed tasks finish.
"""
self._run = False

def run(self):
"""
The "main function" of the agent, looping the claim & execute tasks flow.
"""

"""The "main function" of the agent, looping the claim & execute tasks flow."""
with Executor(self.max_tasks) as executor:
while self._run:

Expand Down Expand Up @@ -66,19 +72,15 @@ def run(self):


def _import_module(module_name, package=None):
"""
Recursively load modules to search for task handlers.
"""
"""Recursively load modules to search for task handlers."""
m = importlib.import_module(module_name, package=package)
if hasattr(m, '__all__'):
for sub_module_name in m.__all__:
_import_module('.' + sub_module_name, module_name)


def main():
"""
Entry point for the agent script.
"""
"""Entry point for the agent script."""
logging.basicConfig(level=logging.DEBUG)

parser = argparse.ArgumentParser()
Expand Down
20 changes: 8 additions & 12 deletions slamon_agent/executor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from concurrent.futures import ThreadPoolExecutor
import logging
import threading

from concurrent.futures import ThreadPoolExecutor
from slamon_agent.handlers import TaskHandler


Expand All @@ -25,7 +25,6 @@ def result_callback(task_id, task_data=None, task_error=none):
with Executor(max_executors=2) as executor:
for task in task_list:
executor.submit_task(task, result_callback)
"""

def __init__(self, max_executors=2):
Expand All @@ -35,17 +34,13 @@ def __init__(self, max_executors=2):
self._logger = logging.getLogger('Executor')

def __enter__(self):
"""
Context manager entry function to start thread pool.
"""
"""Context manager entry function to start thread pool."""
self._logger.debug('Starting executor with %d threads', self._max_executors)
self._thread_pool = ThreadPoolExecutor(self._max_executors)
return self

def __exit__(self, exc_type, exc_val, exc_tb):
"""
Context manager exit function to shutdown thread pool.
"""
"""Context manager exit function to shutdown thread pool."""
self._logger.debug('Stopping executor')
self._thread_pool.shutdown()
self._thread_pool = None
Expand All @@ -71,8 +66,9 @@ def _run_task(self, task, callback):

def submit_task(self, task, callback):
"""
Queue task for asynchronous execution. The callback will receive
task id as first positional parameter and either task_data or task_error
Queue task for asynchronous execution.
The callback will receive task id as first positional parameter and either task_data or task_error
named parameter describing the output.
:param task: dictionary describing the task
Expand All @@ -84,9 +80,9 @@ def submit_task(self, task, callback):

def available_executors(self):
"""
Get number of available executors. (max executors - active executors)
Get number of available executors.
:return: number of available executors
:return: number of available executors (max executors - active executors)
"""
with self._lock:
return max(self._max_executors - self._active_executors, 0)
5 changes: 2 additions & 3 deletions slamon_agent/handlers/task_handler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class TaskHandler(object):
"""
TaskHandler is a decorator to register a callable as a task handler for the agent.
"""
"""TaskHandler is a decorator to register a callable as a task handler for the agent."""

_handlers = {}

Expand All @@ -17,6 +15,7 @@ def __call__(self, handler):
def register(cls, callable, name, version):
"""
Utility to register any callable as task handler.
:param callable: Callable taking one positional argument (input data) and returning dictionary of output params.
:param name: Name of the task type
:param version: Version of the supported task type
Expand Down
6 changes: 2 additions & 4 deletions slamon_agent/handlers/url_http_status_task_handler.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from slamon_agent.handlers import TaskHandler
import requests
from slamon_agent.handlers import TaskHandler


@TaskHandler('url_http_status', 1)
def url_http_status_task_handler(input_params):
"""
Task for checking web page availability and returning status code if page is available
"""
"""Task for checking web page availability and returning status code if page is available."""
url = input_params['url']

response = requests.get(url)
Expand Down
5 changes: 3 additions & 2 deletions slamon_agent/handlers/wait_task_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
@TaskHandler("wait", 1)
def wait_task_handler(input_params):
"""
Agent for prototyping. Will wait for approximately the specified
timeout, with slight random factor.
Agent for prototyping.
Will wait for approximately the specified timeout, with slight random factor.
"""
import time
import random
Expand Down
9 changes: 4 additions & 5 deletions slamon_agent/tests/agent_tests.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import unittest
import logging
import json
import uuid
import logging
import unittest
from unittest.mock import Mock, patch

import responses
import dateutil.parser

from slamon_agent.communication import Communicator, FatalError
from slamon_agent import timeutil
from slamon_agent.agent import Agent
from slamon_agent.handlers import TaskHandler
from slamon_agent import timeutil
from slamon_agent.communication import FatalError, Communicator

logging.basicConfig(
format='%(thread)d:%(levelname)s:%(message)s',
Expand Down
7 changes: 3 additions & 4 deletions slamon_agent/tests/communication_tests.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import unittest
import logging
import json
import logging
import unittest

import responses

from slamon_agent.communication import Communicator, FatalError, TemporaryError
from slamon_agent.communication import FatalError, Communicator, TemporaryError

logging.basicConfig(
format='%(thread)d:%(levelname)s:%(message)s',
Expand Down
2 changes: 1 addition & 1 deletion slamon_agent/tests/executor_tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
import logging
import unittest
from unittest.mock import ANY, Mock

from slamon_agent.executor import Executor
Expand Down
1 change: 0 additions & 1 deletion slamon_agent/tests/task_handler_tests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unittest

import responses

from slamon_agent.handlers import TaskHandler
from slamon_agent.handlers.url_http_status_task_handler import url_http_status_task_handler

Expand Down
2 changes: 1 addition & 1 deletion slamon_agent/timeutil.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from datetime import datetime

from dateutil import parser, tz
from dateutil import tz, parser


def now():
Expand Down

0 comments on commit 4e4d0de

Please sign in to comment.