1package AnyEvent::Gearman::Task;
2use Any::Moose;
3
4use AnyEvent::Gearman::Constants;
5
6BEGIN { do { eval q[use MouseX::Foreign; 1] or die $@ } if any_moose eq 'Mouse' }
7
8extends any_moose('::Object'), 'Object::Event';
9
10has function => (
11    is       => 'rw',
12    isa      => 'Str',
13    required => 1,
14);
15
16has workload => (
17    is       => 'rw',
18    isa      => 'Str',
19    required => 1,
20);
21
22has unique => (
23    is      => 'rw',
24    isa     => 'Str',
25    default => '',
26);
27
28has job_handle => (
29    is      => 'rw',
30    isa     => 'Str',
31    default => '',
32);
33
34has [qw/on_created on_data on_complete on_fail on_status on_warning/] => (
35    is      => 'rw',
36    isa     => 'CodeRef',
37    default => sub { sub {} },
38);
39
40no Any::Moose;
41
42sub BUILDARGS {
43    my ($self, $function, $workload, %params) = @_;
44
45    return {
46        function => $function,
47        workload => $workload,
48        %params,
49    }
50}
51
52sub BUILD {
53    my $self = shift;
54
55    $self->reg_cb(
56        on_created   => $self->on_created,
57        on_data      => $self->on_data,
58        on_complete  => $self->on_complete,
59        on_fail      => $self->on_fail,
60        on_status    => $self->on_status,
61        on_warning   => $self->on_warning,
62    );
63}
64
65sub pack_req {
66    my ($self, $type) = @_;
67    $type = $type && $type eq 'bg'? SUBMIT_JOB_BG : SUBMIT_JOB;
68
69    my $data = $self->function . "\0"
70             . $self->unique . "\0"
71             . $self->workload;
72
73    "\0REQ" . pack('NN', $type, length($data)) . $data;
74}
75
76sub pack_option_req {
77    my ($self, $option) = @_;
78
79    "\0REQ" . pack('NN', OPTION_REQ, length($option)) . $option;
80}
81
82__PACKAGE__->meta->make_immutable;
83
84__END__
85
86=head1 NAME
87
88AnyEvent::Gearman::Task - gearman task
89
90=head1 METHODS
91
92=head2 pack_req
93
94=head2 pack_option_req
95
96=head1 AUTHOR
97
98Daisuke Murase <typester@cpan.org>
99
100Pedro Melo <melo@cpan.org>
101
102=head1 COPYRIGHT AND LICENSE
103
104Copyright (c) 2009 by KAYAC Inc.
105
106This program is free software; you can redistribute
107it and/or modify it under the same terms as Perl itself.
108
109The full text of the license can be found in the
110LICENSE file included with this module.
111
112=cut
113