Ejemplo n.º 1
0
def task_list(mocker):
    username = getpass.getuser()
    specs1 = {
        "can_enable": False,
        "command": "echo foo",
        "enabled": True,
        "expiry": None,
        "extend_url": f"/user/{username}/schedule/task/42/extend",
        "hour": 16,
        "id": 42,
        "interval": "daily",
        "logfile":
        "/user/{username}/files/var/log/tasklog-126708-daily-at-1600-echo_foo.log",
        "minute": 0,
        "printable_time": "16:00",
        "url": f"/api/v0/user/{username}/schedule/42",
        "user": username,
    }
    specs2 = {**specs1}
    specs2.update({"id": 43, "enabled": False})
    mock_task_list = mocker.patch(
        "scripts.pa_get_scheduled_tasks_list.TaskList")
    mock_task_list.return_value.tasks = [
        Task.from_api_specs(specs) for specs in (specs1, specs2)
    ]
    return mock_task_list
Ejemplo n.º 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()
Ejemplo n.º 3
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"
Ejemplo n.º 4
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"
Ejemplo n.º 5
0
    def test_updates_specs(self, task_specs, mocker):
        mock_get_specs = mocker.patch("pythonanywhere.api.schedule.Schedule.get_specs")
        mock_get_specs.return_value = task_specs

        task = Task.from_id(task_id=42)

        for spec, expected_value in task_specs.items():
            assert getattr(task, spec) == expected_value
        assert task.__repr__() == "Daily task <42>: 'echo foo' enabled at 16:00"
Ejemplo n.º 6
0
    def test_raises_when_schedule_delete_fails(self, mocker):
        mock_delete = mocker.patch("pythonanywhere.api.schedule.Schedule.delete")
        mock_delete.side_effect = Exception("error msg")

        with pytest.raises(Exception) as e:
            Task().delete_schedule()

        assert str(e.value) == "error msg"
        assert mock_delete.call_count == 1
Ejemplo n.º 7
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)))
Ejemplo n.º 8
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"}
        )
Ejemplo n.º 9
0
def get_task_from_id(task_id, no_exit=False):
    """Get `Task.from_id` instance representing existing task.

    :param task_id: integer (should be a valid task id)
    :param no_exit: if (default) False sys.exit will be called when
      exception is caught"""

    try:
        return Task.from_id(task_id)
    except Exception as e:
        logger.warning(snakesay(str(e)))
        if not no_exit:
            sys.exit(1)
Ejemplo n.º 10
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"
Ejemplo n.º 11
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"
Ejemplo n.º 12
0
def example_task(task_specs):
    task = Task()
    for spec, value in task_specs.items():
        setattr(task, spec, value)
    return task