def manual_branch(request, build_key, branch_id, label=""): """ Endpoint for creating a manual event. """ if request.method != 'POST': return HttpResponseNotAllowed(['POST']) branch = get_object_or_404(models.Branch, pk=branch_id) user = get_object_or_404(models.GitUser, build_key=build_key) reply = 'OK' try: logger.info('Running manual with user %s on branch %s' % (user, branch)) latest = user.api().last_sha(branch.repository.user.name, branch.repository.name, branch.name) force = bool(int(request.POST.get('force', 0))) update_branch_status = bool(int(request.POST.get('update_branch_status', 1))) if latest: mev = ManualEvent.ManualEvent(user, branch, latest, label) mev.force = force mev.save(update_branch_status) reply = 'Success. Scheduled recipes on branch %s for user %s' % (branch, user) messages.info(request, reply) logger.info(reply) else: reply = "Failed to get latest SHA for %s" % branch except Exception: reply = 'Error running manual for user %s on branch %s\nError: %s'\ % (user, branch, traceback.format_exc()) messages.error(request, reply) logger.info(reply) next_url = request.POST.get('next', None) if next_url: return redirect(next_url) return HttpResponse(reply)
def create_data(self, branch=None, user=None, latest="1", label=""): if branch == None: branch = self.branch if user == None: user = self.build_user manual = ManualEvent.ManualEvent(user, branch, latest, activate_label=label) return manual
def manual_cron(request, recipe_id): allowed = Permissions.is_allowed_to_see_clients(request.session) if not allowed: return HttpResponseForbidden('Not allowed to start manual cron runs') r = get_object_or_404(models.Recipe, pk=recipe_id) user = r.build_user branch = r.branch latest = user.api().last_sha(branch.repository.user.name, branch.repository.name, branch.name) #likely need to add exception checks for this! if latest: r.last_scheduled = datetime.now(tz=pytz.UTC); r.save(); mev = ManualEvent.ManualEvent(user, branch, latest, "", recipe=r); mev.force = True; mev.save(update_branch_status=True); return redirect('ci:cronjobs')
def schedulePinger(): logger.debug("SCHEDULER: Starting") # Has to be done here, because these parts of the django app aren't initialized until ready() runs from ci import models, ManualEvent # Used to make the program sleeps until every *nth* minute, not every n minutes # (12:00, 12:05, 12:10, rather than 12:01, 12:06, 12:11). Sleep until it is time to run interval = croniter("*/5 * * * *", datetime.now()) # Formats time to something more suitable for logs (no time zone or ms format_time = lambda t: t.strftime("%Y-%m-%d %H:%M:%S") local_tz = pytz.timezone("US/Mountain") while True: logger.debug("SCHEDULER: Checking for scheduled recipes") # Get all recipes with schedules, to check if recipes have been changed/added since the last load dbRecipes = models.Recipe.objects.filter( active=True, current=True, scheduler__isnull=False, branch__isnull=False).exclude(scheduler="") now = datetime.now(tz=pytz.UTC) for r in dbRecipes: logger.debug("SCHEDULER: Checking recipe {}".format(r.name)) if r.last_scheduled == datetime.fromtimestamp( 0, tz=pytz.UTC) and not r.schedule_initial_run: r.last_scheduled = now - timedelta(seconds=1) r.save() c = croniter(r.scheduler, start_time=r.last_scheduled.astimezone(local_tz)) next_job_run_time = c.get_next(datetime) user = r.build_user branch = r.branch if next_job_run_time <= now: latest = user.api().last_sha(branch.repository.user.name, branch.repository.name, branch.name) if latest: #likely need to add exception checks for this! r.last_scheduled = now r.save() logger.info( "SCHEDULER: Running scheduled job {} on {}, soonest run time is {}, next run time is {}" .format(r.name, branch.repository.name, format_time(next_job_run_time), format_time(c.get_next(datetime)))) mev = ManualEvent.ManualEvent(user, branch, latest, "", recipe=r) mev.force = True #forces the event through even if it exists. this is because it won't rerun the same job. mev.save(update_branch_status=True ) #magically add the job through a blackbox else: logger.debug( "SCHEDULER: Not running recipe {} on {}, next run time is {}" .format(r.name, branch.repository.name, format_time(next_job_run_time))) # Wait until it is time to run next_run_time = interval.get_next(datetime) dt = (next_run_time - datetime.now()).total_seconds() logger.debug("SCHEDULER: Sleeping for {} sec until {}".format( dt, next_run_time)) time.sleep(dt)