Пример #1
0
def update_workflow_state(workflow):
  today = date.today()
  calculator = workflow_cycle_calculator.get_cycle_calculator(workflow)

  # Start the first cycle if min_start_date < today < max_end_date
  if workflow.status == "Active" and workflow.recurrences and calculator.tasks:
    start_date, end_date = calculator.workflow_date_range()
    # Only create the cycle if we're mid-cycle
    if (start_date <= today <= end_date) \
            and not workflow.cycles:
      cycle = models.Cycle()
      cycle.workflow = workflow
      cycle.calculator = calculator
      # Other cycle attributes will be set in build_cycle.
      build_cycle(
          cycle,
          None,
          base_date=workflow.non_adjusted_next_cycle_start_date)
      notification.handle_cycle_created(None, obj=cycle)

    adjust_next_cycle_start_date(calculator, workflow)

    db.session.add(workflow)
    db.session.flush()
    return

  if not calculator.tasks:
    workflow.next_cycle_start_date = None
    workflow.non_adjusted_next_cycle_start_date = None
    return

  for cycle in workflow.cycles:
    if cycle.is_current:
      return

  if workflow.status == 'Draft':
    return

  if workflow.status == "Inactive":
    if workflow.cycles:
      workflow.status = "Active"
      db.session.add(workflow)
      db.session.flush()
      return

  # Active workflow with no recurrences and no active cycles, workflow is
  # now Inactive
  workflow.status = 'Inactive'
  db.session.add(workflow)
  db.session.flush()
Пример #2
0
def update_workflow_state(workflow):
  today = date.today()
  calculator = workflow_cycle_calculator.get_cycle_calculator(workflow)

  # Start the first cycle if min_start_date < today < max_end_date
  if workflow.status == "Active" and workflow.recurrences and calculator.tasks:
    start_date, end_date = calculator.workflow_date_range()
    # Only create the cycle if we're mid-cycle
    if (start_date <= today <= end_date) \
            and not workflow.cycles:
      cycle = models.Cycle()
      cycle.workflow = workflow
      cycle.calculator = calculator
      # Other cycle attributes will be set in build_cycle.
      build_cycle(
          cycle,
          None,
          base_date=workflow.non_adjusted_next_cycle_start_date)
      notification.handle_cycle_created(None, obj=cycle)

    adjust_next_cycle_start_date(calculator, workflow)

    db.session.add(workflow)
    db.session.flush()
    return

  if not calculator.tasks:
    workflow.next_cycle_start_date = None
    workflow.non_adjusted_next_cycle_start_date = None
    return

  for cycle in workflow.cycles:
    if cycle.is_current:
      return

  if workflow.status == 'Draft':
    return

  if workflow.status == "Inactive":
    if workflow.cycles:
      workflow.status = "Active"
      db.session.add(workflow)
      db.session.flush()
      return

  # Active workflow with no recurrences and no active cycles, workflow is
  # now Inactive
  workflow.status = 'Inactive'
  db.session.add(workflow)
  db.session.flush()
Пример #3
0
def start_recurring_cycles():
    today = date.today()
    workflows = models.Workflow.query.filter(
        models.Workflow.next_cycle_start_date <= today,
        models.Workflow.recurrences == True  # noqa
    )
    for workflow in workflows:
        # Follow same steps as in model_posted.connect_via(models.Cycle)
        while workflow.next_cycle_start_date <= date.today():
            cycle = build_cycle(workflow)
            if not cycle:
                break
            db.session.add(cycle)
            notification.handle_cycle_created(cycle, False)
            notification.handle_workflow_modify(None, workflow)
    log_event(db.session)
    db.session.commit()
Пример #4
0
def start_recurring_cycles():
  today = date.today()
  workflows = models.Workflow.query.filter(
      models.Workflow.next_cycle_start_date <= today,
      models.Workflow.recurrences == True  # noqa
  )
  for workflow in workflows:
    # Follow same steps as in model_posted.connect_via(models.Cycle)
    while workflow.next_cycle_start_date <= date.today():
      cycle = build_cycle(workflow)
      if not cycle:
        break
      db.session.add(cycle)
      notification.handle_cycle_created(cycle, False)
      notification.handle_workflow_modify(None, workflow)
  log_event(db.session)
  db.session.commit()
Пример #5
0
def start_recurring_cycles():
    # Get all workflows that should start a new cycle today
    # The next_cycle_start_date is precomputed and stored when a cycle is created
    today = date.today()
    workflows = db.session.query(models.Workflow)\
        .filter(
        models.Workflow.next_cycle_start_date == today,
        models.Workflow.recurrences == True  # noqa
    ).all()

    # For each workflow, start and save a new cycle.
    for workflow in workflows:
        cycle = models.Cycle()
        cycle.workflow = workflow
        cycle.calculator = workflow_cycle_calculator.get_cycle_calculator(
            workflow)
        cycle.context = workflow.context
        # We can do this because we selected only workflows with
        # next_cycle_start_date = today
        cycle.start_date = date.today()

        # Flag the cycle to be saved
        db.session.add(cycle)

        if workflow.non_adjusted_next_cycle_start_date:
            base_date = workflow.non_adjusted_next_cycle_start_date
        else:
            base_date = date.today()

        # Create the cycle (including all child objects)
        build_cycle(cycle, base_date=base_date)

        # Update the workflow next_cycle_start_date to push it ahead based on the
        # frequency.
        adjust_next_cycle_start_date(cycle.calculator,
                                     workflow,
                                     move_forward=True)

        db.session.add(workflow)

        notification.handle_workflow_modify(None, workflow)
        notification.handle_cycle_created(None, obj=cycle)

    db.session.commit()
    db.session.flush()
Пример #6
0
def start_recurring_cycles():
  # Get all workflows that should start a new cycle today
  # The next_cycle_start_date is precomputed and stored when a cycle is created
  today = date.today()
  workflows = db.session.query(models.Workflow)\
      .filter(
      models.Workflow.next_cycle_start_date == today,
      models.Workflow.recurrences == True  # noqa
  ).all()

  # For each workflow, start and save a new cycle.
  for workflow in workflows:
    cycle = models.Cycle()
    cycle.workflow = workflow
    cycle.calculator = workflow_cycle_calculator.get_cycle_calculator(workflow)
    cycle.context = workflow.context
    # We can do this because we selected only workflows with
    # next_cycle_start_date = today
    cycle.start_date = date.today()

    # Flag the cycle to be saved
    db.session.add(cycle)

    if workflow.non_adjusted_next_cycle_start_date:
      base_date = workflow.non_adjusted_next_cycle_start_date
    else:
      base_date = date.today()

    # Create the cycle (including all child objects)
    build_cycle(cycle, base_date=base_date)

    # Update the workflow next_cycle_start_date to push it ahead based on the
    # frequency.
    adjust_next_cycle_start_date(cycle.calculator, workflow, move_forward=True)

    db.session.add(workflow)

    notification.handle_workflow_modify(None, workflow)
    notification.handle_cycle_created(None, obj=cycle)

  log_event(db.session)
  db.session.commit()
Пример #7
0
def start_recurring_cycles():
    """Start recurring cycles by cron job."""
    with benchmark("contributed cron job start_recurring_cycles"):
        today = date.today()
        workflows = models.Workflow.query.filter(
            models.Workflow.next_cycle_start_date <= today,
            models.Workflow.recurrences == True  # noqa
        )
        for workflow in workflows:
            # Follow same steps as in model_posted.connect_via(models.Cycle)
            while workflow.next_cycle_start_date <= date.today():
                cycle = build_cycle(workflow)
                if not cycle:
                    break
                db.session.add(cycle)
                notification.handle_cycle_created(cycle, False)
                notification.handle_workflow_modify(None, workflow)
            # db.session.commit was moved into cycle intentionally.
            # 'Cycles' for each 'Workflow' should be committed separately
            # to free memory on each iteration. Single commit exeeded
            # maximum memory limit on AppEngine instance.
            log_event(db.session)
            db.session.commit()
Пример #8
0
def start_recurring_cycles():
  """Start recurring cycles by cron job."""
  with benchmark("contributed cron job start_recurring_cycles"):
    today = date.today()
    workflows = models.Workflow.query.filter(
        models.Workflow.next_cycle_start_date <= today,
        models.Workflow.recurrences == True  # noqa
    )
    event = None
    for workflow in workflows:
      # Follow same steps as in model_posted.connect_via(models.Cycle)
      while workflow.next_cycle_start_date <= date.today():
        cycle = build_cycle(workflow)
        if not cycle:
          break
        db.session.add(cycle)
        notification.handle_cycle_created(cycle, False)
        notification.handle_workflow_modify(None, workflow)
      # db.session.commit was moved into cycle intentionally.
      # 'Cycles' for each 'Workflow' should be committed separately
      # to free memory on each iteration. Single commit exeeded
      # maximum memory limit on AppEngine instance.
      event = log_event(db.session, event=event)
      db.session.commit()