Skip to content

Commit 55494c8

Browse files
bdracotoddr
authored andcommitted
Fix exception when serializing a TaskQueue object
If we try to dump a taskqueue object with Cpanel::JSON before loading Cpanel::TaskQueue::Serializer it will crash because TO_JSON hasn't been installed (cherry picked from commit 81a2148d3087eba88146afbb9e79ce6018506c6c) Signed-off-by: Nicolas R <atoomic@cpan.org>
1 parent b580ea2 commit 55494c8

4 files changed

Lines changed: 14 additions & 2 deletions

File tree

lib/cPanel/TaskQueue/Scheduler.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ sub _first (&@) { ## no critic(ProhibitSubr
8989
return;
9090
}
9191

92+
sub TO_JSON {
93+
return { %{ $_[0] } };
94+
}
95+
9296
# Namespace value used when creating unique task ids.
9397
my $tasksched_uuid = 'TaskQueue-Scheduler';
9498

lib/cPanel/TaskQueue/Task.pm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ sub _verify_userdata_arg {
229229
return;
230230
}
231231

232+
sub TO_JSON {
233+
return { %{ $_[0] } };
234+
}
235+
232236
1;
233237

234238
__END__

t/taskqueue_scheduler.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use FindBin;
1212
use lib "$FindBin::Bin/mocks";
1313
use File::Temp ();
1414

15-
use Test::More tests => 93;
15+
use Test::More tests => 94;
1616
use Test::Exception;
1717
use cPanel::TaskQueue::Scheduler;
1818
use MockQueue;
@@ -135,6 +135,8 @@ while ( my $task = $sched->peek_next_task() ) {
135135
ok( $sched->schedule_task( 'noop 0', {} ), 'Scheduled with no time setting.' );
136136
ok( $sched->seconds_until_next_task() <= 0, 'Scheduled right now (or in the last second.' );
137137

138+
is_deeply cPanel::TaskQueue::Scheduler::TO_JSON( {qw/a b c d/} ), { qw/a b c d/ }, "naive TO_JSON helper";
139+
138140
{
139141
my $label = 'flush_all_tasks';
140142

t/taskqueue_task.t

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use strict;
77
use FindBin;
88
use lib "$FindBin::Bin/mocks";
99

10-
use Test::More tests => 27;
10+
use Test::More tests => 28;
1111
use cPanel::TaskQueue::Task;
1212

1313
my $t1 = cPanel::TaskQueue::Task->new( { cmd => 'noop', id => 1, timeout => 10 } );
@@ -58,3 +58,5 @@ is( $t1->pid, $$, ' ... but I can give it one.' );
5858
ok( cPanel::TaskQueue::Task::is_valid_taskid( $t1->uuid() ), 'Taskid is not valid.' );
5959
ok( !cPanel::TaskQueue::Task::is_valid_taskid(), 'Missing taskid is not valid.' );
6060
ok( !cPanel::TaskQueue::Task::is_valid_taskid('fred'), 'badly formed taskid is not valid.' );
61+
62+
is_deeply cPanel::TaskQueue::Task::TO_JSON( { 1..6} ), { 1..6 }, "naive TO_JSON helper";

0 commit comments

Comments
 (0)