Skip to content

Commit

Permalink
update: multiple fixes for cron manager, other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Apr 29, 2024
1 parent 3e88663 commit d1f3ca0
Show file tree
Hide file tree
Showing 23 changed files with 124 additions and 80 deletions.
20 changes: 12 additions & 8 deletions ckanext/ap_cron/assets/js/ap-hyperscript.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
ckan.setSchedule = function (scheduleContainer) {
$(scheduleContainer).val(getSchedule())
ckan.setCreateSchedule = function (scheduleContainer) {
$(scheduleContainer).val(getSchedule("create"))
}

ckan.setEditSchedule = function (scheduleContainer) {
$(scheduleContainer).val(getSchedule("edit"))
}

/**
* Get a cron schedule string
*
* @returns
*/
function getSchedule() {
const min = $("#job-minute").val() || "*";
const hour = $("#job-hour").val() || "*";
const day = $("#job-day").val() || "*";
const month = $("#job-month").val() || "*";
const week = $("#job-week").val() || "*";
function getSchedule(scope) {
const min = $(`#${scope}-job-minute`).val() || "*";
const hour = $(`#${scope}-job-hour`).val() || "*";
const day = $(`#${scope}-job-day`).val() || "*";
const month = $(`#${scope}-job-month`).val() || "*";
const week = $(`#${scope}-job-week`).val() || "*";

return `${min} ${hour} ${day} ${month} ${week}`
}
2 changes: 1 addition & 1 deletion ckanext/ap_cron/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def _job_should_be_started(job: CronJob) -> bool:
if not job.last_run:
return True

now = dt.now()
now = dt.utcnow()
next_run = cron_utils.get_next_run_datetime(job.last_run, job.schedule)

return next_run <= now
Expand Down
8 changes: 1 addition & 7 deletions ckanext/ap_cron/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def ap_cron_add_cron_job(context, data_dict):

log.info("[id:%s] Cron job has been created", job.id)

return job
return job.dictize(context)


@tk.side_effect_free
Expand Down Expand Up @@ -95,9 +95,3 @@ def ap_cron_run_cron_job(context, data_dict):
"job": job.dictize(context),
"success": enqueue_cron_job(job),
}


def aaa_test_cron(context, data_dict):
from time import sleep

sleep(5)
2 changes: 2 additions & 0 deletions ckanext/ap_cron/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def update_cron_job(
json_list_or_string,
ignore,
one_of,
isodate,
cron_job_exists,
cron_schedule_validator,
cron_actions_to_string,
Expand All @@ -94,6 +95,7 @@ def update_cron_job(
"id": [not_missing, unicode_safe, cron_job_exists],
"name": [ignore_missing, unicode_safe],
"schedule": [ignore_missing, unicode_safe, cron_schedule_validator],
"last_run": [ignore_missing, isodate],
"actions": [
ignore_missing,
unicode_safe,
Expand Down
4 changes: 2 additions & 2 deletions ckanext/ap_cron/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def delete(self) -> None:
model.Session.delete(self)

@classmethod
def add(cls, job_data: CronJobData) -> DictizedCronJob:
def add(cls, job_data: CronJobData) -> Self:
job = cls(
name=job_data["name"],
schedule=job_data["schedule"],
Expand All @@ -88,7 +88,7 @@ def add(cls, job_data: CronJobData) -> DictizedCronJob:
model.Session.add(job)
model.Session.commit()

return job.dictize({})
return job

def dictize(self, context) -> DictizedCronJob:
return {
Expand Down
18 changes: 11 additions & 7 deletions ckanext/ap_cron/templates/ap_cron/cron_edit_modal_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

{{ form.input("name", label=_("Name"), value=data.name, is_required=true, attrs={"required": "required", "class": "form-control"}) }}

{{ form.input('actions', label=_('Actions'), value=data.actions|join(",") if data.actions else "", is_required=true, error=error, attrs={"multiple": "multiple", "data-module": "autocomplete", "data-module-source": h.url_for('ap_cron.action_autocomplete', incomplete="?"), "data-module-tags": ""}) }}
{{ form.input('actions', label=_('Actions'), value=data.actions|join(",") if data.actions else "", is_required=true, error=error, attrs={"multiple": "multiple", "data-module": "autocomplete", "data-module-source": "/admin-panel/cron/actions_autocomplete?incomplete=?", "data-module-tags": "", "data-module-createtags": "false"}) }}

{% call form.textarea("data", id=field_data_id, label=_("Data"), value=data.data|tojson if data else "{}", attrs={"data-module": "ap-jsonlint", "data-module-input": "#" + field_data_id, "data-module-result":"#" + field_data_validation_id}) %}
<div id="{{ field_data_validation_id }}"></div>
Expand Down Expand Up @@ -40,26 +40,30 @@

<div class="row">
<div class="col-md-2">
<input type="text" class="form-control" id="job-minute" value="*">
<input type="text" class="form-control" id="{{ scope }}-job-minute" value="*">
</div>

<div class="col-md-2">
<input type="text" class="form-control" id="job-hour" value="*">
<input type="text" class="form-control" id="{{ scope }}-job-hour" value="*">
</div>

<div class="col-md-2">
<input type="text" class="form-control" id="job-day" value="*">
<input type="text" class="form-control" id="{{ scope }}-job-day" value="*">
</div>

<div class="col-md-2">
<input type="text" class="form-control" id="job-month" value="*">
<input type="text" class="form-control" id="{{ scope }}-job-month" value="*">
</div>

<div class="col-md-2">
<input type="text" class="form-control" id="job-week" value="*">
<input type="text" class="form-control" id="{{ scope }}-job-week" value="*">
</div>

<div class="col-md-2"><a class="btn btn-primary" _="{{ 'on click call ckan.setSchedule(#' ~ field_schedule_id ~ ')' }}">Set</a></div>
{% if scope == "create" %}
<div class="col-md-2"><a class="btn btn-primary" _="{{ 'on click call ckan.setCreateSchedule(#' ~ field_schedule_id ~ ')' }}">{{ _("Set") }}</a></div>
{% else %}
<div class="col-md-2"><a class="btn btn-primary" _="{{ 'on click call ckan.setEditSchedule(#' ~ field_schedule_id ~ ')' }}">{{ _("Set") }}</a></div>
{% endif %}
</div>

{{ form.input("schedule", id=field_schedule_id, value=data.schedule, label=_("Schedule"), is_required=true, attrs={"readonly": 1, "required": 1, "class": "form-control"}) }}
2 changes: 1 addition & 1 deletion ckanext/ap_cron/templates/ap_cron/cron_new_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h5 class="modal-title" id="add-cron-job-label">{{ _("Adding a cron job")}}</h5>
</div> <!-- .modal-header -->

<div class="modal-body">
{% snippet 'ap_cron/cron_edit_modal_form.html', data={} %}
{% snippet 'ap_cron/cron_edit_modal_form.html', data={}, scope="create" %}
</div> <!-- .modal-body -->

<div class="modal-footer">
Expand Down
2 changes: 1 addition & 1 deletion ckanext/ap_cron/templates/ap_cron/cron_record.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<a
data-module="ap-tooltip" title="{{ _('Open job logs')}}"
class="btn btn-black"
href="{{ h.url_for('ap_log.list', type=h.ap_cron_get_cron_logger_name(), q=data.id) }}">
href="{{ h.url_for('ap_log.list') + '?ap-logs:name=' + h.ap_cron_get_cron_logger_name() + '&ap-logs:q=' + data.id }}">
<i class="fa-regular fa-file-lines"></i>
</a>

Expand Down
28 changes: 16 additions & 12 deletions ckanext/ap_cron/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import datetime
import logging
from datetime import datetime as dt
from typing import Any

from croniter import croniter
Expand All @@ -15,9 +15,9 @@
log = logging.getLogger(LOG_NAME)


def get_next_run_datetime(date: dt, schedule: str) -> dt:
def get_next_run_datetime(date: datetime.datetime, schedule: str) -> datetime.datetime:
"""Get a datetime object of the next job run"""
return croniter(schedule, date).get_next(dt)
return croniter(schedule, date).get_next(datetime.datetime)


def enqueue_cron_job(job: CronJob) -> bool:
Expand Down Expand Up @@ -73,23 +73,27 @@ def cron_job_pipe(data_dict: dict[str, Any]) -> DictizedCronJob:

job: CronJob = data_dict["data"]["cron_job"]

log.info("[id:%s] the cron job has been started", job.id)
log.info("[id:%s] The cron job has been started", job.id)

_update_job_state({"id": job.id, "state": CronJob.State.running})
_update_job_state(
{
"id": job.id,
"state": CronJob.State.running,
"last_run": datetime.datetime.utcnow(),
}
)

payload = data_dict["data"].get(KWARGS, {})

for action in data_dict["data"]["actions"]:
log.info("[id:%s] starting to run an action %s", job.id, action)
log.info("[id:%s] Starting to run an action %s", job.id, action)

try:
result = tk.get_action(action)(
{"ignore_auth": True}, payload
)
result = tk.get_action(action)({"ignore_auth": True}, payload)
payload = result if result else {}
except tk.ValidationError as e:
e.error_dict["action_name"] = action
e.error_dict["kwargs"] = payload
e.error_dict["kwargs"] = payload # type: ignore
job.data[ERRORS] = e.error_dict

log.exception(
Expand All @@ -101,9 +105,9 @@ def cron_job_pipe(data_dict: dict[str, Any]) -> DictizedCronJob:
{"id": job.id, "state": CronJob.State.failed, "data": job.data}
)

log.info("[id:%s] the action %s was executed successfully...", job.id, action)
log.info("[id:%s] The action %s was executed successfully...", job.id, action)

log.info("[id:%s] cron job has successfuly finished", job.id)
log.info("[id:%s] The cron job has successfuly finished", job.id)

return _update_job_state(
{"id": job.id, "state": CronJob.State.finished, "data": job.data}
Expand Down
3 changes: 2 additions & 1 deletion ckanext/ap_cron/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ def get(self, job_id: str) -> str:
"ap_cron/cron_edit_modal_form.html",
extra_vars={
"data": result,
"scope": "edit"
},
)

Expand Down Expand Up @@ -244,4 +245,4 @@ def action_autocomplete() -> Response:
ap_cron.add_url_rule("/run_active", view_func=CronRunActiveView.as_view("run_active"))

# API
ap_cron.add_url_rule("/util/action/autocomplete", view_func=action_autocomplete)
ap_cron.add_url_rule("/actions_autocomplete", view_func=action_autocomplete)
16 changes: 8 additions & 8 deletions ckanext/ap_doi/config_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ about: Configuration options for the DOI extension
fields:
- field_name: ckanext.ap_doi.mock_api_calls
label: Mock API calls
description: If enabled, the extension will not make any API calls to DataCite
help_text: If enabled, the extension will not make any API calls to DataCite
validators: default(true), one_of([true, false])
preset: select
required: true
Expand All @@ -18,30 +18,30 @@ fields:
- field_name: ckanext.doi.account_name
label: Account name
required: true
description: Your DataCite Repository account
help_text: Your DataCite Repository account
form_snippet: text.html
display_snippet: text.html

- field_name: ckanext.doi.account_password
label: Account password
required: true
description: Your DataCite Repository account password
help_text: Your DataCite Repository account password
input_type: password

- field_name: ckanext.doi.prefix
label: DOI prefix
required: true
description: The prefix taken from your DataCite Repository account (from your test account if running in test mode)
help_text: The prefix taken from your DataCite Repository account (from your test account if running in test mode)

- field_name: ckanext.doi.publisher
label: Publisher
required: true
description: The name of the institution publishing the DOI
help_text: The name of the institution publishing the DOI

- field_name: ckanext.doi.test_mode
label: Test mode
required: true
description: If test mode is set to true, the DOIs will use the DataCite test site. The test site uses a separate account, so you must also change your credentials and prefix.
help_text: If test mode is set to true, the DOIs will use the DataCite test site. The test site uses a separate account, so you must also change your credentials and prefix.
validators: default(true), one_of([true, false])
preset: select
required: true
Expand All @@ -53,8 +53,8 @@ fields:

- field_name: ckanext.doi.site_url
label: Site URL
description: Used to build the link back to the dataset
help_text: Used to build the link back to the dataset

- field_name: ckanext.doi.site_title
label: Site title
description: Site title to use in the citation
help_text: Site title to use in the citation
1 change: 0 additions & 1 deletion ckanext/ap_example/config_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ fields:
validators: default(3600) int_validator
input_type: number


- field_name: ckanext.ap_example.drupal_url
label: Drupal base URL
required: true
Expand Down
Loading

0 comments on commit d1f3ca0

Please sign in to comment.