Example #1
0
 def test_instantiates_new_hourly_disabled(self, mocker):
     task = Task.to_be_created(command="myscript.py", hour=None, minute=10, disabled=True)
     assert task.command == "myscript.py"
     assert task.hour is None
     assert task.minute == 10
     assert task.interval == "hourly"
     assert task.enabled is False
     assert task.__repr__() == "Hourly task 'myscript.py' ready to be created"
Example #2
0
def main(*, command, hour, minute, disabled):
    get_logger(set_info=True)
    hour = int(hour) if hour is not None else None
    task = Task.to_be_created(command=command,
                              hour=hour,
                              minute=int(minute),
                              disabled=disabled)
    task.create_schedule()
Example #3
0
 def test_instantiates_new_daily_enabled(self):
     task = Task.to_be_created(command="myscript.py", hour=8, minute=10, disabled=False)
     assert task.command == "myscript.py"
     assert task.hour == 8
     assert task.minute == 10
     assert task.interval == "daily"
     assert task.enabled is True
     assert task.__repr__() == "Daily task 'myscript.py' ready to be created"
Example #4
0
def set(
    command: str = typer.Option(
        ..., "-c", "--command", help="Task's command to be scheduled"
    ),
    hour: int = typer.Option(
        None,
        "-h",
        "--hour",
        min=0,
        max=23,
        help="Sets the task to be performed daily at HOUR",
    ),
    minute: int = typer.Option(
        ...,
        "-m",
        "--minute",
        min=0,
        max=59,
        help="Minute on which the task will be executed",
    ),
    disabled: bool = typer.Option(
        False, "-d", "--disabled", help="Creates disabled task (otherwise enabled)"
    ),
):
    """Create a scheduled task.

    Two categories of tasks are available: daily and hourly.
    Both kinds require a command to run and scheduled time. In order to create a
    daily task provide hour and minute; to create hourly task provide only minute.
    If task is intended to be enabled later add --disabled flag.

    Example:
      Create a daily task to be run at 13:15:

        pa schedule set --command "echo foo" --hour 13 --minute 15

      Create an inactive hourly task to be run 27 minutes past every hour:

        pa schedule set --command "echo bar" --minute 27 --disabled

    Note:
      Once task is created its behavior may be altered later on with
      `pa schedule update` or deleted with `pa schedule delete`
      commands."""

    logger = get_logger(set_info=True)

    task = Task.to_be_created(
        command=command, hour=hour, minute=minute, disabled=disabled
    )
    try:
        task.create_schedule()
    except Exception as e:
        logger.warning(snakesay(str(e)))
Example #5
0
    def test_creates_daily_task(self, mocker, task_specs):
        mock_create = mocker.patch("pythonanywhere.api.schedule.Schedule.create")
        mock_create.return_value = task_specs
        mock_update_specs = mocker.patch("pythonanywhere.task.Task.update_specs")
        task = Task.to_be_created(command="echo foo", hour=16, minute=0, disabled=False)

        task.create_schedule()

        assert mock_update_specs.call_args == call(task_specs)
        assert mock_create.call_count == 1
        assert mock_create.call_args == call(
            {"command": "echo foo", "hour": 16, "minute": 0, "enabled": True, "interval": "daily"}
        )
Example #6
0
 def test_raises_when_to_be_created_gets_wrong_minute(self):
     with pytest.raises(ValueError) as e:
         Task.to_be_created(command="echo foo", hour=12, minute=78)
     assert str(e.value) == "Minute has to be in 0..59"
Example #7
0
 def test_raises_when_to_be_created_gets_wrong_hour(self):
     with pytest.raises(ValueError) as e:
         Task.to_be_created(command="echo foo", hour=25, minute=1)
     assert str(e.value) == "Hour has to be in 0..23"