o
    [h                     @   s   d dl Z d dlmZ d dlZd dlmZ d dlmZmZ d dlm	Z	 d dl
mZ d dlmZmZ d dlZd dlmZ eZeeZd	efd
dZdd ZG dd dZdd Zdd Zdd ZdS )    N)	getLogger)sleep)requestparse)URLError)urljoin)dbsession_scope)TaskQueueMessagedatac                 C   s*   t | }tj| |d}t|}dS )z
    make the request over the network rather than in-process,
    to avoid race conditions. everything must be handled by the main
    server instance.
    r   N)r   	urlencodeencoder   Requesturlopen)urlr   reqresp r   r/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/otree/tasks.pypost   s   r   c              
   C   sD   zt |  W d S  ty! } ztd|  dt| d d }~ww )NzError occurred when opening z: )r   r   r   	Exceptionrepr)r   excr   r   r   get   s   r   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
Workerc                 C   s,   d| | _ ttjt d k   d S )Nzhttp://127.0.0.1:<   )base_urlr
   objects_filter
epoch_timetimedelete)selfportr   r   r   __init__&   s
   
zWorker.__init__c                 C   s   t d 	 t F ttjt kdD ]0}zt| |jdi |	  W n t
y@ } ztt| W Y d }~nd }~ww t| qW d    n1 sQw   Y  td q)Nz2timeoutworker is listening for messages through DBTr      r   )print_functionr	   r
   r   r   r    Zorder_bygetattrmethodkwargsr   logger	exceptionr   r   r!   r   )r"   taskr   r   r   r   listen.   s&   zWorker.listenc                 C   s^   ddl m} |j||d }|r-| }td|  tt| j	|t
jjdid d S d S )Nr   Participant)code_index_in_pages Auto-submitting timed out page: Tr   )otree.models.participantr/   r   first_url_i_should_be_onr*   infor   r   r   otree	constantsZtimeout_happened)r"   Zparticipant_code
page_indexr/   pppathr   r   r   submit_expired_urlA   s   


zWorker.submit_expired_urlc                 C   s`   ddl m} ||j||j|d k}|D ]}| }td|  t	t
| j| qdS )a7  This is necessary when a wait page is followed by a timeout page.
        We can't guarantee the user's browser will properly continue to poll
        the wait page and get redirected, so after a grace period we load the page
        automatically, to kick off the expiration timer of the timeout page.
        r   r.      r2   N)r3   r/   r   idin_r1   r5   r*   r6   r   r   r   )r"   Zparticipant_pksr9   r/   Zunvisited_participantsr:   r;   r   r   r   ensure_pages_visited\   s   
zWorker.ensure_pages_visitedN)__name__
__module____qualname__r$   r-   r<   r@   r   r   r   r   r   %   s
    r   c                 C   s&   t j| |tt  t|d d S )N)r(   r   Zkwargs_json)r
   Zobjects_createroundr    jsondumpsr(   delayr)   r   r   r   _db_enqueue{   s
   
rI   c                 K      t d| |d d S )Nr@   rG   rI   rH   r)   r   r   r   r@         r@   c                 K   rJ   )Nr<   rG   rK   rL   r   r   r   r<      rM   r<   )rE   loggingr   r    r   urllibr   r   urllib.errorr   urllib.parser   Zotree.databaser   r	   Zotree.constantsr7   Zotree.models_concreter
   printr&   rA   r*   dictr   r   r   rI   r@   r<   r   r   r   r   <module>   s$    V