Skip to content

Commit df2e7ff

Browse files
authored
Merge pull request #31 from dabapps/black
Add and run black
2 parents dcfa829 + d9e0eb2 commit df2e7ff

File tree

14 files changed

+318
-242
lines changed

14 files changed

+318
-242
lines changed

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ env:
1616
install:
1717
- pip install -r test-requirements.txt
1818
- pip install -U django==$DJANGO_VERSION
19-
script: python manage.py test
19+
- if [ "$TRAVIS_PYTHON_VERSION" != "3.5" ]; then pip install black==19.10b0; fi
20+
script:
21+
- if [ "$TRAVIS_PYTHON_VERSION" != "3.5" ]; then black --check django_dbq; fi
22+
- python manage.py test
2023
deploy:
2124
provider: pypi
2225
user: dabapps

django_dbq/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.2.0'
1+
__version__ = "1.2.0"

django_dbq/management/commands/create_job.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,23 @@
1111
class Command(BaseCommand):
1212

1313
help = "Create a job"
14-
args = '<job_name>'
14+
args = "<job_name>"
1515

1616
def add_arguments(self, parser):
17-
parser.add_argument('args', nargs='+')
17+
parser.add_argument("args", nargs="+")
1818
parser.add_argument(
19-
'--workspace',
20-
action='store_true',
21-
dest='workspace',
19+
"--workspace",
20+
action="store_true",
21+
dest="workspace",
2222
default=None,
23-
help="JSON-formatted initial commandworkspace."
23+
help="JSON-formatted initial commandworkspace.",
2424
)
2525
parser.add_argument(
26-
'--queue_name',
27-
action='store_true',
28-
dest='queue_name',
26+
"--queue_name",
27+
action="store_true",
28+
dest="queue_name",
2929
default=None,
30-
help="A specific queue to add this job to"
30+
help="A specific queue to add this job to",
3131
)
3232

3333
def handle(self, *args, **options):
@@ -38,19 +38,22 @@ def handle(self, *args, **options):
3838
if name not in settings.JOBS:
3939
raise CommandError('"%s" is not a valid job name' % name)
4040

41-
workspace = options['workspace']
41+
workspace = options["workspace"]
4242
if workspace:
4343
workspace = json.loads(workspace)
4444

45-
queue_name = options['queue_name']
45+
queue_name = options["queue_name"]
4646

4747
kwargs = {
48-
'name': name,
49-
'workspace': workspace,
48+
"name": name,
49+
"workspace": workspace,
5050
}
5151

5252
if queue_name:
53-
kwargs['queue_name'] = queue_name
53+
kwargs["queue_name"] = queue_name
5454

5555
job = Job.objects.create(**kwargs)
56-
self.stdout.write('Created job: "%s", id=%s for queue "%s"' % (job.name, job.pk, queue_name if queue_name else 'default'))
56+
self.stdout.write(
57+
'Created job: "%s", id=%s for queue "%s"'
58+
% (job.name, job.pk, queue_name if queue_name else "default")
59+
)

django_dbq/management/commands/delete_old_jobs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ class Command(BaseCommand):
88

99
def handle(self, *args, **options):
1010
Job.objects.delete_old()
11-
self.stdout.write('Deleted old jobs')
11+
self.stdout.write("Deleted old jobs")

django_dbq/management/commands/worker.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
logger = logging.getLogger(__name__)
1212

1313

14-
DEFAULT_QUEUE_NAME = 'default'
14+
DEFAULT_QUEUE_NAME = "default"
1515

1616

1717
def process_job(queue_name):
@@ -22,7 +22,14 @@ def process_job(queue_name):
2222
if not job:
2323
return
2424

25-
logger.info('Processing job: name="%s" queue="%s" id=%s state=%s next_task=%s', job.name, queue_name, job.pk, job.state, job.next_task)
25+
logger.info(
26+
'Processing job: name="%s" queue="%s" id=%s state=%s next_task=%s',
27+
job.name,
28+
queue_name,
29+
job.pk,
30+
job.state,
31+
job.next_task,
32+
)
2633
job.state = Job.STATES.PROCESSING
2734
job.save()
2835

@@ -40,18 +47,30 @@ def process_job(queue_name):
4047

4148
failure_hook_name = job.get_failure_hook_name()
4249
if failure_hook_name:
43-
logger.info("Running failure hook %s for job id=%s", failure_hook_name, job.pk)
50+
logger.info(
51+
"Running failure hook %s for job id=%s", failure_hook_name, job.pk
52+
)
4453
failure_hook_function = import_string(failure_hook_name)
4554
failure_hook_function(job, exception)
4655
else:
4756
logger.info("No failure hook for job id=%s", job.pk)
4857

49-
logger.info('Updating job: name="%s" id=%s state=%s next_task=%s', job.name, job.pk, job.state, job.next_task or 'none')
58+
logger.info(
59+
'Updating job: name="%s" id=%s state=%s next_task=%s',
60+
job.name,
61+
job.pk,
62+
job.state,
63+
job.next_task or "none",
64+
)
5065

5166
try:
5267
job.save()
5368
except:
54-
logger.error('Failed to save job: id=%s org=%s', job.pk, job.workspace.get('organisation_id'))
69+
logger.error(
70+
"Failed to save job: id=%s org=%s",
71+
job.pk,
72+
job.workspace.get("organisation_id"),
73+
)
5574
raise
5675

5776

@@ -67,7 +86,11 @@ def __init__(self, name, rate_limit_in_seconds):
6786

6887
def do_work(self):
6988
sleep(1)
70-
if self.last_job_finished and (timezone.now() - self.last_job_finished).total_seconds() < self.rate_limit_in_seconds:
89+
if (
90+
self.last_job_finished
91+
and (timezone.now() - self.last_job_finished).total_seconds()
92+
< self.rate_limit_in_seconds
93+
):
7194
return
7295

7396
process_job(self.queue_name)
@@ -79,14 +102,20 @@ class Command(BaseCommand):
79102
help = "Run a queue worker process"
80103

81104
def add_arguments(self, parser):
82-
parser.add_argument('queue_name', nargs='?', default='default', type=str)
83-
parser.add_argument('rate_limit', help='The rate limit in seconds. The default rate limit is 1 job per second.', nargs='?', default=1, type=int)
105+
parser.add_argument("queue_name", nargs="?", default="default", type=str)
84106
parser.add_argument(
85-
'--dry-run',
86-
action='store_true',
87-
dest='dry_run',
107+
"rate_limit",
108+
help="The rate limit in seconds. The default rate limit is 1 job per second.",
109+
nargs="?",
110+
default=1,
111+
type=int,
112+
)
113+
parser.add_argument(
114+
"--dry-run",
115+
action="store_true",
116+
dest="dry_run",
88117
default=False,
89-
help="Don't actually start the worker. Used for testing."
118+
help="Don't actually start the worker. Used for testing.",
90119
)
91120

92121
def handle(self, *args, **options):
@@ -96,14 +125,17 @@ def handle(self, *args, **options):
96125
if len(args) != 1:
97126
raise CommandError("Please supply a single queue job name")
98127

99-
queue_name = options['queue_name']
100-
rate_limit_in_seconds = options['rate_limit']
128+
queue_name = options["queue_name"]
129+
rate_limit_in_seconds = options["rate_limit"]
101130

102-
self.stdout.write("Starting job worker for queue \"%s\" with rate limit %s/s" % (queue_name, rate_limit_in_seconds))
131+
self.stdout.write(
132+
'Starting job worker for queue "%s" with rate limit %s/s'
133+
% (queue_name, rate_limit_in_seconds)
134+
)
103135

104136
worker = Worker(queue_name, rate_limit_in_seconds)
105137

106-
if options['dry_run']:
138+
if options["dry_run"]:
107139
return
108140

109141
worker.run()

django_dbq/migrations/0001_initial.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,46 @@
1010

1111
class Migration(migrations.Migration):
1212

13-
dependencies = [
14-
]
13+
dependencies = []
1514

1615
operations = [
1716
migrations.CreateModel(
18-
name='Job',
17+
name="Job",
1918
fields=[
20-
('id', UUIDField(serialize=False, editable=False, default=uuid.uuid4, primary_key=True)),
21-
('created', models.DateTimeField(db_index=True, auto_now_add=True)),
22-
('modified', models.DateTimeField(auto_now=True)),
23-
('name', models.CharField(max_length=100)),
24-
('state', models.CharField(db_index=True, max_length=20, default='NEW', choices=[('NEW', 'NEW'), ('READY', 'READY'), ('PROCESSING', 'PROCESSING'), ('FAILED', 'FAILED'), ('COMPLETE', 'COMPLETE')])),
25-
('next_task', models.CharField(max_length=100, blank=True)),
26-
('workspace', jsonfield.fields.JSONField(null=True)),
27-
('queue_name', models.CharField(db_index=True, max_length=20, default='default')),
19+
(
20+
"id",
21+
UUIDField(
22+
serialize=False,
23+
editable=False,
24+
default=uuid.uuid4,
25+
primary_key=True,
26+
),
27+
),
28+
("created", models.DateTimeField(db_index=True, auto_now_add=True)),
29+
("modified", models.DateTimeField(auto_now=True)),
30+
("name", models.CharField(max_length=100)),
31+
(
32+
"state",
33+
models.CharField(
34+
db_index=True,
35+
max_length=20,
36+
default="NEW",
37+
choices=[
38+
("NEW", "NEW"),
39+
("READY", "READY"),
40+
("PROCESSING", "PROCESSING"),
41+
("FAILED", "FAILED"),
42+
("COMPLETE", "COMPLETE"),
43+
],
44+
),
45+
),
46+
("next_task", models.CharField(max_length=100, blank=True)),
47+
("workspace", jsonfield.fields.JSONField(null=True)),
48+
(
49+
"queue_name",
50+
models.CharField(db_index=True, max_length=20, default="default"),
51+
),
2852
],
29-
options={
30-
'ordering': ['-created'],
31-
},
53+
options={"ordering": ["-created"],},
3254
),
3355
]

django_dbq/migrations/0002_auto_20151016_1027.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@
77
class Migration(migrations.Migration):
88

99
dependencies = [
10-
('django_dbq', '0001_initial'),
10+
("django_dbq", "0001_initial"),
1111
]
1212

1313
operations = [
14-
migrations.AlterModelOptions(
15-
name='job',
16-
options={'ordering': ['created']},
17-
),
14+
migrations.AlterModelOptions(name="job", options={"ordering": ["created"]},),
1815
]

django_dbq/migrations/0003_auto_20180713_1000.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@
88
class Migration(migrations.Migration):
99

1010
dependencies = [
11-
('django_dbq', '0002_auto_20151016_1027'),
11+
("django_dbq", "0002_auto_20151016_1027"),
1212
]
1313

1414
operations = [
1515
migrations.AlterModelOptions(
16-
name='job',
17-
options={'ordering': ['-priority', 'created']},
16+
name="job", options={"ordering": ["-priority", "created"]},
1817
),
1918
migrations.AddField(
20-
model_name='job',
21-
name='priority',
19+
model_name="job",
20+
name="priority",
2221
field=models.SmallIntegerField(db_index=True, default=0),
2322
),
2423
]

0 commit comments

Comments
 (0)