def execute(self, heroku): base_url = get_base_url() self.out.log( "Server is running on {}. Press Ctrl+C to exit.".format(base_url)) self.out.log("Launching the experiment...") try: result = _handle_launch_data("{}/launch".format(base_url), error=self.out.error, attempts=1) except Exception: # Show output from server self.dispatch[r"POST /launch"] = "launch_request_complete" heroku.monitor(listener=self.notify) else: if result["status"] == "success": self.out.log(result["recruitment_msg"]) dashboard_url = "{}/dashboard/".format(get_base_url()) self.display_dashboard_access_details(dashboard_url) if not self.no_browsers: self.open_dashboard(dashboard_url) self.heroku = heroku self.out.log( "Monitoring the Heroku Local server for recruitment or completion..." ) heroku.monitor(listener=self.notify)
def test_local_base_url(self): from dallinger.utils import get_base_url config = get_config() config.ready = True config.set('host', 'localhost') config.set('base_port', 5000) assert (get_base_url() == 'http://localhost:5000')
def from_current_config(cls): config = get_config() if not config.ready: config.load_config() ad_url = '{}/ad'.format(get_base_url()) hit_domain = os.getenv('HOST') return cls(config, hit_domain, ad_url)
def recruit(self, n=1): """Recruit n new participant bots to the queue""" logger.info("Recruiting {} Bot participants".format(n)) factory = self._get_bot_factory() urls = [] q = _get_queue(name="low") for _ in range(n): base_url = get_base_url() worker = generate_random_id() hit = generate_random_id() assignment = generate_random_id() ad_parameters = ( "recruiter={}&assignmentId={}&hitId={}&workerId={}&mode=sandbox" ) ad_parameters = ad_parameters.format(self.nickname, assignment, hit, worker) url = "{}/ad?{}".format(base_url, ad_parameters) urls.append(url) bot = factory(url, assignment_id=assignment, worker_id=worker, hit_id=hit) job = q.enqueue(bot.run_experiment, job_timeout=self._timeout) logger.warning("Created job {} for url {}.".format(job.id, url)) return urls
def test_remote_base_url_always_ssl(self): from dallinger.utils import get_base_url config = get_config() config.ready = True config.set("host", "http://dlgr-bogus.herokuapp.com") config.set("num_dynos_web", 1) assert get_base_url() == "https://dlgr-bogus.herokuapp.com"
def test_local_base_url(self): from dallinger.utils import get_base_url config = get_config() config.ready = True config.set("host", "localhost") config.set("base_port", 5000) config.set("num_dynos_web", 1) assert get_base_url() == "http://localhost:5000"
def recruit_participants(self, n=1): """Talk about recruiting participants.""" for i in range(n): ad_url = "{}/ad?assignmentId=debug{}&hitId={}&workerId={}&mode=debug".format( get_base_url(), generate_random_id(), generate_random_id(), generate_random_id(), ) logger.info('New participant requested: {}'.format(ad_url))
def __init__(self): super(MTurkRecruiter, self).__init__() self.config = get_config() self.ad_url = '{}/ad'.format(get_base_url()) self.hit_domain = os.getenv('HOST') self.mturkservice = MTurkService( self.config.get('aws_access_key_id'), self.config.get('aws_secret_access_key'), self.config.get('mode') != u"live" ) self._validate_conifg()
def execute(self, heroku): base_url = get_base_url() self.out.log( "Server is running on {}. Press Ctrl+C to exit.".format(base_url)) self.out.log("Launching the experiment...") time.sleep(4) result = _handle_launch_data('{}/launch'.format(base_url), error=self.out.error) if result['status'] == 'success': self.out.log(result['recruitment_msg']) heroku.monitor(listener=self.notify)
def __call__(self): gevent.sleep(0.200) try: logger.info("Replay ready: {}{}".format( get_base_url(), self.experiment.replay_path)) except RuntimeError: # config not loaded, we may be in unit tests pass while not self.experiment.replay_started(): gevent.sleep(0.01) self.experiment.log("Looping through replayable data", key="replay") timestamp = self.timestamp events = self.experiment.events_for_replay() if not events.count(): self.experiment.replay_finish() return first_timestamp = timestamp(events[0].creation_time) self.experiment.log( "Found {} messages to replay starting from {}".format( events.count(), events[0].creation_time), key="replay", ) start = time.time() for event in events: event_offset = timestamp(event.creation_time) - first_timestamp cur_offset = time.time() - start if event_offset >= cur_offset: if (event_offset - cur_offset) > 1: self.experiment.log( "Waiting {} seconds to replay {} {}".format( event_offset - cur_offset, event.type, event.id), key="replay", ) gevent.sleep(event_offset - cur_offset) self.experiment.replay_event(event) self.experiment.log( "Replayed {} events in {} seconds (original duration {} seconds)". format( events.count(), time.time() - start, timestamp(event.creation_time) - first_timestamp, ), key="replay", ) self.experiment.replay_finish() return
def recruit(self, n=1): """Generate experiemnt URLs and print them to the console.""" urls = [] template = "{}/ad?recruiter={}&assignmentId={}&hitId={}&workerId={}&mode={}" for i in range(n): ad_url = template.format(get_base_url(), self.nickname, generate_random_id(), generate_random_id(), generate_random_id(), self._get_mode()) logger.info('{} {}'.format(NEW_RECRUIT_LOG_PREFIX, ad_url)) urls.append(ad_url) return urls
def __init__(self): super(MTurkRecruiter, self).__init__() self.config = get_config() self.ad_url = "{}/ad?recruiter={}".format(get_base_url(), self.nickname) self.hit_domain = os.getenv("HOST") self.mturkservice = MTurkService( self.config.get("aws_access_key_id"), self.config.get("aws_secret_access_key"), self.config.get("aws_region"), self.config.get("mode") != "live", ) self.messenger = get_messenger(self.config) self._validate_config()
def __init__(self): super(MTurkRecruiter, self).__init__() self.config = get_config() base_url = get_base_url() self.ad_url = "{}/ad?recruiter={}".format(base_url, self.nickname) self.notification_url = "{}/mturk-sns-listener".format(base_url) self.hit_domain = os.getenv("HOST") self.mturkservice = MTurkService( aws_access_key_id=self.config.get("aws_access_key_id"), aws_secret_access_key=self.config.get("aws_secret_access_key"), region_name=self.config.get("aws_region"), sandbox=self.config.get("mode") != "live", ) self.messenger = get_messenger(self.config) self._validate_config()
def execute(self, heroku): base_url = get_base_url() self.out.log( "Server is running on {}. Press Ctrl+C to exit.".format(base_url)) self.out.log("Launching the experiment...") try: result = _handle_launch_data('{}/launch'.format(base_url), error=self.out.error) except Exception: # Show output from server self.dispatch[r'POST /launch'] = 'launch_request_complete' heroku.monitor(listener=self.notify) else: if result['status'] == 'success': self.out.log(result['recruitment_msg']) self.heroku = heroku heroku.monitor(listener=self.notify)
def execute(self, heroku): base_url = get_base_url() self.out.log( "Server is running on {}. Press Ctrl+C to exit.".format(base_url)) self.out.log("Launching the experiment...") time.sleep(4) try: result = _handle_launch_data("{}/launch".format(base_url), error=self.out.error) except Exception: # Show output from server self.dispatch[r"POST /launch"] = "launch_request_complete" heroku.monitor(listener=self.notify) else: if result["status"] == "success": self.out.log(result["recruitment_msg"]) self.heroku = heroku heroku.monitor(listener=self.notify)
def recruit(self, n=1): """Recruit n new participant bots to the queue""" factory = self._get_bot_factory() urls = [] q = _get_queue() for _ in range(n): base_url = get_base_url() worker = generate_random_id() hit = generate_random_id() assignment = generate_random_id() ad_parameters = 'assignmentId={}&hitId={}&workerId={}&mode=sandbox' ad_parameters = ad_parameters.format(assignment, hit, worker) url = '{}/ad?{}'.format(base_url, ad_parameters) urls.append(url) bot = factory(url, assignment_id=assignment, worker_id=worker) job = q.enqueue(bot.run_experiment, timeout=60 * 20) logger.info("Created job {} for url {}.".format(job.id, url)) return urls
def recruitment_closed(self, match): """Recruitment is closed. Check the output of the summary route until the experiment is complete, then we can stop monitoring Heroku subprocess output. """ base_url = get_base_url() status_url = base_url + '/summary' self.out.log( "Recruitment is complete. Waiting for experiment completion...") time.sleep(10) try: resp = requests.get(status_url) exp_data = resp.json() except (ValueError, requests.exceptions.RequestException): self.out.error('Error fetching experiment status.') self.out.log('Experiment summary: {}'.format(exp_data)) if exp_data.get('completed', False): self.out.log('Experiment completed, all nodes filled.') return HerokuLocalWrapper.MONITOR_STOP
def check_status(self): """Check the output of the summary route until the experiment is complete, then we can stop monitoring Heroku subprocess output. """ self.out.log("Recruitment is complete. Waiting for experiment completion...") base_url = get_base_url() status_url = base_url + "/summary" while not self.complete: time.sleep(10) try: resp = requests.get(status_url) exp_data = resp.json() except (ValueError, requests.exceptions.RequestException): self.out.error("Error fetching experiment status.") else: self.out.log("Experiment summary: {}".format(exp_data)) if exp_data.get("completed", False): self.out.log("Experiment completed, all nodes filled.") self.complete = True self.heroku.stop()
def execute(self, heroku): """Start the server, load the zip file into the database, then loop until terminated with <control>-c. """ db.init_db(drop_all=True) self.out.log( "Ingesting dataset from {}...".format(os.path.basename(self.zip_path)) ) data.ingest_zip(self.zip_path) base_url = get_base_url() self.out.log("Server is running on {}. Press Ctrl+C to exit.".format(base_url)) if self.exp_config.get("replay"): self.out.log("Launching the experiment...") time.sleep(4) _handle_launch_data("{}/launch".format(base_url), error=self.out.error) heroku.monitor(listener=self.notify) # Just run until interrupted: while self.keep_running(): time.sleep(1)
def test_remote_base_url_always_ssl(self): from dallinger.utils import get_base_url config = get_config() config.ready = True config.set('host', 'http://dlgr-bogus.herokuapp.com') assert (get_base_url() == 'https://dlgr-bogus.herokuapp.com')