public function testDelete() { $repository = new JobRepository(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::RUNNING; $job->attempts = 7; $job->save(); $this->assertNotNull(Job::find($job->id)); $repository->delete($job->id); $this->assertNull(Job::find($job->id)); }
public function testFire() { $task = new GarbageCollectTask(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::SCHEDULED; $job->created_at = Carbon::now()->subDays(32); $job->save(); $job2 = new Job(); $job2->task = 'test.test2'; $job2->state = JobState::FAILED; $job2->created_at = Carbon::now()->subDays(32); $job2->save(); $job3 = new Job(); $job3->task = 'test.test3'; $job3->state = JobState::COMPLETE; $job3->created_at = Carbon::now()->subDays(32); $job3->save(); $job4 = new Job(); $job4->task = 'test.test4'; $job4->state = JobState::CANCELLED; $job4->created_at = Carbon::now()->subDays(32); $job4->save(); $job5 = new Job(); $job5->task = 'test.test5'; $job5->created_at = Carbon::now()->subDays(28); $job5->save(); $gcJob = new Job(); $gcJob->task = 'jobs.gc'; $gcJob->created_at = Carbon::now()->subDays(1); $gcJob->save(); $task->fire($gcJob, m::mock(JobSchedulerInterface::class)); $this->assertNotNull(Job::find($job->id)); $this->assertNull(Job::find($job2->id)); $this->assertNull(Job::find($job3->id)); $this->assertNull(Job::find($job4->id)); $this->assertNotNull(Job::find($job5->id)); }
public function testFireWithCustomQueue() { $job = new Job(); $job->state = JobState::SCHEDULED; $job->run_at = Carbon::now()->subMinute(); $job->queue_connection = 'dogemq'; $job->queue_name = 'food'; $job->task = 'sometask'; $job->save(); $impersonator = new Impersonator(); $impersonator->mock(JobSchedulerInterface::class, function (MockInterface $mock) use($job) { $mock->shouldReceive('findReady')->atLeast()->once()->andReturn(new Collection([$job])); }); $impersonator->mock(Repository::class, function (MockInterface $mock) { $mock->shouldReceive('get')->with('jobs.queue.connection')->andReturn('nopemq')->once(); $mock->shouldReceive('get')->with('jobs.queue.id')->andReturn('fakejobs')->once(); }); $impersonator->mock(QueuePusherInterface::class, function (MockInterface $mock) { $mock->shouldReceive('push')->atLeast()->once()->with(m::type('string'), m::type('array'), 'dogemq', 'food'); }); /** @var EnqueueScheduledCommand $command */ $command = $impersonator->make(EnqueueScheduledCommand::class); $input = new StringInput('--take=25'); $output = new NullOutput(); $command->setLaravel($this->app); $command->run($input, $output); }
public function testFireWithExceptionAndRetries() { $laravelJob = m::mock(LaravelJob::class); $laravelJob->shouldReceive('release')->atLeast()->once(); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::QUEUED; $job->attempts = 0; $job->retries = 5; $job->save(); $handler = m::mock(BaseTask::class); $handler->shouldReceive('fire')->with($job, m::type(JobSchedulerInterface::class))->atLeast()->once()->andThrow('Exception'); $impersonator = new Impersonator(); $impersonator->mock(JobRepositoryInterface::class, function (MockInterface $mock) use($job) { $mock->shouldReceive('find')->with(1337)->atLeast()->once()->andReturn($job); $mock->shouldReceive('started')->with($job, m::type('string'))->atLeast()->once(); $mock->shouldReceive('release')->with($job)->atLeast()->once(); }); $impersonator->mock(HandlerResolverInterface::class, function (MockInterface $mock) use($job, $handler) { $mock->shouldReceive('resolve')->with($job)->atLeast()->once()->andReturn($handler); }); /** @var RunTaskCommand $command */ $command = $impersonator->make(RunTaskCommand::class); $command->fire($laravelJob, ['job_id' => 1337]); }
/** * Cancel a job. * * Note: If the job was pushed on a queue, it won't be removed. However, * workers should query the DB to check if it has been cancelled before * running them. * * @param Job $job */ public function cancel(Job $job) { $job->state = JobState::CANCELLED; $job->save(); }
public function testFindByTag() { $impersonator = new Impersonator(); /** @var JobScheduler $scheduler */ $scheduler = $impersonator->make(JobScheduler::class); $job = new Job(); $job->task = 'test.test'; $job->state = JobState::SCHEDULED; $job->save(); $this->assertEquals(0, $scheduler->findByTag('wow')->count()); $scheduler->tag($job, 'wow'); $scheduler->tag($job, 'omg'); $scheduler->tag($job, 'wow'); $this->assertEquals(1, $scheduler->findByTag('wow')->count()); $this->assertEquals(1, $scheduler->findByTag('omg')->count()); $job->state = JobState::CANCELLED; $job->save(); $this->assertEquals(0, $scheduler->findByTag('wow')->count()); $this->assertEquals(0, $scheduler->findByTag('omg')->count()); $this->assertEquals(1, $scheduler->findByTag('wow', false)->count()); $this->assertEquals(1, $scheduler->findByTag('omg', false)->count()); }
/** * Make sure this job does not run again even if it has retries left. * * This should be used when a problem is detected with the way the job or * task are coded which prevents them from running correctly. * * @param Job $job * @param null $message */ public function giveUp(Job $job, $message = null) { $job->state = JobState::FAILED; $job->attempts = $job->retries + 1; if (!is_null($message)) { $job->message = $message; } $job->save(); }