Skip to content

Commit

Permalink
Refactor scan example: shorter and generic
Browse files Browse the repository at this point in the history
  • Loading branch information
lnauta committed Jan 10, 2025
1 parent 4fbce71 commit d5a7092
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 49 deletions.
3 changes: 3 additions & 0 deletions examples/N-core-job.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

python local_example.py --design_doc $DOC --view $VIEW
35 changes: 0 additions & 35 deletions examples/core-scanner.py

This file was deleted.

28 changes: 28 additions & 0 deletions examples/core_scanner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-

import logging
import sys

import picasconfig
from picas.picaslogger import picaslogger
from picas.clients import CouchDB
from picas.executers import execute
from picas.util import arg_parser

picaslogger.propagate = False

# expand the parser for the example
parser = arg_parser()
parser.add_argument("--cores", default=1, type=str, help="Number of cores for the job")
parser.set_defaults(design_doc="SingleCore")
args = arg_parser().parse_args()

client = CouchDB(url=picasconfig.PICAS_HOST_URL, db=picasconfig.PICAS_DATABASE, username=picasconfig.PICAS_USERNAME, password=picasconfig.PICAS_PASSWORD)

work_avail = client.is_view_nonempty(args.view, design_doc=args.design_doc)
if work_avail:
picaslogger.info(f"Starting a picas clients checking view {args.view} in design document {args.design_doc}")
command = ["sbatch", "--cores {args.cores}", f"--export=VIEW={args.view},DOC={args.design_doc}", "N-core-job.sh"]
execute(command)
else:
picaslogger.info(f"Not starting a picas client, there is nothing to do in view {args.view} and for the designdocs {args.design_doc}.")
16 changes: 2 additions & 14 deletions examples/local_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from picas.iterators import TaskViewIterator
from picas.iterators import EndlessViewIterator
from picas.modifiers import BasicTokenModifier
from picas.util import Timer
from picas.util import Timer, arg_parser

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,21 +79,9 @@ def process_task(self, token):
pass


def arg_parser():
"""
Arguments parser for optional values of the example
returns: argparse object
"""
parser = argparse.ArgumentParser(description="Arguments used in the different classes in the example.")
parser.add_argument("-d", "--design_doc", default="Monitor", type=str, help="Select the designdoc used by the actor class")
parser.add_argument("-V", "--view", default="todo", type=str, help="Select the view used by the actor class")
parser.add_argument("-v", "--verbose", action="store_true", help="Set verbose") # add v for optional verbosity in the future
return parser.parse_args()


def main():
# parse user arguments
args = arg_parser()
args = arg_parser().parse_args()
# setup connection to db
client = CouchDB(url=picasconfig.PICAS_HOST_URL, db=picasconfig.PICAS_DATABASE, username=picasconfig.PICAS_USERNAME, password=picasconfig.PICAS_PASSWORD)
print("Connected to the database %s sucessfully. Now starting work..." %(picasconfig.PICAS_DATABASE))
Expand Down
12 changes: 12 additions & 0 deletions picas/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ def seconds():
return int(time.time())


def arg_parser():
"""
Arguments parser for optional values of the example
returns: argparse object
"""
parser = argparse.ArgumentParser(description="Arguments used in the different classes in the example.")
parser.add_argument("--design_doc", default="Monitor", type=str, help="Select the designdoc used by the actor class")
parser.add_argument("--view", default="todo", type=str, help="Select the view used by the actor class")
parser.add_argument("-v", "--verbose", action="store_true", help="Set verbose")
return parser


class Timer:
"""Timer class"""

Expand Down

0 comments on commit d5a7092

Please sign in to comment.