<commit_before>#!/usr/bin/env python2

"""Simple worker process to process OpenAddress sources on the task queue

Jobs get enqueued to a PQ task queue by some other system.
This program pops jobs and runs them one at a time, then
enqueues a new message on a separate PQ queue when the work is done."""

import time, os, subprocess, psycopg2

from . import db_connect, db_queue, db_queue, MAGIC_OK_MESSAGE, DONE_QUEUE

# File path and URL path for result directory. Should be S3.
_web_output_dir = '/var/www/html/oa-runone'
_web_base_url = 'http://minar.us.to/oa-runone'

def do_work(job_id, job_contents):
    "Do the actual work of running a source file in job_contents"

    # Make a directory to run the whole job
    assert '/' not in job_id
    out_dir = '%s/%s' % (_web_output_dir, job_id)
    os.mkdir(out_dir)

    # Write the user input to a file
    out_fn = '%s/user_input.txt' % out_dir
    with file(out_fn, 'wb') as out_fp:
        out_fp.write(job_contents)

    # Make a directory in which to run openaddr
    oa_dir = '%s/%s' % (out_dir, 'out')
    os.mkdir(oa_dir)

    # Invoke the job to do
    result_code = subprocess.call(('openaddr-process-one',
                                   '-l', '%s/logfile.txt' % out_dir,
                                   out_fn,
                                   oa_dir))

    # Prepare return parameters
    r = { 'result_code': result_code,
          'output_url': '%s/%s' % (_web_base_url, job_id),
          'message': MAGIC_OK_MESSAGE }

    return r

def run(task_data):
    "Run a task posted to the queue. Handles the JSON for task and result objects."

    print "Got job %s" % task_data

    # Unpack the task object
    content = task_data['content']
    url = task_data['url']
    job_id = task_data['id']
    name = task_data['name']

    # Run the task
    result = do_work(job_id, content)

    # Prepare a result object
    r = { 'id' : job_id,
          'url': url,
          'name': name,
          'result' : result }
    return r

def main():
    "Single threaded worker to serve the job queue"

    # Connect to the queue
    conn = db_connect(os.environ['DATABASE_URL'])
    input_queue = db_queue(conn)<commit_msg>Using FQDN for openaddr.ci.worker URL<commit_after>#!/usr/bin/env python2

"""Simple worker process to process OpenAddress sources on the task queue

Jobs get enqueued to a PQ task queue by some other system.
This program pops jobs and runs them one at a time, then
enqueues a new message on a separate PQ queue when the work is done."""

import time, os, subprocess, psycopg2, urlparse, socket

from . import db_connect, db_queue, db_queue, MAGIC_OK_MESSAGE, DONE_QUEUE

# File path and URL path for result directory. Should be S3.
_web_output_dir = '/var/www/html/oa-runone'
_web_base_url = 'http://{host}/oa-runone/'.format(host=socket.getfqdn())

def do_work(job_id, job_contents):
    "Do the actual work of running a source file in job_contents"

    # Make a directory to run the whole job
    assert '/' not in job_id
    out_dir = '%s/%s' % (_web_output_dir, job_id)
    os.mkdir(out_dir)

    # Write the user input to a file
    out_fn = '%s/user_input.txt' % out_dir
    with file(out_fn, 'wb') as out_fp:
        out_fp.write(job_contents)

    # Make a directory in which to run openaddr
    oa_dir = '%s/%s' % (out_dir, 'out')
    os.mkdir(oa_dir)

    # Invoke the job to do
    result_code = subprocess.call(('openaddr-process-one',
                                   '-l', '%s/logfile.txt' % out_dir,
                                   out_fn,
                                   oa_dir))

    # Prepare return parameters
    r = { 'result_code': result_code,
          'output_url': urlparse.urljoin(_web_base_url, job_id),
          'message': MAGIC_OK_MESSAGE }

    return r

def run(task_data):
    "Run a task posted to the queue. Handles the JSON for task and result objects."

    print "Got job %s" % task_data

    # Unpack the task object
    content = task_data['content']
    url = task_data['url']
    job_id = task_data['id']
    name = task_data['name']

    # Run the task
    result = do_work(job_id, content)

    # Prepare a result object
    r = { 'id' : job_id,
          'url': url,
          'name': name,
          'result' : result }
    return r

def main():
    "Single threaded worker to serve the job queue"

    # Connect to the queue
    conn = db_connect(os.environ['DATABASE_URL'])
    input_queue = db_queue(conn)
{'fraction_non_alphanumeric': 0.10166068222621184, 'fraction_numerical': 0.0013464991023339318, 'mean_word_length': 2.5599041533546325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4784763', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1444, 'n_words': 478}